Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Dimensions must be equal, but are 784 and 500 for 'Mul' (op: 'Mul') with input shapes: [?,784], [784,500]

I am trying to learn Tensor Flow and so I followed this tutorial on Neural Networks by https://pythonprogramming.net/tensorflow-neural-network-session-machine-learning-tutorial/

I am trying to run the code, but keep getting the same dimension Error even when my dimensions seem correct.

I am new to Tensor Flow, so I am not exactly sure what am I doing wrong.

I'll post the code and the error.

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)



n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500

n_classes = 10
batch_size = 100

x = tf.placeholder('float', [None,784])
y = tf.placeholder('float')

def neural_network_model(data):
    #(input_data * weights) + biases


    hidden_1_layer = {
        'weights' : tf.Variable(tf.random_normal([784,n_nodes_hl1])),
        'biases' : tf.Variable(tf.random_normal([n_nodes_hl1]))
    }


     hidden_2_layer = {
        'weights' :                           tf.Variable(tf.random_normal([n_nodes_hl1,n_nodes_hl2])),
        'biases' : tf.Variable(tf.random_normal([n_nodes_hl2]))
    }
    hidden_3_layer = {
        'weights' : tf.Variable(tf.random_normal([n_nodes_hl2,n_nodes_hl3])),
        'biases' : tf.Variable(tf.random_normal([n_nodes_hl3]))
    }
    output_layer = {
        'weights' : tf.Variable(tf.random_normal([n_nodes_hl3,n_classes])),
        'biases' : tf.Variable(tf.random_normal([n_classes]))
    }



     net_Layer1 = tf.add(tf.multiply(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
     output_layer1 = tf.nn.relu(net_Layer1)

     net_Layer2 = tf.add(tf.multiply(output_layer1, hidden_2_layer['weights']), hidden_2_layer['biases'])
     output_layer2 = tf.nn.relu(net_Layer2)

     net_Layer3 = tf.add(tf.multiply(output_layer2, hidden_3_layer['weights']), hidden_3_layer['biases'])
     output_layer3 = tf.nn.relu(net_Layer3)


     output = tf.add(tf.multiply(output_layer3, output_layer['weights']), output_layer['biases'])

     return output


     def train_neural_network(input):
         prediction = neural_network_model(input)
         error = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = prediction,labels = y))

         optimizer = tf.train.AdamOptimizer().minimize(error)

         epochs = 10

         with tf.Session() as sess:
           sess.run(tf.global_variables_initializer)

         for epoch in epochs:
               epoch_loss = 0
             for _ in range(int(mnist.train.num_examples/batch_size)):
                 epoch_x, epoch_y = mnist.train.next_batch(batch_size)
                 _, e = sess.run([optimizer, error], feed_dict={x:epoch_x, y:epoch_y})
                 epoch_loss += e

            print('Epoch', epoch, 'completed out of', epochs, 'loss :', epoch_loss)


           correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y,1))
           accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
           print('Accuracy:', accuracy.eval({x.mnist.test.images, y.mnist.test.labels}))


    train_neural_network(x)

The error I am getting is the following-

  net_Layer1 = tf.add(tf.multiply(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
 File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 357, in multiply
   return gen_math_ops._mul(x, y, name)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 1625, in _mul
   result = _op_def_lib.apply_op("Mul", x=x, y=y, name=name)
 File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
  op_def=op_def)
 File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2397, in create_op
   set_shapes_for_outputs(ret)
 File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1757, in set_shapes_for_outputs
  shapes = shape_func(op)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1707, in call_with_requiring
  return call_cpp_shape_fn(op, require_shape_fn=True)
 File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn
   debug_python_shape_fn, require_shape_fn)
 File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 675, in _call_cpp_shape_fn_impl
  raise ValueError(err.message)
ValueError: Dimensions must be equal, but are 784 and 500 for 'Mul' (op: 'Mul') with input shapes: [?,784], [784,500].
like image 611
Shubh77 Avatar asked Feb 23 '17 05:02

Shubh77


1 Answers

The error comes because you use "multiply"

In all the lines where you use

tf.add(tf.multiply(.....))

Use:

tf.add(tf.matmul(......))

Because this is the matrix multiplication.

like image 136
CrisH Avatar answered Oct 19 '22 04:10

CrisH