ValueError:尺寸必须相等,但对于' Mul'是784和500。 (op:' Mul')输入形状:[?,784],[784,500]

时间:2017-02-23 05:18:31

标签: python python-2.7 machine-learning tensorflow

我正在尝试学习Tensor Flow,所以我按照https://pythonprogramming.net/tensorflow-neural-network-session-machine-learning-tutorial/

跟踪了神经网络的教程。

我正在尝试运行代码,但即使我的尺寸看起来正确,也要保持相同的尺寸错误。

我是Tensor Flow的新手,所以我不确定我做错了什么。

我发布代码和错误。

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)

我得到的错误如下 -

  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].

1 个答案:

答案 0 :(得分:10)

错误的发生是因为您使用"乘以"

在您使用的所有行中

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

使用:

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

因为这是矩阵乘法。