2017-02-23 16 views
2

Я пытаюсь изучить Tensor Flow, и поэтому я следил этот учебник по Neural Networks по https://pythonprogramming.net/tensorflow-neural-network-session-machine-learning-tutorial/ValueError: Размеры должны быть равны, но для 785 и 500 для Mul (op: 'Mul') с формами ввода: [?, 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

выглядит так, как будто вы пытаетесь использовать мульт-мульт ('' ''), где matrix-mul - это то, что вы хотите –

ответ

4

ошибка приходит потому, что вы используете "умножать"

Во всех линиях, где вы используете

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

Использование:

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

Потому что это матричное умножение.

+0

Только ответы на вопросы являются низкими качествами и являются кандидатами для удаления. см .: http://meta.stackexchange.com/questions/148272 –

Смежные вопросы