Tensorflow: feed dict error: You must feed a value for placeholder tensor

2024/10/14 12:23:51

I have one bug I cannot find out the reason. Here is the code:

with tf.Graph().as_default():global_step = tf.Variable(0, trainable=False)images = tf.placeholder(tf.float32, shape = [FLAGS.batch_size,33,33,1])labels = tf.placeholder(tf.float32, shape = [FLAGS.batch_size,21,21,1])logits = inference(images)losses = loss(logits, labels)train_op = train(losses, global_step)saver = tf.train.Saver(tf.all_variables())summary_op = tf.merge_all_summaries()init = tf.initialize_all_variables()sess = tf.Session()sess.run(init)                                                 summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, sess.graph)for step in xrange(FLAGS.max_steps):start_time = time.time()data_batch, label_batch = SRCNN_inputs.next_batch(np_data, np_label,FLAGS.batch_size)_, loss_value = sess.run([train_op, losses], feed_dict={images: data_batch, labels: label_batch})duration = time.time() - start_timedef next_batch(np_data, np_label, batchsize, training_number = NUM_EXAMPLES_PER_EPOCH_TRAIN):perm = np.arange(training_number)np.random.shuffle(perm)data = np_data[perm]label = np_label[perm]data_batch = data[0:batchsize,:]label_batch = label[0:batchsize,:]return data_batch, label_batch

where np_data is the whole training samples read from hdf5 file, and the same to np_label.

After I run the code, I got the error like this :

2016-07-07 11:16:36.900831: step 0, loss = 55.22 (218.9 examples/sec; 0.585 sec/batch)
Traceback (most recent call last):File "<ipython-input-1-19672e1f8f12>", line 1, in <module>runfile('/home/kang/Documents/work_code_PC1/tf_SRCNN/SRCNN_train.py', wdir='/home/kang/Documents/work_code_PC1/tf_SRCNN')File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfileexecfile(filename, namespace)File "/usr/lib/python3/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 85, in execfileexec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)File "/home/kang/Documents/work_code_PC1/tf_SRCNN/SRCNN_train.py", line 155, in <module>train_test()File "/home/kang/Documents/work_code_PC1/tf_SRCNN/SRCNN_train.py", line 146, in train_testsummary_str = sess.run(summary_op)File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 372, in runrun_metadata_ptr)File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 636, in _runfeed_dict_string, options, run_metadata)File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 708, in _do_runtarget_list, options, run_metadata)File "/usr/local/lib/python3.4/dist-packages/tensorflow/python/client/session.py", line 728, in _do_callraise type(e)(node_def, op, message)InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [128,33,33,1][[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[128,33,33,1], _device="/job:localhost/replica:0/task:0/gpu:0"]()]][[Node: truediv/_74 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_56_truediv", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'Placeholder', defined at:

So, It shows that for the step 0 it has the result, which means that the data has been fed into the Placeholders.

But why does it come the error of feeding data into Placeholder in the next time?

When I try to comment the code summary_op = tf.merge_all_summaries() and the code works fine. why is it the case?

Answer

When I try to comment the code summary_op = tf.merge_all_summaries() and the code works fine. why is it the case?

summary_op is an operation. If there exists (and this is true in your case) a summary operation related to the result of another operation that depends upon the values of the placeholders, you have to feed the graph the required values.

So, your line summary_str = sess.run(summary_op) needs the dictionary of the values to store.

Usually, instead of re-executing the operations to log the values, you run the operations and the summary_op once.

Do something like

if step % LOGGING_TIME_STEP == 0:_, loss_value, summary_str = sess.run([train_op, losses, summary_op], feed_dict={images: data_batch, labels: label_batch})
else:_, loss_value = sess.run([train_op, losses], feed_dict={images: data_batch, labels: label_batch})
https://en.xdnf.cn/q/69413.html

Related Q&A

Pass many pieces of data from Python to C program

I have a Python script and a C program and I need to pass large quantities of data from Python script that call many times the C program. Right now I let the user choose between passing them with an AS…

Parse JavaScript to instrument code

I need to split a JavaScript file into single instructions. For examplea = 2; foo() function bar() {b = 5;print("spam"); }has to be separated into three instructions. (assignment, function ca…

Converting all files (.jpg to .png) from a directory in Python

Im trying to convert all files from a directory from .jpg to .png. The name should remain the same, just the format would change.Ive been doing some researches and came to this:from PIL import Image im…

AssertionError: Gaps in blk ref_locs when unstack() dataframe

I am trying to unstack() data in a Pandas dataframe, but I keep getting this error, and Im not sure why. Here is my code so far with a sample of my data. My attempt to fix it was to remove all rows whe…

Python does not consider distutils.cfg

I have tried everything given and the tutorials all point in the same direction about using mingw as a compiler in python instead of visual c++.I do have visual c++ and mingw both. Problem started comi…

Is it possible to dynamically generate commands in Python Click

Im trying to generate click commands from a configuration file. Essentially, this pattern:import click@click.group() def main():passcommands = [foo, bar, baz] for c in commands:def _f():print("I a…

Different accuracy between python keras and keras in R

I build a image classification model in R by keras for R.Got about 98% accuracy, while got terrible accuracy in python.Keras version for R is 2.1.3, and 2.1.5 in pythonfollowing is the R model code:mod…

Named Entity Recognition in aspect-opinion extraction using dependency rule matching

Using Spacy, I extract aspect-opinion pairs from a text, based on the grammar rules that I defined. Rules are based on POS tags and dependency tags, which is obtained by token.pos_ and token.dep_. Belo…

Python Socket : AttributeError: __exit__

I try to run example from : https://docs.python.org/3/library/socketserver.html#socketserver-tcpserver-example in my laptop but it didnt work.Server :import socketserverclass MyTCPHandler(socketserver.…

How to save pygame Surface as an image to memory (and not to disk)

I am developing a time-critical app on a Raspberry PI, and I need to send an image over the wire. When my image is captured, I am doing like this:# pygame.camera.Camera captures images as a Surface pyg…