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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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