我对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/replica:0/task:0/device:cpu:0”](argmax_8,argmax_9)]
回溯真的很长,我希望只有最后几行有帮助
我尝试通过改变架构来运行网络,只有当只有两个卷积层时,网络才能正常运行。我写的代码引用了这一页
https://www.tensorflow.org/versions/r1.2/get_started/mnist/pros
权重、偏差、最大池和con2d值与链接中的值相同