首页 > 解决方案 > 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

标签: tensorflowconv-neural-network

解决方案


为了摆脱错误,您需要更正以下几点:

  • 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')

  • 添加一个名为fc3before outlayer 的全连接层:

    fc3 = tf.add(tf.matmul(fc2, weights['WD3']), biases['BD3']) fc3 = tf.nn.relu(fc3)

  • 最后将输出层的输入从 更改fc2fc3

    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

推荐阅读