【问题标题】:InvalidArgumentError: Incompatible shapes: [400] vs. [50]InvalidArgumentError:不兼容的形状:[400] 与 [50]
【发布时间】:2018-07-02 16:44:09
【问题描述】:

我对 tensorflow 还很陌生。我已经为二进制分类构建了一个多层 CNN 这是我到目前为止所做的代码

# First Layer
W_conv1 = weight_variable([11, 11, 1, 32])
b_conv1 = bias_variable([32])

x_image = tf.reshape(x, [-1,64,64,1])

h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
print(h_conv1.shape)
print(h_pool1.shape)
# Second Layer
W_conv2 = weight_variable([7, 7, 32, 64])
b_conv2 = bias_variable([64])

h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
print(h_conv2.shape)
print(h_pool2.shape)
#Third Layer 
W_conv3 = weight_variable([5, 5, 64, 128])
b_conv3 = bias_variable([128])

h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3)
h_pool3 = max_pool_2x2(h_conv3)
print(h_conv3.shape)
print(h_pool3.shape)
# Fourth Layer
W_conv4 = weight_variable([3, 3, 128, 64])
b_conv4 = bias_variable([64])

h_conv4 = tf.nn.relu(conv2d(h_pool3, W_conv4) + b_conv4)
h_pool4 = max_pool_2x2(h_conv4)
print(h_conv4.shape)
print(h_pool4.shape)
#Fifth Layer
W_conv5 = weight_variable([2, 2, 64, 32])
b_conv5 = bias_variable([32])

h_conv5 = tf.nn.relu(conv2d(h_pool4, W_conv5) + b_conv5)
h_pool5 = max_pool_2x2(h_conv5)
print(h_conv5.shape)
print(h_pool5.shape)
# Densely Connected Layer
W_fc1 = weight_variable([2 * 2 * 32, 1024])
b_fc1 = bias_variable([1024])

h_pool2_flat = tf.reshape(h_pool4, [-1, 2*2*32])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
print(h_pool2_flat.shape)
print(h_fc1.shape)

下面这段代码的输出是 -

(?, 64, 64, 32)
(?, 32, 32, 32)
(?, 32, 32, 64)
(?, 16, 16, 64)
(?, 16, 16, 128)
(?, 8, 8, 128)
(?, 8, 8, 64)
(?, 4, 4, 64)
(?, 4, 4, 32)
(?, 2, 2, 32)
(?, 256)
(?, 1024)

当我运行程序时出现以下错误


InvalidArgumentError(参见上文的追溯):不兼容的形状:[400] 与 [50] [[节点:Equal_4 = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/device:CPU:0"](ArgMax_8, ArgMax_9)]]

回溯真的很长,我希望只有最后几行有帮助

我尝试通过改变架构来运行网络,只有当只有两个卷积层时才能正常运行。我写的代码引用了这个页面https://www.tensorflow.org/versions/r1.2/get_started/mnist/pros

weight、bias、maxpooling 和 con2d 值与链接中的相同

【问题讨论】:

  • 只需做一些调试,打印出每一步中张量的形状。有一个步骤,您希望拥有 50 大小的张量并获得 1600 大小的张量。
  • 我在调试时注意到错误中的预期形状始终是 batch_size 的 32 倍。
  • 我试着像你说的那样在每一步打印张量,但没有一个是 1600 的形状
  • 形状 50 怎么样?
  • 50 是批量大小,所以它会随着我的变化而变化

标签: python tensorflow


【解决方案1】:

通过更改全连接层中的值来修复错误

这是编辑后的一段代码

# Densely Connected Layer
W_fc1 = weight_variable([2 * 2 * 128, 1024])
b_fc1 = bias_variable([1024])

h_pool2_flat = tf.reshape(h_pool4, [-1, 2*2*128])   
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 2021-09-18
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    相关资源
    最近更新 更多