InvalidArgumentError:不兼容的形状:[400]与[50]

时间:2018-07-02 16:44:09

标签: python tensorflow

我对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]        [[Node:Equal_4 = Equal [T = DT_INT64,_device =“ / job:localhost /副本: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值与链接中的值相同

1 个答案:

答案 0 :(得分:0)

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

这是经过编辑的代码

# 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)