InvalidArgumentError: logits和labels必须具有相同大小 logits_size=[80,2] labels_size=[1,80]

我正在改编这个教程,以便我在自己的图像集上训练一个卷积神经网络。

我遇到了这个错误:

Traceback (most recent call last):  File "scr.py", line 416, in <module>    optimize(1)  File "scr.py", line 390, in optimize    session.run(optimizer, feed_dict=feed_dict_train)  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 905, in run    run_metadata_ptr)  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1140, in _run    feed_dict_tensor, options, run_metadata)  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1358, in _do_run    options, run_metadata)  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1377, 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=[80,2] labels_size=[1,80]     [[Node: softmax_cross_entropy_with_logits_sg = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](softmax_cross_entropy_with_logits_sg/Reshape, softmax_cross_entropy_with_logits_sg/Reshape_1)]]Caused by op u'softmax_cross_entropy_with_logits_sg', defined at:  File "scr.py", line 346, in <module>    labels=y_true)  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/util/deprecation.py", line 250, in new_func    return func(*args, **kwargs)  [...]  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1650, in __init__    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-accessInvalidArgumentError (see above for traceback): logits and labels must be same size: logits_size=[80,2] labels_size=[1,80]     [[Node: softmax_cross_entropy_with_logits_sg = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](softmax_cross_entropy_with_logits_sg/Reshape, softmax_cross_entropy_with_logits_sg/Reshape_1)]]

所以错误一定是来自这个调用:

x = tf.placeholder(tf.float32, shape=[None, IMG_HEIGHT, IMG_WIDTH, CHANNELS], name='x')y_true = tf.placeholder(tf.int64, name='y_true')layer_conv1, weights_conv1 = new_conv_layer(input=x,                                            num_input_channels=CHANNELS,                                            filter_size=filter_size1,                                            num_filters=num_filters1,                                            use_pooling=True)layer_conv2, weights_conv2 = new_conv_layer(input=layer_conv1,                                            num_input_channels=num_filters1,                                            filter_size=filter_size2,                                            num_filters=num_filters2,                                            use_pooling=True)layer_flat, num_features = flatten_layer(layer_conv2)layer_fc1 = new_fc_layer(input=layer_flat,                         num_inputs=num_features,                         num_outputs=fc_size,                         use_relu=True)layer_fc2 = new_fc_layer(input=layer_fc1,                         num_inputs=fc_size,                         num_outputs=N_CLASSES,                         use_relu=False)# Predicted Classy_pred = tf.nn.softmax(layer_fc2)y_pred_cls = tf.argmax(y_pred, axis=1)

这是我调用softmax_cross_entropy_with_logits的地方:

# Cost Functioncross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=layer_fc2,                                                        labels=y_true)cost = tf.reduce_mean(cross_entropy)

如果需要,完整源码在这里

logits和labels的大小看起来并不那么不同,那么我做错了什么?


回答:

tf.nn.softmax_cross_entropy_with_logits 期望标签是概率分布(通常是一热编码的标签)。

如果你想使用整数标签,请使用tf.losses.sparse_softmax_cross_entropytf.nn.sparse_softmax_cross_entropy_with_logits

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注