我正在按如下方式实现CNN,但出现此错误:
ValueError:尺寸必须相等,但“ Add_1”的尺寸必须为10和3072 (操作:“添加”),输入形状为[?,10],[3072]
我在下面附加了我的部分代码,我怀疑该错误来自哪里。
weights = {
'WC1': tf.Variable(tf.random_normal([5, 5, 3, 32]), name='W0'),
'WC2': tf.Variable(tf.random_normal([5, 5, 32, 64]), name='W1'),
'WD1': tf.Variable(tf.random_normal([8 * 8 * 64, 64]), name='W2'),
'WD2': tf.Variable(tf.random_normal([64, n_classes]), name='W3'),
'WD3': tf.Variable(tf.random_normal([128, 3072]), name='W3'),
'out2': tf.Variable(tf.random_normal([3072, n_classes]), name='W3'),
}
biases = {
'BC1': tf.Variable(tf.random_normal([32]), name='B0'),
'BC2': tf.Variable(tf.random_normal([64]), name='B1'),
'BD1': tf.Variable(tf.random_normal([64]), name='B2'),
'BD2': tf.Variable(tf.random_normal([3072]), name='B3'),
'out': tf.Variable(tf.random_normal([10]), name='B3')
}
def conv_net(x, weights, biases):
conv1 = conv2d(x, weights['WC1'], biases['BC1'])
conv1 = maxpool2d(conv1, k=2)
conv1 = normalize_layer(conv1)
conv2 = conv2d(conv1, weights['WC2'], biases['BC2'])
conv2 = maxpool2d(conv2, k=2)
conv2 = normalize_layer(conv2)
fc1 = tf.reshape(conv2, [-1, weights['WD1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['WD1']), biases['BD1'])
fc1 = tf.nn.relu(fc1)
fc2 = tf.add(tf.matmul(fc1, weights['WD2']), biases['BD2'])
fc2 = tf.nn.relu(fc2)
out = tf.add(tf.matmul(fc2, weights['out']), biases['out'])
return out
答案 0 :(得分:1)
以下是您需要纠正的一些要点,以消除错误:
weights['WD2']
从tf.Variable(tf.random_normal([64, n_classes]), name='W3')
更改为tf.Variable(tf.random_normal([64, 128]), name='W3')
biases['BD2']
从tf.Variable(tf.random_normal([3072]), name='B3')
更改为tf.Variable(tf.random_normal([128]), name='B3')
在BD3
字典中添加另一个名为biases
的密钥,如下所示:
'BD3': tf.Variable(tf.random_normal([3072]), name='B3')
在fc3
层之前添加一个名为out
的完全连接的层:
fc3 = tf.add(tf.matmul(fc2, weights['WD3']), biases['BD3'])
fc3 = tf.nn.relu(fc3)
最后将输入层的输入从fc2
更改为fc3
:
out = tf.add(tf.matmul(fc3, weights['out']), biases['out'])
out
字典中没有密钥weights
。因此,您将权重字典中的out2
键更改为out
。我想这一定是错字。weights
和biases
词典中给出的名称。您已多次使用相同的名称。 修改后的代码:
weights = {
'WC1': tf.Variable(tf.random_normal([5, 5, 3, 32]), name='W0'),
'WC2': tf.Variable(tf.random_normal([5, 5, 32, 64]), name='W1'),
'WD1': tf.Variable(tf.random_normal([8 * 8 * 64, 64]), name='W2'),
'WD2': tf.Variable(tf.random_normal([64, 128]), name='W3'),
'WD3': tf.Variable(tf.random_normal([128, 3072]), name='W4'),
'out': tf.Variable(tf.random_normal([3072, n_classes]), name='W5')
}
biases = {
'BC1': tf.Variable(tf.random_normal([32]), name='B0'),
'BC2': tf.Variable(tf.random_normal([64]), name='B1'),
'BD1': tf.Variable(tf.random_normal([64]), name='B2'),
'BD2': tf.Variable(tf.random_normal([128]), name='B3'),
'BD3': tf.Variable(tf.random_normal([3072]), name='B4'),
'out': tf.Variable(tf.random_normal([n_classes]), name='B5')
}
def conv_net(x, weights, biases):
conv1 = conv2d(x, weights['WC1'], biases['BC1'])
conv1 = maxpool2d(conv1, k=2)
conv1 = normalize_layer(conv1)
conv2 = conv2d(conv1, weights['WC2'], biases['BC2'])
conv2 = maxpool2d(conv2, k=2)
conv2 = normalize_layer(conv2)
fc1 = tf.reshape(conv2, [-1, weights['WD1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['WD1']), biases['BD1'])
fc1 = tf.nn.relu(fc1)
fc2 = tf.add(tf.matmul(fc1, weights['WD2']), biases['BD2'])
fc2 = tf.nn.relu(fc2)
fc3 = tf.add(tf.matmul(fc2, weights['WD3']), biases['BD3'])
fc3 = tf.nn.relu(fc3)
out = tf.add(tf.matmul(fc3, weights['out']), biases['out'])
return out