2016-12-28 4 views
3

私はtensorflowを初めて使用しており、チュートリアルに従っています。ここでTensorFlow InvalidArgumentError:Matrix size-compatible:[0]:[100,784]、In [1]:[500,10]

InvalidArgumentError (see above for traceback): Matrix size-compatible: In[0]: [100,784], In[1]: [500,10] 
    [[Node: MatMul_3 = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_0, Variable_6/read)]] 

が私のコードです:

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') #this second parameter makes sure that the image fed in is 28*28 
y = tf.placeholder('float') 

def neural_network_model(data): 
    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]))} 

    # input_data * weights + biases 
    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases']) 
    # activation function 
    l1 = tf.nn.relu(l1) 

    l2 = tf.add(tf.matmul(data, hidden_2_layer['weights']), hidden_2_layer['biases']) 
    l2 = tf.nn.relu(l2) 

    l3 = tf.add(tf.matmul(data, hidden_3_layer['weights']), hidden_3_layer['biases']) 
    l3 = tf.nn.relu(l3) 

    output = tf.matmul(data, output_layer['weights']) + output_layer['biases'] 
    return output 

def train_neural_network(x): 
    prediction = neural_network_model(x) 
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction, y)) 
    #learning rate = 0.001 
    optimizer = tf.train.AdamOptimizer().minimize(cost) 
    hm_epochs = 10 
    with tf.Session() as sess: 
     sess.run(tf.initialize_all_variables()) 
     for epoch in range(hm_epochs): 
      epoch_loss = 0 
      for _ in range(int(mnist.train.num_examples/batch_size)): 
       epoch_x, epoch_y = mnist.train.next_batch(batch_size) 
       _, c = sess.run([optimizer, cost], feed_dict={x:epoch_x,y:epoch_y})//THIS IS THE LINE WHERE THE ERROR 0CCURS 
       epoch_loss += c 
      print 'Epoch ' + epoch + ' completed out of ' + hm_epoch + ' 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) 

私はエラーが私が間違って何をやっているとどのように私はそれを修正することができ起こる行をマークしている私が言うエラーを取得していますか?

スタックオーバーフローが発生すると、詳細を記述するのが難しくなり、詳細が不十分でコードが多すぎると言われます。私は正直なところ、テンソルフローを十分に理解していないので、これ以上の詳細は追加できません。私は誰かがこれで私を助けることができると思っています。私は問題はoptimizercostが異なる次元を持っていると思うが、私はそれについて何をすべきか理解できない。

+1

問題は十分明確ですが、心配しないでください! @Matt D – martianwars

答えて

4

一つのエラーが

l2 = tf.add(tf.matmul(data, hidden_2_layer['weights']), hidden_2_layer['biases']) 

この行にあるあなたの第二の重み変数は、寸法500 x 500を持っていますが、乗算は互換性がありませんので、ごdata変数は100x784データを供給しました。

l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases']) 

はまた l3outputために、対応する変更を行い、これを行います。


は常に

x = tf.placeholder(tf.float32, shape=(None, 784)) 

これはグラフを構築しながら、あなたがそのようなエラーをキャッチすることができます、このように、プレースホルダのための形状を指定し、TensorFlowは、これらのエラーを特定することができるようになります。

+0

ありがとう、私は出力でも 'データ 'を' l3'に変更する必要があると思いますか? –

+0

はい、あなたもそうする必要があります – martianwars

関連する問題