我正在尝试编写一个自定义模型,其中我正在编写一个自定义train_step函数
我正在从自定义数据生成器创建一个“tf.data.Dataset”,例如
tds = tf.data.Dataset.from_generator(tdg.__iter__,args=None,output_types = (tf.float32,tf.int32),output_shapes = (tf.TensorShape([16,64,64,3]),tf.TensorShape([16])))
tds = tds.batch(1)
Run Code Online (Sandbox Code Playgroud)
在自定义 DataGenerator 中,该__iter__方法定义为
def __iter__(self):
for item in (self[i] for i in range(len(self))):
yield item
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试检索train_step函数内的数据时,x,y = data我得到
Tensor("IteratorGetNext:0", shape=(None, 16, 64, 64, 3), dtype=float32)
和
Tensor("IteratorGetNext:1", shape=(None, 16), dtype=int32) 作为输出
如果我跑print(x[0])那么我得到
Tensor("strided_slice:0", shape=(16,), dtype=int32)
我没有得到具有numpy()属性的张量
这是哪里出错了??
我正在尝试使用 Python3 在 Windows Powershell 和 WSL Linux 终端中打印表情符号。
我尝试过使用 unicode、CLDR 名称,还安装了表情符号库。
print("\U0001F44D")
print(emoji.emojize(':thumbs_up:'))
Run Code Online (Sandbox Code Playgroud)
但在终端中,它只在框中显示一个问号。没有表情符号显示。
这里有什么问题呢?
但如果我在这里复制输出,它就会正确显示。像这样
码:
if (round(xw(1))>2) & (round(xw(2))>2) & (round(xw(1))<h-1) & (round(xw(2))<w-1)
W0 = img(round(xw(1))-2:round(xw(1))+2,round(xw(2))-2:round(xw(2))+2);
else
NA=1;
break
endif
Run Code Online (Sandbox Code Playgroud)
xw是一个列向量,包含一个点的坐标.
h并且w是图像的尺寸.
我在OCTAVE中使用这些代码行
但是当我运行包含这些行的函数时,我会收到警告
warning: Matlab-style short-circuit operation performed for operator &
是否尽管使用&,八度音程正在进行&&操作?
我了解到如果我使用&&那么取决于第一个语句是True或者False,下一个语句被评估.
那么,当我收到此警告时,这是发生了什么事吗?那么这个问题的解决方案是什么?
我想检查是否所有的陈述都是True,而不仅仅是第一个.
我正在尝试在 Tensorflow 中构建多标签二元分类模型。该模型tf.math.reduce_max在两层之间有一个运算符(它不是最大池化,它用于不同的目的)。
班级数为3。
我正在使用二进制交叉熵损失并使用 Adam 优化器。
即使经过几个小时的训练,当我检查预测时,所有预测都在 0.49 到 0.51 的范围内。
该模型似乎没有学习任何东西,而是进行随机预测,这让我认为使用函数tf.math.reduce_max可能会导致问题。
然而,我在网上读到该torch.max函数允许通过它反向传播梯度。
当我检查 Tensorboard 中的图表时,我发现该图表在tf.math.reduce_max操作员处显示为未连接。那么,这个运算符是否允许梯度通过它反向传播?
编辑:添加代码
input_tensor = Input(shape=(256, 256, 3))
base_model_toc = VGG16(input_tensor=input_tensor,weights='imagenet',pooling=None, include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = tf.math.reduce_max(x,axis=0,keepdims=True)
x = Dense(1024,activation='relu')(x)
output_1 = Dense(3, activation='sigmoid')(x)
model_a = Model(inputs=base_model_toc.input, outputs=output_1)
for layer in base_model.layers:
layer.trainable = True
Run Code Online (Sandbox Code Playgroud)
之所以这样tf.math.reduce_max做是axis = 0因为这是这个模型中需要做的
我使用的优化器是 Adam,初始学习率为 0.00001
我在 keras 中创建了一个自定义层,用于在输入 ConvLSTM2D 层之前重塑 CNN 的输出
class TemporalReshape(Layer):
def __init__(self,batch_size,num_patches):
super(TemporalReshape,self).__init__()
self.batch_size = batch_size
self.num_patches = num_patches
def call(self,inputs):
nshape = (self.batch_size,self.num_patches)+inputs.shape[1:]
return tf.reshape(inputs, nshape)
def get_config(self):
config = super().get_config().copy()
config.update({'batch_size':self.batch_size,'num_patches':self.num_patches})
return config
Run Code Online (Sandbox Code Playgroud)
当我尝试使用加载最佳模型时
model = tf.keras.models.load_model('/content/saved_models/model_best.h5',custom_objects={'TemporalReshape':TemporalReshape})
Run Code Online (Sandbox Code Playgroud)
我收到错误
TypeError Traceback (most recent call last)
<ipython-input-83-40b46da33e91> in <module>()
----> 1 model = tf.keras.models.load_model('/content/saved_models/model_best.h5',custom_objects={'TemporalReshape':TemporalReshape})
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/save.py in load_model(filepath, custom_objects, compile, options)
180 if (h5py is not None and (
181 isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
--> 182 return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
183
184 …Run Code Online (Sandbox Code Playgroud) BasicBlock我正在尝试通过torchvision.models.resnet这样做导入课程
from torchvision.models.resnet import *
Run Code Online (Sandbox Code Playgroud)
它没有给出错误,但是当我尝试BasicBlock在代码中使用类(应该已经导入)时,我收到错误
NameError: name 'BasicBlock' is not defined
即使BasicBlock存在于torchvision.models.resnet
但是当我像这样导入时它没有给出错误
from torchvision.models.resnet import BasicBlock
然后在我的代码中使用它
为什么我会收到此错误?