TensorFlowのバージョンは0.9.0です。
現在、TensorFlowを用いた学習の勉強をしています。チュートリアルとして手書き文字の認識をするプログラムを書いているのですが、学習モデルが予測した結果や、実際の値を出力する方法が分からず詰まっています。現在の状況としては、現在以下のようなコードで学習を行い、予測と実際の値を出力しようとしているのですが……
Python
1import tensorflow as tf 2from tensorflow.examples.tutorials.mnist import input_data 3 4mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 5 6x = tf.placeholder(tf.float32, [None, 784]) 7 8W = tf.Variable(tf.zeros([784, 10])) 9 10b = tf.Variable(tf.zeros([10])) 11 12y = tf.nn.softmax(tf.matmul(x, W) + b) 13 14y_ = tf.placeholder(tf.float32, [None, 10]) 15 16cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) 17train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) 18 19init = tf.initialize_all_variables() 20sess = tf.Session() 21sess.run(init) 22 23for i in range(1000): 24 batch_xs, batch_ys = mnist.train.next_batch(100) 25 sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) 26 27# 予測と実際の値を出力したい 28x_answer = tf.argmax(y, 1) 29y_answer = tf.argmax(y_, 1) 30print("x", sess.run(x_answer[0:10], feed_dict={x: [1,1,1,1,1,1,1,1,1,1]})) 31print('y', sess.run(y_answer[0:10], feed_dict={x: [1,1,1,1,1,1,1,1,1,1]})) 32 33# 正答率の出力 34correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) 35accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 36print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})) 37 38sess.close()
以下のようなエラーが出て失敗してしまいます。
Traceback (most recent call last): File "test1.py", line 29, in <module> print("x", sess.run(x_answer[0:10], feed_dict={x: [1,1,1,1,1,1,1,1,1,1]})) File "/home/numanicloud/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 372, in run run_metadata_ptr) File "/home/numanicloud/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 625, in _run % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape()))) ValueError: Cannot feed value of shape (10,) for Tensor 'Placeholder:0', which has shape '(?, 784)'
うまくいく方法をご存じの方がいらっしゃいましたら、教えていただけないでしょうか。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。