我正在改编这个教程,以便我在自己的图像集上训练一个卷积神经网络。
我遇到了这个错误:
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_entropy
或 tf.nn.sparse_softmax_cross_entropy_with_logits
。