python - InvalidArgumentError:张量流日志和标签仍然不可广播
问题描述
我尝试将 ResNet 构建为用于 RGB 图像分类的卷积模型,我使用 cifar-10 图像数据集来训练我的模型。当我编译和评估模型时,我有一个无效的参数错误说 logits 和 labels 必须是可广播的,我不确定这个错误的来源。我怎样才能摆脱这个错误?谁能指出我的代码有什么问题?有什么办法解决这个问题吗?谢谢
我目前的尝试:
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
在 quick_execute(op_name, num_outputs, inputs, attrs, ctx, name) 58 ctx.ensure_initialized() 59 张量 = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, ---> 60 个输入, attrs, num_outputs) 61 除了核心._NotOkStatusException as e: 62 如果名称不是无:
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) ]] [操作:__inference_train_function_119387]
我不明白为什么会发生这个错误,是我的模型问题还是输入/输出调光设置不正确?有什么快速的方法可以追踪错误并使模型评估工作正常吗?任何想法?谢谢
更新:
在这里,我使用泰勒展开来构建我的计算模型,使用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
时,它运行代码没有任何问题。所以你需要更新那部分代码。希望它有所帮助。
推荐阅读
- c# - 比较同一集合中的两个元素的 lambda 表达式的语法是什么?
- php - WordPress 插件 pagenavi,我看不到超过 2 个页面,即使有 100 多个帖子
- kentico - Kentico 12 DancingGoat MVC SiteName 为空或 null
- maven - 创建 Maven 插件
- java - 在一行中反序列化接口属性的对象?
- spring - 如何在spring-boot-starter-data-rest中为Swagger文档实现snake_case?
- linux - Bash 脚本检查远程运行命令的权限
- html - 带有溢出-x 的响应表:自动和溢出-y:可见不工作
- ios - Can I update an app while it's available for Pre-Order in the App-Store
- excel - 为什么我收到错误“应用程序定义或对象定义错误”?