InvalidArgumentError: tensorflow logits and labels still not broadcastable
InvalidArgumentError: tensorflow logits and labels still not broadcastable
我尝试将 ResNet 构建为用于 RGB 图像分类的卷积模型,我使用 cifar-10 图像数据集来训练我的模型。当我编译和评估模型时,我有一个无效的参数错误说 logits 和标签必须是可广播的,我不确定这个错误的来源。我怎样才能摆脱这个错误?谁能指出我的代码有什么问题?有什么办法解决这个问题吗?谢谢
我目前的尝试:
h = Conv2D(filters=32, kernel_size=(3,3), padding='same', activation='relu', input_shape=input_shape)(h)
h = Conv2D(filters=32, kernel_size=(3,3), activation='relu')(h)
h = MaxPooling2D(pool_size=(2,2))(h)
h = Dropout(0.25)(h)
h = Flatten()(h)
h = Dense(512, activation='relu')(h)
h = Dense(10, activation='softmax')(h)
model = Model(inputs=x, outputs=h)
return model
my_model = resNet_2(train_imgs)
my_model.compile(optimizer="adam", loss="categorical_crossentropy", metrics="accuracy")
model_history = my_model.fit(train_imgs, train_label_one_hot, batch_size=64, epochs=20, validation_data=(test_imgs, test_label_one_hot))
my_model.evaluate(train_imgs, train_label_one_hot)
my_model.evaluate(test_imgs, test_label_one_hot)
错误
InvalidArgumentError Traceback (most recent call last)
<ipython-input-40-5a4cccc5d1ec> in <module>()
3 # my_model.summary()
4 # ## use cifar-10 image dataset
----> 5 my_history = my_model.fit(train_imgs, train_label_one_hot, batch_size=64, epochs=20, validation_data=(test_imgs,
test_label_one_hot))
6 my_model.evaluate(train_imgs, train_label_one_hot)
7 my_model.evaluate(test_imgs, test_label_one_hot)
8 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py
in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:
InvalidArgumentError: logits and labels must be broadcastable: logits_size=[192,10] labels_size=[64,10]
[[node categorical_crossentropy/softmax_cross_entropy_with_logits (defined at
:5) ]]
[Op:__inference_train_function_119387]
我不明白为什么会出现这个错误,是我的模型有问题还是 input/output 暗淡设置不正确?任何快速追踪错误并使模型评估工作正常的方法?任何想法?谢谢
更新:
这里我使用了泰勒展开来建立我的计算模型,使用exp_order
意味着我使用了2个展开项的泰勒级数或近似阶数。谁能告诉我为什么我的模型会出现这样的错误?有任何解决这个问题的方法吗?谢谢
错误来自这部分代码
def get_exp(x, exp_order):
x_ = x[..., None]
x_ = tf.tile(x_, multiples=[1, 1, exp_order + 1])
pows = tf.range(0, exp_order + 1, dtype=tf.float32)
x_p = tf.pow(x_, pows)
return x_p
当exp_order = 2 时,则tf.tile
使x_
的大小为三倍。当您的输入数据为 batch_size = 64 时,上面的代码预计大小为 192。当我输入 exp_order = 0
时,它运行代码没有任何问题。所以你需要更新那部分 code.Hope 它有帮助。
我尝试将 ResNet 构建为用于 RGB 图像分类的卷积模型,我使用 cifar-10 图像数据集来训练我的模型。当我编译和评估模型时,我有一个无效的参数错误说 logits 和标签必须是可广播的,我不确定这个错误的来源。我怎样才能摆脱这个错误?谁能指出我的代码有什么问题?有什么办法解决这个问题吗?谢谢
我目前的尝试:
h = Conv2D(filters=32, kernel_size=(3,3), padding='same', activation='relu', input_shape=input_shape)(h)
h = Conv2D(filters=32, kernel_size=(3,3), activation='relu')(h)
h = MaxPooling2D(pool_size=(2,2))(h)
h = Dropout(0.25)(h)
h = Flatten()(h)
h = Dense(512, activation='relu')(h)
h = Dense(10, activation='softmax')(h)
model = Model(inputs=x, outputs=h)
return model
my_model = resNet_2(train_imgs)
my_model.compile(optimizer="adam", loss="categorical_crossentropy", metrics="accuracy")
model_history = my_model.fit(train_imgs, train_label_one_hot, batch_size=64, epochs=20, validation_data=(test_imgs, test_label_one_hot))
my_model.evaluate(train_imgs, train_label_one_hot)
my_model.evaluate(test_imgs, test_label_one_hot)
错误
InvalidArgumentError Traceback (most recent call last) <ipython-input-40-5a4cccc5d1ec> in <module>() 3 # my_model.summary() 4 # ## use cifar-10 image dataset ----> 5 my_history = my_model.fit(train_imgs, train_label_one_hot, batch_size=64, epochs=20, validation_data=(test_imgs,
test_label_one_hot)) 6 my_model.evaluate(train_imgs, train_label_one_hot) 7 my_model.evaluate(test_imgs, test_label_one_hot)
8 frames /usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/execute.py
in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name) 58 ctx.ensure_initialized() 59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, ---> 60 inputs, attrs, num_outputs) 61 except core._NotOkStatusException as e: 62 if name is not None:
InvalidArgumentError: logits and labels must be broadcastable: logits_size=[192,10] labels_size=[64,10] [[node categorical_crossentropy/softmax_cross_entropy_with_logits (defined at
:5) ]] [Op:__inference_train_function_119387]
我不明白为什么会出现这个错误,是我的模型有问题还是 input/output 暗淡设置不正确?任何快速追踪错误并使模型评估工作正常的方法?任何想法?谢谢
更新:
这里我使用了泰勒展开来建立我的计算模型,使用exp_order
意味着我使用了2个展开项的泰勒级数或近似阶数。谁能告诉我为什么我的模型会出现这样的错误?有任何解决这个问题的方法吗?谢谢
错误来自这部分代码
def get_exp(x, exp_order):
x_ = x[..., None]
x_ = tf.tile(x_, multiples=[1, 1, exp_order + 1])
pows = tf.range(0, exp_order + 1, dtype=tf.float32)
x_p = tf.pow(x_, pows)
return x_p
当exp_order = 2 时,则tf.tile
使x_
的大小为三倍。当您的输入数据为 batch_size = 64 时,上面的代码预计大小为 192。当我输入 exp_order = 0
时,它运行代码没有任何问题。所以你需要更新那部分 code.Hope 它有帮助。