tensorflow - CIFAR-10 TensorFlow CNN 错误操作:'ValueError:尺寸必须相等
问题描述
我正在按如下方式实现 CNN,但出现此错误:
ValueError:尺寸必须相等,但输入形状为 [?,10]、[3072] 的“Add_1”(操作:“Add”)为 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
解决方案
为了摆脱错误,您需要更正以下几点:
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
beforeout
layer 的全连接层: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
推荐阅读
- c - 带有 2 个可执行文件的 Makefile 错误:“main”的多个定义
- javascript - 使用具有所有用户名的 forEach 创建一个数组
- php - 简单的html dom提取没有域名的链接
- linux - 使用 ksh 返回目录中行数最多的文件
- r - 如何删除R中的一个数字
- xcode - 故事板中的所有视图控制器突然只显示安全区域
- html - 为移动站点加载一个图像并隐藏另一个,加载隐藏的图像并在 Squarespace 上隐藏移动图像
- csv - 有没有办法通过 csv 文件在 mysql 工作台中创建关系?
- linux - 创建将基于来自 webform 的值构建子域 (linux/whm/cpanel) 的脚本
- jquery - 循环遍历项目列表(跳过并继续计数)