gpt4 book ai didi

python - logits 和 labels must be same size error 使用 SoftmaxCrossEntropyWithLogits

转载 作者:太空宇宙 更新时间:2023-11-03 11:20:42 25 4
gpt4 key购买 nike

我是 Tensorflow 的新手。我一直在尝试重新设计 Deep MNIST在 MovieLens 数据集上预测电影评级的教程。我稍微简化了模型,因此不再使用 5 分制,而是使用简单的二元 Y/N 评级(类似于 Netflix 上最新的评级系统)。我试图只使用部分评级来预测对新项目的偏好。训练模型时,堆栈跟踪中出现以下错误:

Traceback (most recent call last):
File "/Users/Eric/dev/Coding Academy >Tutorials/tf_impl/deep_tf_group_rec_SO.py", line 223, in <module>
train_step.run(feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 0.5})
File "/Library/Python/2.7/site->packages/tensorflow/python/framework/ops.py", line 1550, in run
_run_using_default_session(self, feed_dict, self.graph, session)
File "/Library/Python/2.7/site->packages/tensorflow/python/framework/ops.py", line 3764, in >_run_using_default_session
session.run(operation, feed_dict)
File "/Library/Python/2.7/site->packages/tensorflow/python/client/session.py", line 767, in run
run_metadata_ptr)
File "/Library/Python/2.7/site->packages/tensorflow/python/client/session.py", line 965, in _run
feed_dict_string, options, run_metadata)
File "/Library/Python/2.7/site->packages/tensorflow/python/client/session.py", line 1015, in _do_run
target_list, options, run_metadata)
File "/Library/Python/2.7/site->packages/tensorflow/python/client/session.py", line 1035, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: logits and >labels must be same size: logits_size=[1,2] labels_size=[50,2]
[[Node: SoftmaxCrossEntropyWithLogits = >SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, >_device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_2, Reshape_3)]]

Caused by op u'SoftmaxCrossEntropyWithLogits', defined at:
File "/Users/Eric/dev/Coding Academy >Tutorials/tf_impl/deep_tf_group_rec_SO.py", line 209, in <module>
cross_entropy = >tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, >logits=y_conv))
File "/Library/Python/2.7/site->packages/tensorflow/python/ops/nn_ops.py", line 1617, in >softmax_cross_entropy_with_logits
precise_logits, labels, name=name)
File "/Library/Python/2.7/site->packages/tensorflow/python/ops/gen_nn_ops.py", line 2265, in >_softmax_cross_entropy_with_logits
features=features, labels=labels, name=name)
File "/Library/Python/2.7/site->packages/tensorflow/python/framework/op_def_library.py", line 763, in >apply_op
op_def=op_def)
File "/Library/Python/2.7/site->packages/tensorflow/python/framework/ops.py", line 2327, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/Library/Python/2.7/site->packages/tensorflow/python/framework/ops.py", line 1226, in __init__
self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): logits and labels must >be same size: logits_size=[1,2] labels_size=[50,2]
[[Node: SoftmaxCrossEntropyWithLogits = >SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, >_device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_2, Reshape_3)]]

导致错误的代码可以查看here

模型中使用的变量大小:

  • x (?, 1682)

  • y_ (?, 2)

  • x_history (?, 290, 290, 1)
  • h_pool1 (?, 145, 145, 32)
  • h_pool2 (?, 73, 73, 64)
  • h_pool3 (?, 37, 37, 128)
  • h_pool4 (?, 19, 19, 256)
  • h_pool5 (?, 10, 10, 512)
  • h_fc1 (?, 1024)
  • h_fc1_drop (?, 1024)
  • y_conv (?, 2)

最佳答案

问题在于您在此行中将输入批处理(形状 50 个训练实例 x 1682 个特征) reshape 为 [-1, 290, 290, 1]:

x_history = tf.reshape(x, [-1, 290, 290, 1])

您可以通过运行以下命令看到 x_history 的形状:

x_history.eval(feed_dict={x:batch[0], y_:batch[1], keep_prob:1.0}).shape

=> (1, 290, 290, 1)

这有效地获取了 50 个实例的所有特征并将其放入单个实例中(第一个维度是 1,它需要为 50),然后通过网络的其余部分运行它。因此,您的 cross_entropy 评估失败了,因为它无法将这批 50 个目标标签与您网络的单个输出对齐。

您需要选择图层形状,以便通过网络保留批尺寸(形状打印输出中的 ?)。一种方法是将特征填充到 1764 并 reshape 为 [-1,42,42,1],因为 42*42 = 1764。

值得注意的是,二维卷积最常用于自然为二维的图像数据。鉴于您的特征不是二维的,您最好从更简单的全连接层网络开始?

关于python - logits 和 labels must be same size error 使用 SoftmaxCrossEntropyWithLogits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43480186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com