Dametime
Dametime

Reputation: 743

CIFAR-10 TensorFlow CNN error op: 'ValueError: Dimensions must be equal

I am implementing the CNN as below, but I got this error:

ValueError: Dimensions must be equal, but are 10 and 3072 for 'Add_1' (op: 'Add') with input shapes: [?,10], [3072]

I have attached my partial code below, where I suspect the error is coming from.

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

Upvotes: 0

Views: 114

Answers (1)

Anubhav Singh
Anubhav Singh

Reputation: 8719

These are few points that you need to correct in order to get rid of the error:

  • change weights['WD2'] from tf.Variable(tf.random_normal([64, n_classes]), name='W3') to tf.Variable(tf.random_normal([64, 128]), name='W3')
  • change biases['BD2'] from tf.Variable(tf.random_normal([3072]), name='B3') to tf.Variable(tf.random_normal([128]), name='B3')
  • Add another key named BD3 in the biases dictionary like below:

    'BD3': tf.Variable(tf.random_normal([3072]), name='B3')

  • Add a fully connected layer named fc3 before out layer:

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

  • Finally change input to the output layer from fc2 to fc3:

    out = tf.add(tf.matmul(fc3, weights['out']), biases['out'])

  • There is no key out in your weights dictionary. So, you change out2 key to out in the weights dictionary. I suppose this must be a typo.
  • One more thing, please correct the names you have given in weights and biases dictionaries. You have used the same name multiple times.

Modified Code:

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

Upvotes: 1

Related Questions