2017-02-23 49 views
2

我想学习张量流量,所以我遵循本教程神经网络https://pythonprogramming.net/tensorflow-neural-network-session-machine-learning-tutorial/ValueError:尺寸必须相等,但对于'Mul'(op:'Mul'),其输入形状为784和500:[?,784],[784,500]

我试图运行的代码,但不断收到相同的尺寸错误,即使我的尺寸看起来正确。

我是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]. 
+2

看起来像你正在尝试element-wise mul(“'*'”)其中matrix-mul是你想要的 –

回答

4

的错误出现,因为你用 “正片叠底”

在所有地方使用

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

线使用:

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

因为这是矩阵乘法。

+0

代码只有答案质量低,是删除的候选人。请参阅:http://meta.stackexchange.com/questions/148272 –

相关问题