Getting "InvalidArgumentError" in TensorFlow when working on MNIST data - python

I'm following a Udemy course and a beginner with CNN with TensorFlow. When I'm running a session I'm getting InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10] error.
with tf.Session() as sess:
sess.run(init)
for step in range(1000):
batch_x, batch_y = mnist.train.next_batch(100)
sess.run(train, feed_dict={x:batch_x, y:batch_y})
# Evaluate the model
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_true,1))
# Output will be like [True, False, True.....] --> Cast to [1.0, 0.0, 1.0.....]
acc = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
print(sess.run(acc,feed_dict={x:mnist.test.images,y_true:mnist.test.labels}))
I'm getting the following error.
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
~/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1326 try:
-> 1327 return fn(*args)
1328 except errors.OpError as e:
~/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
1305 feed_dict, fetch_list, target_list,
-> 1306 status, run_metadata)
1307
~/anaconda3/envs/tfdeeplearning/lib/python3.5/contextlib.py in __exit__(self, type, value, traceback)
65 try:
---> 66 next(self.gen)
67 except StopIteration:
~/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py in raise_exception_on_not_ok_status()
465 compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 466 pywrap_tensorflow.TF_GetCode(status))
467 finally:
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
[[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-30-1df33799cd1a> in <module>()
7 batch_x, batch_y = mnist.train.next_batch(100)
8
----> 9 sess.run(train, feed_dict={x:batch_x, y:batch_y})
10
11 # Evaluate the model
~/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
893 try:
894 result = self._run(None, fetches, feed_dict, options_ptr,
--> 895 run_metadata_ptr)
896 if run_metadata:
897 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1122 if final_fetches or final_targets or (handle and feed_dict_tensor):
1123 results = self._do_run(handle, final_targets, final_fetches,
-> 1124 feed_dict_tensor, options, run_metadata)
1125 else:
1126 results = []
~/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1319 if handle is None:
1320 return self._do_call(_run_fn, self._session, feeds, fetches, targets,
-> 1321 options, run_metadata)
1322 else:
1323 return self._do_call(_prun_fn, self._session, handle, feeds, fetches)
~/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1338 except KeyError:
1339 pass
-> 1340 raise type(e)(node_def, op, message)
1341
1342 def _extend_graph(self):
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
[[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'Placeholder_1', defined at:
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 477, in start
ioloop.IOLoop.instance().start()
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tornado/ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tornado/stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
handler(stream, idents, msg)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2802, in run_ast_nodes
if self.run_code(code, result):
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-23-12e74ec33d57>", line 2, in <module>
y_true = tf.placeholder(tf.float32, shape=[None, 10])
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 1548, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2094, in _placeholder
name=name)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/Users/majain/anaconda3/envs/tfdeeplearning/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
[[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
`

The dimensions of y_true and mnist.test.labels should be consistent.Hope can help you.

It looks like, you haven't initialized the placeholder variables.
Tensorflow constructs a computation graph by using statements that you have written.
So when you write:
sess.run(acc,feed_dict={x:mnist.test.images,y_true:mnist.test.labels})
TensorFlow searches what all things it requires to calculate acc and recursively it looks for other variables.
The error tells that one of the placeholder variable isn't initialized or initialized in a wrong way.
You are feeding wrong variable names in feed_dict. Tensorflow is looking for y_true for computing cross_entropy step but you are feeding it "y". Make following change Computation graph sess.run(train, feed_dict={x:batch_x, y_true:batch_y})

Related

tensorboard written in the jupyter notebook doesn't work

I run the code
import tensorflow as tf
x = tf.placeholder(tf.float32, [None, 1])
y_ = tf.placeholder(tf.float32, [None, 1])
m = tf.Variable(0.)
y = m*x
loss = tf.reduce_mean(tf.pow(y - y_, 2))
update = tf.train.GradientDescentOptimizer(0.0001).minimize(loss)
msum = tf.summary.scalar('m', m)
losssum = tf.summary.scalar('loss', loss)
merged = tf.summary.merge_all()
sess = tf.Session()
file_writer = tf.summary.FileWriter('./my_graph', sess.graph)
sess.run(tf.global_variables_initializer())
data_dict = {x:[[7.01], [3.02], [4.99], [8.]], y_:[[14.01], [6.01], [10.], [16.04]]}
for i in range(0,1000):
[_,curr_sammary] = sess.run([update,merged], feed_dict = data_dict)
file_writer.add_summary(curr_sammary, i)
file_writer.close()
print(sess.run(m))
But this code is not running because of the following error:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1321 try:
-> 1322 return fn(*args)
1323 except errors.OpError as e:
/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1306 return self._call_tf_sessionrun(
-> 1307 options, feed_dict, fetch_list, target_list, run_metadata)
1308
/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1408 self._session, options, feed_dict, fetch_list, target_list,
-> 1409 run_metadata)
1410 else:
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,1]
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[?,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-42-c57f3c0f3525> in <module>()
1 data_dict = {x:[[7.01], [3.02], [4.99], [8.]], y_:[[14.01], [6.01], [10.], [16.04]]}
2 for i in range(0,1):
----> 3 [_,curr_sammary] = sess.run([update,merged], feed_dict = data_dict)
4 file_writer.add_summary(curr_sammary, i)
5
/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
898 try:
899 result = self._run(None, fetches, feed_dict, options_ptr,
--> 900 run_metadata_ptr)
901 if run_metadata:
902 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1133 if final_fetches or final_targets or (handle and feed_dict_tensor):
1134 results = self._do_run(handle, final_targets, final_fetches,
-> 1135 feed_dict_tensor, options, run_metadata)
1136 else:
1137 results = []
/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1314 if handle is None:
1315 return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1316 run_metadata)
1317 else:
1318 return self._do_call(_prun_fn, handle, feeds, fetches)
/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1333 except KeyError:
1334 pass
-> 1335 raise type(e)(node_def, op, message)
1336
1337 def _extend_graph(self):
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,1]
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[?,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op 'Placeholder', defined at:
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.5/dist-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelapp.py", line 486, in start
self.io_loop.start()
File "/usr/local/lib/python3.5/dist-packages/tornado/platform/asyncio.py", line 132, in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.5/asyncio/base_events.py", line 345, in run_forever
self._run_once()
File "/usr/lib/python3.5/asyncio/base_events.py", line 1312, in _run_once
handle._run()
File "/usr/lib/python3.5/asyncio/events.py", line 125, in _run
self._callback(*self._args)
File "/usr/local/lib/python3.5/dist-packages/tornado/platform/asyncio.py", line 122, in _handle_events
handler_func(fileobj, events)
File "/usr/local/lib/python3.5/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py", line 450, in _handle_events
self._handle_recv()
File "/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py", line 480, in _handle_recv
self._run_callback(callback, msg)
File "/usr/local/lib/python3.5/dist-packages/zmq/eventloop/zmqstream.py", line 432, in _run_callback
callback(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
return fn(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/usr/local/lib/python3.5/dist-packages/ipykernel/ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/local/lib/python3.5/dist-packages/ipykernel/zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2662, in run_cell
raw_cell, store_history, silent, shell_futures)
File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2785, in _run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2901, in run_ast_nodes
if self.run_code(code, result):
File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-1-8fcfa2faf332>", line 3, in <module>
x = tf.placeholder(tf.float32, [None, 1])
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/array_ops.py", line 1734, in placeholder
return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 4924, in placeholder
"Placeholder", dtype=dtype, shape=shape, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3414, in create_op
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1740, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,1]
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[?,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
After trying to understand the source of the problem I saw that if the line
[_,curr_sammary] = sess.run([update,merged], feed_dict = data_dict)
in the loop I change the line
sess.run(update, feed_dict = data_dict)
the problem is solved.
I tried to search for understand what the error was and didn't find anything to solve the problem.
I need the line to work in its original form (without the deletions)
I would be happy if someone can tell me what the problem is and what its solution is.
I'm on tensorflow-gpu (1.7.0) and the original, unedited version
[_,curr_sammary] = sess.run([update,merged], feed_dict = data_dict)
runs successfully runs with no errors on both Jupyter and on the terminal.
I DID, however, notice that when i ran the script a second time on Jupyter, that I got the same error as you. This leads me to believe you're keeping state between runs.
On the terminal (where no state is kept between consecutive runs), it all worked fine

My TensorFlow code produces an error with the dimensionality of inputs

InvalidArgumentError Traceback (most recent call last)
C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1321 try:
-> 1322 return fn(*args)
1323 except errors.OpError as e:
C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1306 return self._call_tf_sessionrun(
-> 1307 options, feed_dict, fetch_list, target_list, run_metadata)
1308
C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1408 self._session, options, feed_dict, fetch_list, target_list,
-> 1409 run_metadata)
1410 else:
InvalidArgumentError: Tensor must be 4-D with last dim 1, 3, or 4, not [32,32,28,28]
[[Node: Konvolution1/Konvolution1Bild = ImageSummary[T=DT_FLOAT, bad_color=Tensor<type: uint8 shape: [4] values: 255 0 0...>, max_images=3, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Konvolution1/Konvolution1Bild/tag, Konvolution1/Konvolution1Bild/tensor/_39)]]
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-8-73b3b51081a0> in <module>()
18 print(i, 'Trainingsgenauigkeit = ', genau*100,'%', 'verlust = ', verlust)
19
---> 20 s = sess.run(merged, feed_dict={x:batch_x, y:batch_y, dropoutPH:dropout})
21 writer.add_summary(s, i)
22 genauigkeitTest = sess.run(genauigkeitTraining, feed_dict={x:mnist.test.images,y:mnist.test.labels, keep_prob:1.0})
C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
898 try:
899 result = self._run(None, fetches, feed_dict, options_ptr,
--> 900 run_metadata_ptr)
901 if run_metadata:
902 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1133 if final_fetches or final_targets or (handle and feed_dict_tensor):
1134 results = self._do_run(handle, final_targets, final_fetches,
-> 1135 feed_dict_tensor, options, run_metadata)
1136 else:
1137 results = []
C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1314 if handle is None:
1315 return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1316 run_metadata)
1317 else:
1318 return self._do_call(_prun_fn, handle, feeds, fetches)
C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1333 except KeyError:
1334 pass
-> 1335 raise type(e)(node_def, op, message)
1336
1337 def _extend_graph(self):
InvalidArgumentError: Tensor must be 4-D with last dim 1, 3, or 4, not [32,32,28,28]
[[Node: Konvolution1/Konvolution1Bild = ImageSummary[T=DT_FLOAT, bad_color=Tensor<type: uint8 shape: [4] values: 255 0 0...>, max_images=3, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Konvolution1/Konvolution1Bild/tag, Konvolution1/Konvolution1Bild/tensor/_39)]]
Caused by op 'Konvolution1/Konvolution1Bild', defined at:
File "C:\Anaconda\envs\Matura\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Anaconda\envs\Matura\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "C:\Anaconda\envs\Matura\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
app.start()
File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel\kernelapp.py", line 486, in start
self.io_loop.start()
File "C:\Anaconda\envs\Matura\lib\site-packages\tornado\platform\asyncio.py", line 127, in start
self.asyncio_loop.run_forever()
File "C:\Anaconda\envs\Matura\lib\asyncio\base_events.py", line 421, in run_forever
self._run_once()
File "C:\Anaconda\envs\Matura\lib\asyncio\base_events.py", line 1425, in _run_once
handle._run()
File "C:\Anaconda\envs\Matura\lib\asyncio\events.py", line 127, in _run
self._callback(*self._args)
File "C:\Anaconda\envs\Matura\lib\site-packages\tornado\ioloop.py", line 759, in _run_callback
ret = callback()
File "C:\Anaconda\envs\Matura\lib\site-packages\tornado\stack_context.py", line 276, in null_wrapper
return fn(*args, **kwargs)
File "C:\Anaconda\envs\Matura\lib\site-packages\zmq\eventloop\zmqstream.py", line 536, in <lambda>
self.io_loop.add_callback(lambda : self._handle_events(self.socket, 0))
File "C:\Anaconda\envs\Matura\lib\site-packages\zmq\eventloop\zmqstream.py", line 450, in _handle_events
self._handle_recv()
File "C:\Anaconda\envs\Matura\lib\site-packages\zmq\eventloop\zmqstream.py", line 480, in _handle_recv
self._run_callback(callback, msg)
File "C:\Anaconda\envs\Matura\lib\site-packages\zmq\eventloop\zmqstream.py", line 432, in _run_callback
callback(*args, **kwargs)
File "C:\Anaconda\envs\Matura\lib\site-packages\tornado\stack_context.py", line 276, in null_wrapper
return fn(*args, **kwargs)
File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel\kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel\ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\Anaconda\envs\Matura\lib\site-packages\ipykernel\zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "C:\Anaconda\envs\Matura\lib\site-packages\IPython\core\interactiveshell.py", line 2662, in run_cell
raw_cell, store_history, silent, shell_futures)
File "C:\Anaconda\envs\Matura\lib\site-packages\IPython\core\interactiveshell.py", line 2785, in _run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "C:\Anaconda\envs\Matura\lib\site-packages\IPython\core\interactiveshell.py", line 2901, in run_ast_nodes
if self.run_code(code, result):
File "C:\Anaconda\envs\Matura\lib\site-packages\IPython\core\interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-5-9e8915c7d2a2>", line 26, in <module>
tf.summary.image('Konvolution1Bild', tf.unstack(konv1, axis = 3))
File "C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\summary\summary.py", line 162, in image
tag=tag, tensor=tensor, max_images=max_outputs, name=scope)
File "C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\ops\gen_logging_ops.py", line 387, in image_summary
bad_color=bad_color, name=name)
File "C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\framework\ops.py", line 3392, in create_op
op_def=op_def)
File "C:\Anaconda\envs\Matura\lib\site-packages\tensorflow\python\framework\ops.py", line 1718, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): Tensor must be 4-D with last dim 1, 3, or 4, not [32,32,28,28]
[[Node: Konvolution1/Konvolution1Bild = ImageSummary[T=DT_FLOAT, bad_color=Tensor<type: uint8 shape: [4] values: 255 0 0...>, max_images=3, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Konvolution1/Konvolution1Bild/tag, Konvolution1/Konvolution1Bild/tensor/_39)]]
Snippets of the code that produce this error:
merged = tf.summary.merge_all()
init = tf.global_variables_initializer()
with tf.Session() as sess:
writer= tf.summary.FileWriter( "/tmp/mnist_logs", sess.graph)
sess.run(init)
for i in range(anzahlIterationen):
batch_x, batch_y = mnist.train.next_batch(batchGrösse)
sess.run(training, feed_dict={x: batch_x, y: batch_y, dropoutPH:dropout})
if i % (np.floor(anzahlIterationen/5)) == 0:
verlust, genau = sess.run([kreuzentropie, genauigkeitTraining], feed_dict={x:batch_x, y:batch_y, dropoutPH:dropout})
print(i, 'Trainingsgenauigkeit = ', genau*100,'%', 'verlust = ', verlust)
s = sess.run(merged, feed_dict={x:batch_x, y:batch_y, dropoutPH:dropout})
writer.add_summary(s, i)
genauigkeitTest = sess.run(genauigkeitTraining, feed_dict={x:mnist.test.images,y:mnist.test.labels, keep_prob:1.0})
print("test accuracy %g"%(testAccurac
I really don't know where this error comes from. I can't think of a way to influence the tensors that I input because I want data from that.
The code already worked for previous versions. I can't think of I moment where I changed something with the summary

tf.matmul in gpu-enabled tensorflow and 'InternalError: Blas GEMV launch failed'

I am trying to use my laptop's gpu to perform linear regression on a mock data set (Code part #1) using tensorflow (Code part #2). My problem is that, depending on how y_pred is defined in the cost function block, I get an InternalError when running that block on the gpu. Running it on the cpu however is fine. The traceback is shown at the end of this question (Traceback).
So the question is: What is the reason for this InternalError? How can I fix this so I can use tf.matmul?
Specs: The laptop is running Windows 10 64bit and has a GeForce 940MX card and Intel HD graphics 630. On the software side the laptop is running cuda 9.0 and the corresponding cudnn.
Code part #1:
import matplotlib.pylab as plt
import tensorflow as tf
import numpy as np
# generate some linear train and test data
X_train = np.linspace(0,1,100).reshape((-1,1))
t_train = X_train.dot([1])
t_train = t_train.reshape((-1,1))
X_test = np.linspace(0,1,150).reshape((-1,1))
t_test = X_test.dot([1])
t_tets = t_test.reshape((-1,1))
N, D = X_train.shape
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(X_train, t_train, '.', label="train")
ax.plot(X_test, t_test, '.', label="test")
plt.legend()
plt.show()
Code part #2:
tf.reset_default_graph()
cpu = "/cpu:0"
gpu = "/gpu:0"
# placeholders and weights variable
with tf.device(cpu):
X = tf.placeholder(tf.float32, shape=(None,D), name="X")
y = tf.placeholder(tf.float32, shape=(None,1), name="y")
w = tf.Variable(tf.ones([D,1])*10, name="w")
learning_rate = .1
# cost function
with tf.device(gpu):
y_pred = tf.matmul(X,w) # does not work
#y_pred = X*w # works
error = y_pred - y
cost = tf.reduce_mean(tf.square(error))
# update rule
with tf.device(cpu):
gradient = tf.gradients(cost, [w])[0]
update = tf.assign(w, w - learning_rate * gradient)
init = tf.global_variables_initializer()
n_epochs = 500
n_print = 50
with tf.Session() as sess:
sess.run(init)
for epoch in range(n_epochs):
sess.run(update, feed_dict={X:X_train, y:t_train})
if epoch % n_print == 0:
print("epoch",epoch,
": cost",cost.eval(feed_dict={X:X_train, y:t_train}),
"w",w.eval())
Traceback caused by tf.matmul in Code part #2:
---------------------------------------------------------------------------
InternalError Traceback (most recent call last)
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1360 try:
-> 1361 return fn(*args)
1362 except errors.OpError as e:
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
1339 return tf_session.TF_Run(session, options, feed_dict, fetch_list,
-> 1340 target_list, status, run_metadata)
1341
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
515 compat.as_text(c_api.TF_Message(self.status.status)),
--> 516 c_api.TF_GetCode(self.status.status))
517 # Delete the underlying status object from memory otherwise it stays alive
InternalError: Blas GEMV launch failed: m=1, n=100
[[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_X_0_0/_1, w/read/_3)]]
[[Node: sub/_11 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_21_sub", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
During handling of the above exception, another exception occurred:
InternalError Traceback (most recent call last)
<ipython-input-4-0375a84ed72a> in <module>()
11 for epoch in range(n_epochs):
12
---> 13 sess.run(update, feed_dict={X:X_train, y:t_train})
14
15 if epoch % n_print == 0:
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
903 try:
904 result = self._run(None, fetches, feed_dict, options_ptr,
--> 905 run_metadata_ptr)
906 if run_metadata:
907 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1135 if final_fetches or final_targets or (handle and feed_dict_tensor):
1136 results = self._do_run(handle, final_targets, final_fetches,
-> 1137 feed_dict_tensor, options, run_metadata)
1138 else:
1139 results = []
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1353 if handle is None:
1354 return self._do_call(_run_fn, self._session, feeds, fetches, targets,
-> 1355 options, run_metadata)
1356 else:
1357 return self._do_call(_prun_fn, self._session, handle, feeds, fetches)
E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1372 except KeyError:
1373 pass
-> 1374 raise type(e)(node_def, op, message)
1375
1376 def _extend_graph(self):
InternalError: Blas GEMV launch failed: m=1, n=100
[[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_X_0_0/_1, w/read/_3)]]
[[Node: sub/_11 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_21_sub", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
Caused by op 'MatMul', defined at:
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\__main__.py", line 3, in <module>
app.launch_new_instance()
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
app.start()
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\kernelapp.py", line 477, in start
ioloop.IOLoop.instance().start()
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tornado\ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\kernelbase.py", line 235, in dispatch_shell
handler(stream, idents, msg)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\ipykernel\zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\IPython\core\interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\IPython\core\interactiveshell.py", line 2821, in run_ast_nodes
if self.run_code(code, result):
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-9dfe23bd6608>", line 16, in <module>
y_pred = tf.matmul(X,w) # does not work
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\ops\math_ops.py", line 2064, in matmul
a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 2790, in _mat_mul
name=name)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\framework\ops.py", line 3271, in create_op
op_def=op_def)
File "E:\Programs\Anaconda3\envs\py35-gpu\lib\site-packages\tensorflow\python\framework\ops.py", line 1650, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InternalError (see above for traceback): Blas GEMV launch failed: m=1, n=100
[[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_X_0_0/_1, w/read/_3)]]
[[Node: sub/_11 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_21_sub", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_2' with dtype float

So for the following code:
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(2000):
batch = mnist.train.next_batch(50)
if i % 100 == 0:
train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1]})
print('step %d, training accuracy %g' % (i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1]})
print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
I get this error:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
~/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1326 try:
-> 1327 return fn(*args)
1328 except errors.OpError as e:
~/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
1305 feed_dict, fetch_list, target_list,
-> 1306 status, run_metadata)
1307
~/anaconda/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
87 try:
---> 88 next(self.gen)
89 except StopIteration:
~/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in raise_exception_on_not_ok_status()
465 compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 466 pywrap_tensorflow.TF_GetCode(status))
467 finally:
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_2' with dtype float
[[Node: Placeholder_2 = Placeholder[dtype=DT_FLOAT, shape=<unknown>, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-15-4f0f48e885d9> in <module>()
4 batch = mnist.train.next_batch(50)
5 if i % 100 == 0:
----> 6 train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1]})
7 print('step %d, training accuracy %g' % (i, train_accuracy))
8 train_step.run(feed_dict={x: batch[0], y_: batch[1]})
~/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in eval(self, feed_dict, session)
539
540 """
--> 541 return _eval_using_default_session(self, feed_dict, self.graph, session)
542
543
~/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _eval_using_default_session(tensors, feed_dict, graph, session)
4083 "the tensor's graph is different from the session's "
4084 "graph.")
-> 4085 return session.run(tensors, feed_dict)
4086
4087
~/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
893 try:
894 result = self._run(None, fetches, feed_dict, options_ptr,
--> 895 run_metadata_ptr)
896 if run_metadata:
897 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
~/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1122 if final_fetches or final_targets or (handle and feed_dict_tensor):
1123 results = self._do_run(handle, final_targets, final_fetches,
-> 1124 feed_dict_tensor, options, run_metadata)
1125 else:
1126 results = []
~/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1319 if handle is None:
1320 return self._do_call(_run_fn, self._session, feeds, fetches, targets,
-> 1321 options, run_metadata)
1322 else:
1323 return self._do_call(_prun_fn, self._session, handle, feeds, fetches)
~/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1338 except KeyError:
1339 pass
-> 1340 raise type(e)(node_def, op, message)
1341
1342 def _extend_graph(self):
InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_2' with dtype float
[[Node: Placeholder_2 = Placeholder[dtype=DT_FLOAT, shape=<unknown>, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'Placeholder_2', defined at:
File "/Users/mona/anaconda/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/Users/mona/anaconda/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/mona/anaconda/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "/Users/mona/anaconda/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/Users/mona/anaconda/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 477, in start
ioloop.IOLoop.instance().start()
File "/Users/mona/anaconda/lib/python3.6/site-packages/zmq/eventloop/ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "/Users/mona/anaconda/lib/python3.6/site-packages/tornado/ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "/Users/mona/anaconda/lib/python3.6/site-packages/tornado/stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "/Users/mona/anaconda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "/Users/mona/anaconda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "/Users/mona/anaconda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "/Users/mona/anaconda/lib/python3.6/site-packages/tornado/stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "/Users/mona/anaconda/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/Users/mona/anaconda/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
handler(stream, idents, msg)
File "/Users/mona/anaconda/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/Users/mona/anaconda/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/Users/mona/anaconda/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/Users/mona/anaconda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/Users/mona/anaconda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2802, in run_ast_nodes
if self.run_code(code, result):
File "/Users/mona/anaconda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-4-a02fa3deb1c0>", line 11, in <module>
y_conv = deepnn(x)
File "<ipython-input-3-36fb872ab440>", line 59, in deepnn
keep_prob = tf.placeholder(tf.float32)
File "/Users/mona/anaconda/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1548, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
File "/Users/mona/anaconda/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2094, in _placeholder
name=name)
File "/Users/mona/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/Users/mona/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/Users/mona/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_2' with dtype float
[[Node: Placeholder_2 = Placeholder[dtype=DT_FLOAT, shape=<unknown>, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
How should I go about troubleshooting and fixing this error?
Assuming Placeholder_2 is the digit label, its shape is <unknown> as mentioned in the error message.
But the shape of this placeholder should be [None, 10], where None represent batch number and 10 is for the number of classes.
Create the placeholder like:
y = tf.placeholder(tf.float32, shape=[None, 10])
and you should be fine.
print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
Just delete the blank spaces before "print" in your last line :)

Generating multiple Tensorboard directories

I'm trying to run the same training and Test in a loop, while changing some parameters. Each time the training is done, Tensorflow sould generate a new Tensorboard directory.
And that's exactly the problem I have:
........................
def generatingTBFolder(index):
global TB_Folder
TB_Folder ='TB_Graphs_'+str(index)
with tf.device('/cpu:0'):
for k in range(2,5):## here is the problem
generatingTBFolder(k)
train(k,numberOFclasses)
I don't use a loop like :
with tf.device('/cpu:0'):
generatingTBFolder(3)
train(3,numberOFclasses)
the program works fine and generate a TB folder with the result. but using the loop delivers the following :
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1038 try:
-> 1039 return fn(*args)
1040 except errors.OpError as e:
c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
1020 feed_dict, fetch_list, target_list,
-> 1021 status, run_metadata)
1022
c:\users\engine\appdata\local\programs\python\python35\lib\contextlib.py in __exit__(self, type, value, traceback)
65 try:
---> 66 next(self.gen)
67 except StopIteration:
c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\framework\errors_impl.py in raise_exception_on_not_ok_status()
465 compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 466 pywrap_tensorflow.TF_GetCode(status))
467 finally:
InvalidArgumentError: You must feed a value for placeholder tensor 'input/Features_values' with dtype float
[[Node: input/Features_values = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-2-54a526dfc682> in <module>()
238 for k in range(2,5):
239 generatingTBFolder(k)
--> 240 train(k,numberOFclasses)
<ipython-input-2-54a526dfc682> in train(numberOfFeatures, numberOFclasses)
208 for i in range(max_steps):
209 if i%5 ==0: # Record summarie and Test-set accruracy
--> 210 summary, acc = sess.run([merged,accuracy], feed_dict=feed_dict(False))
211 #summary, acc = sess.run([merged,accuracy], feed_dict={x:np.reshape(Test_Frame.values[:,indices[0:numberOfFeatures]],[len(Test_Frame),numberOfFeatures]),y_:np.asarray(Test_Frame.iloc[:,-1]),keep_prob:0.9})
212 test_writer.add_summary(summary,i)
c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
776 try:
777 result = self._run(None, fetches, feed_dict, options_ptr,
--> 778 run_metadata_ptr)
779 if run_metadata:
780 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
980 if final_fetches or final_targets:
981 results = self._do_run(handle, final_targets, final_fetches,
--> 982 feed_dict_string, options, run_metadata)
983 else:
984 results = []
c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1030 if handle is None:
1031 return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-> 1032 target_list, options, run_metadata)
1033 else:
1034 return self._do_call(_prun_fn, self._session, handle, feed_dict,
c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1050 except KeyError:
1051 pass
-> 1052 raise type(e)(node_def, op, message)
1053
1054 def _extend_graph(self):
InvalidArgumentError: You must feed a value for placeholder tensor 'input/Features_values' with dtype float
[[Node: input/Features_values = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'input/Features_values', defined at:
File "c:\users\engine\appdata\local\programs\python\python35\lib\runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "c:\users\engine\appdata\local\programs\python\python35\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\__main__.py", line 3, in <module>
app.launch_new_instance()
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
app.start()
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\kernelapp.py", line 474, in start
ioloop.IOLoop.instance().start()
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tornado\ioloop.py", line 887, in start
handler_func(fd_obj, events)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\kernelbase.py", line 276, in dispatcher
return self.dispatch_shell(stream, msg)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\kernelbase.py", line 228, in dispatch_shell
handler(stream, idents, msg)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\kernelbase.py", line 390, in execute_request
user_expressions, allow_stdin)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\ipykernel\zmqshell.py", line 501, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\IPython\core\interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\IPython\core\interactiveshell.py", line 2821, in run_ast_nodes
if self.run_code(code, result):
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-1-54a526dfc682>", line 240, in <module>
train(k,numberOFclasses)
File "<ipython-input-1-54a526dfc682>", line 90, in train
x =tf.placeholder(tf.float32,[None,numberOfFeatures],name='Features_values')
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1507, in placeholder
name=name)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 1997, in _placeholder
name=name)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 768, in apply_op
op_def=op_def)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2336, in create_op
original_op=self._default_original_op, op_def=op_def)
File "c:\users\engine\appdata\local\programs\python\python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1228, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'input/Features_values' with dtype float
[[Node: input/Features_values = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
I've already seen this error, when I start a Tensorflow program and it crashes because of shape error or any non syntax error, starting the same program without restarting the python kernel delivers that the same error message.
The problem no is I really need to do that in a loop ( 500 is the actual limit) so there is no way to do it manually.
Any idea how may I solve this ?
I found a workaround to this issue, by starting the whole script using another one, that uses a loop:
if __name__ == "__main__" :
with tf.device('/cpu:0'):
print("Got ", int(sys.argv[1]))
generatingTBFolder(int(sys.argv[1]))
train(int(sys.argv[1]),numberOFclasses)
And here is the script:
import os
for i in range(2,456):
command="python TensorCode.py "+str(i)
os.system(command)
the result looks good in Tensorboard.
hope this may helps someone out there !

Categories

Resources