TensorFlow错误:logits和标签大小必须相同

时间:2015-12-29 21:33:13

标签: tensorflow

我一直在尝试通过基于互联网上的各种示例实施ApproximatelyAlexNet来学习TensorFlow。基本上扩展AlexNet示例here以获取224x224 RGB图像(而不是28x28灰度图像),并添加更多层,更改内核大小,步幅等,以及我在网上找到的其他AlexNet实现。

已经解决了一些不匹配的形状类型错误,但这个让我感到难过:

tensorflow.python.framework.errors.InvalidArgumentError: logits and labels must be same size: logits_size=dim { size: 49 } dim { size: 10 } labels_size=dim { size: 1 } dim { size: 10 }
     [[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"](Softmax, _recv_Placeholder_1_0/_13)]]
     [[Node: gradients/Mean_grad/range_1/_17 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_457_gradients/Mean_grad/range_1", tensor_type=DT_INT32, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

49维尤其令人费解。对于调试,我的批量大小目前为1,如果我将其增加到2,则49变为98。

如果我记录传递给

的x和y的形状
sess.run(optimizer, feed_dict={x: batchImages, y: batchLabels, keepProb: P_DROPOUT})

我得到了

x shape: (1, 150528)
y shape: (1, 10)

符合预期:150528 = 224 * 224 RGB像素,以及代表我的标签的单热矢量。

非常感谢您解决这个问题的任何帮助!

更新代码显示错误:

https://gist.github.com/j4m3z0r/e70096d0f7bd4bd24c42

4 个答案:

答案 0 :(得分:14)

感谢您将代码作为Gist共享。为了使形状一致,有两个必要的变化:

  1. 该行:

    OwnerDraw

    ...负责批量维度中的错误fc1 = tf.reshape(pool5, [-1, wd1Shape[0]]) 。输入为1 x 7 x 7 x 256,并且重新整形为49 x 256,因为49为256.可能的替换如下:

    wd1Shape[0]

    ...这将给pool5Shape = pool5.get_shape().as_list() fc1 = tf.reshape(pool5, [-1, pool5Shape[1] * pool5Shape[2] * pool5Shape[3]]) 形状1 x 12544。

  2. 进行此更改后,fc1权重矩阵(256 x 4096)的大小与'wd1'中的节点数不匹配。您可以按如下方式更改此矩阵的定义:

    fc1

    ...虽然你可能想修改其他权重,或者执行额外的池来减少这个矩阵的大小。

答案 1 :(得分:2)

在使用model.fit(..)时,我遇到了类似的问题。 原来我的output_size定义为2,而应该使用“ binary_crossentropy”作为损失函数,应该将其定义为1。

答案 2 :(得分:0)

鉴于您没有提供实际代码,您使用它很难确切地说出错误。

以下是调试此类问题的一些常规技巧:

  • print(tensor.get_shape())添加到与问题相关的位置(在您的情况下,密集2,外,_加权['输出'],_偏见['输出']是可疑的。)

  • 确保您的矩阵乘法顺序正确(例如,密集2乘_weights ['out'],应该导致batch_size x 10矩阵)。

如果你修改了你链接的AlexNet中的代码,你可能已经改变了下一行:

 dense1 = tf.reshape(norm3, [-1, _weights['wd1'].get_shape().as_list()[0]]) # Reshape conv3 output to fit dense layer input
 dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'], name='fc1') # Relu activation
 dense2 = tf.nn.relu(tf.matmul(dense1, _weights['wd2']) + _biases['bd2'], name='fc2') # Relu activation
 out = tf.matmul(dense2, _weights['out']) + _biases['out']

在你的情况下,dense2的形状可能是[49,1024]。您可以通过添加打印dense2.get_shape()进行检查。你应该为所有张量的形状打印,直到找到一个得到49的形状。我只能猜测你改变了什么,但它可以是重塑形状之一。

答案 3 :(得分:0)

此问题是因为您的类变量和标签不匹配。

例如: - 在您的代码中,您将类变量声明为10 但标签可能不是10。

一旦你创建了类变量并标记了相同的维度。这个问题将得到解决。