TensorFlow error: logits and labels must be same size
TensorFlow error: logits and labels must be same size
我一直在尝试通过基于 Internet 上的各种示例实施 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 像素,以及代表我的标签的单热向量。
如果您能帮助解决这个问题,我们将不胜感激!
更新: 此处显示错误的代码:
鉴于您没有提供您正在使用的实际代码,因此很难准确地说出哪里出了问题。
以下是调试此类问题的一些一般提示:
在与问题相关的地方加上print(tensor.get_shape())
(你的情况dense2,out,_weights['out'],_biases['out']是可疑的).
确保您的矩阵乘法顺序正确(例如 dense2 by _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 的张量。我只能猜测您更改了什么,但它可能是重塑之一。
感谢您作为 Gist 分享您的代码。要使形状一致,需要进行两个更改:
行:
fc1 = tf.reshape(pool5, [-1, wd1Shape[0]])
...负责batch维度中的错误49
。输入是 1 x 7 x 7 x 256,它被重塑为 49 x 256,因为 wd1Shape[0]
是 256。一种可能的替换如下:
pool5Shape = pool5.get_shape().as_list()
fc1 = tf.reshape(pool5, [-1, pool5Shape[1] * pool5Shape[2] * pool5Shape[3]])
...这将给 fc1
形状 1 x 12544.
进行此更改后,'wd1'
权重矩阵的大小 (256 x 4096) 与 fc1
中的节点数不匹配。您可以按如下方式更改此矩阵的定义:
'wd1': tf.Variable(tf.random_normal([12544, 4096])),
...尽管您可能想修改其他权重,或执行额外的池化以减小此矩阵的大小。
这个问题是因为您的 class 变量和标签不匹配。
例如:-
在您的代码中,您将 class 变量声明为 10
但标签可能不是 10.
将 class 变量和标签设为相同维度后。此问题将得到解决。
我在使用 model.fit(..) 时遇到了类似的问题。
原来我的 output_size 在使用 "binary_crossentropy" 作为损失函数时被定义为 2,而它应该被定义为 1.
我一直在尝试通过基于 Internet 上的各种示例实施 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 像素,以及代表我的标签的单热向量。
如果您能帮助解决这个问题,我们将不胜感激!
更新: 此处显示错误的代码:
鉴于您没有提供您正在使用的实际代码,因此很难准确地说出哪里出了问题。
以下是调试此类问题的一些一般提示:
在与问题相关的地方加上
print(tensor.get_shape())
(你的情况dense2,out,_weights['out'],_biases['out']是可疑的).确保您的矩阵乘法顺序正确(例如 dense2 by _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 的张量。我只能猜测您更改了什么,但它可能是重塑之一。
感谢您作为 Gist 分享您的代码。要使形状一致,需要进行两个更改:
行:
fc1 = tf.reshape(pool5, [-1, wd1Shape[0]])
...负责batch维度中的错误
49
。输入是 1 x 7 x 7 x 256,它被重塑为 49 x 256,因为wd1Shape[0]
是 256。一种可能的替换如下:pool5Shape = pool5.get_shape().as_list() fc1 = tf.reshape(pool5, [-1, pool5Shape[1] * pool5Shape[2] * pool5Shape[3]])
...这将给
fc1
形状 1 x 12544.进行此更改后,
'wd1'
权重矩阵的大小 (256 x 4096) 与fc1
中的节点数不匹配。您可以按如下方式更改此矩阵的定义:'wd1': tf.Variable(tf.random_normal([12544, 4096])),
...尽管您可能想修改其他权重,或执行额外的池化以减小此矩阵的大小。
这个问题是因为您的 class 变量和标签不匹配。
例如:- 在您的代码中,您将 class 变量声明为 10 但标签可能不是 10.
将 class 变量和标签设为相同维度后。此问题将得到解决。
我在使用 model.fit(..) 时遇到了类似的问题。 原来我的 output_size 在使用 "binary_crossentropy" 作为损失函数时被定义为 2,而它应该被定义为 1.