質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%

Q&A

解決済

1回答

628閲覧

tesorflowのplaceholderエラー

xakamsi

総合スコア13

0グッド

0クリップ

投稿2019/03/04 03:35

編集2019/03/04 03:36

tesorflowを利用したMNISTの実装をしています。
実装時に以下のエラーメッセージが発生しました。

python

1 2import tensorflow as tf 3import os 4from tensorflow.examples.tutorials.mnist import input_data 5 6os.environ['CUDA_VISIBLE_DEVICES'] = "" 7 8def main(): 9# ============init============ 10 # 初期値の定義 11 image_size = 28 * 28 12 output_num = 10 13 learning_rate = 0.001 14 loop_num = 30000 15 batch_size = 100 16 17 mnist = input_data.read_data_sets("MNIST_data/",one_hot=True) 18 19 print(mnist.train.labels[0]) 20 21 # ============model============ 22 with tf.device("/cpu:0"): 23 24 x = tf.placeholder(tf.float32, [None, image_size]) 25 W = tf.Variable(tf.zeros([image_size,output_num])) 26 b = tf.Variable(tf.zeros([output_num])) 27 out = tf.matmul(x,W) + b 28 y = tf.nn.softmax(out) 29 y_ = tf.placeholder(tf.float32, [None, output_num]) 30 31 cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) 32 train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy) 33 correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1)) 34 35 accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) 36 37 init = tf.global_variables_initializer() 38 sess = tf.InteractiveSession() 39 sess.run(init) 40 41 # ============training============ 42 # loop_num回(30000回)トレーニングを行う 43 for i in range(loop_num): 44 batch_xs, batch_ys = mnist.train.next_batch(batch_size) 45 sess.run(train_step ,feed_dict={x:batch_xs, y_:batch_ys}) 46 47 if i % 100 == 0: 48 print("step",i, "train_accuracy:",sess.run(accuracy, feed_dict={x:batch_xs, y_:batch_ys})) 49 50 # ============test============ 51print("test_accurancy:",sess.run(accuracy,feed_dict={x:mnist.test.images, y:mnist.test.labels})) 52 53if __name__ == "__main__": 54 main() 55

エラー内容
WARNING:tensorflow:From C:\Users\MASAKI\Documents\nakatani\sourceTree\MNIST\mnist_beginner.py:23: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\base.py:252: _internal_retry.<locals>.wrap.<locals>.wrapped_fn (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please use urllib or similar directly.
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
WARNING:tensorflow:From C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
WARNING:tensorflow:From C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
WARNING:tensorflow:From C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:290: DataSet.init (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
2019-02-28 22:10:20.434004: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2

Traceback (most recent call last):
File "C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1334, in _do_call
return fn(*args)
File "C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1319, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1407, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
[[{{node Placeholder_1}} = Placeholderdtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\MASAKI\Documents\nakatani\sourceTree\MNIST\mnist_beginner.py", line 90, in <module>
main()
File "C:\Users\MASAKI\Documents\nakatani\sourceTree\MNIST\mnist_beginner.py", line 87, in main
print("test_accurancy:",sess.run(accuracy,feed_dict={x:mnist.test.images, y:mnist.test.labels}))
File "C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 929, in run
run_metadata_ptr)
File "C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1152, in _run
feed_dict_tensor, options, run_metadata)
File "C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1328, in _do_run
run_metadata)
File "C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1348, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
[[node Placeholder_1 (defined at C:\Users\MASAKI\Documents\nakatani\sourceTree\MNIST\mnist_beginner.py:43) = Placeholderdtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

Caused by op 'Placeholder_1', defined at:
File "C:\Users\MASAKI\Documents\nakatani\sourceTree\MNIST\mnist_beginner.py", line 90, in <module>
main()
File "C:\Users\MASAKI\Documents\nakatani\sourceTree\MNIST\mnist_beginner.py", line 43, in main
y_ = tf.placeholder(tf.float32, [None, output_num])
File "C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1747, in placeholder
return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
File "C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 5205, in placeholder
"Placeholder", dtype=dtype, shape=shape, name=name)
File "C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
return func(*args, **kwargs)
File "C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3274, in create_op
op_def=op_def)
File "C:\Users\MASAKI\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1770, in init
self._traceback = tf_stack.extract_stack()

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
[[node Placeholder_1 (defined at C:\Users\MASAKI\Documents\nakatani\sourceTree\MNIST\mnist_beginner.py:43) = Placeholderdtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

機械学習について初学者であるため、
お力添えいただけますと幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

以下を修正したら動きました。

インデントを直す

diff

1 # ============test============ 2- print("test_accurancy:",sess.run(accuracy,feed_dict={x:mnist.test.images, y:mnist.test.labels})) 3+ print("test_accurancy:",sess.run(accuracy,feed_dict={x:mnist.test.images, y:mnist.test.labels}))

y ではなく y_

diff

1 print("test_accurancy:", sess.run(accuracy, feed_dict={ 2- x: mnist.test.images, y: mnist.test.labels})) 3+ x: mnist.test.images, y_: mnist.test.labels}))

投稿2019/03/04 04:38

tiitoi

総合スコア21956

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問