0
$\begingroup$

I come across this error when I run the following code, trying to get prediction on test data. By printing out operation names, I see no operation named Placeholder_4, and can see an op named prediction_output

with tf.Session() as sess:
    imported_meta = tf.train.import_meta_graph("test-model-220.meta")
    imported_meta.restore(sess, tf.train.latest_checkpoint('./'))

    graph = tf.get_default_graph()

    for op in graph.get_operations():
        print(op.name)

    batch_size = tf.placeholder(tf.int64)
    a, b = tf.placeholder(tf.float32, shape=[None, len(X)]), tf.placeholder(tf.float32, shape=[None, 8])

    train_dataset = tf.data.Dataset.from_tensor_slices((a, b)).batch(batch_size).repeat()
    test_dataset = tf.data.Dataset.from_tensor_slices((a, b)).batch(batch_size)

    iterator = tf.data.Iterator.from_structure(train_dataset.output_types, train_dataset.output_shapes)

    features, labels = iterator.get_next()

    # create the initialisation operations
    training_init_op = iterator.make_initializer(train_dataset)
    test_init_op = iterator.make_initializer(test_dataset)
    # a = tf.placeholder(tf.float32, shape=[None, len(X)])

    sess.run(test_init_op)

    pred = graph.get_tensor_by_name("prediction_output:0")
    prediction = sess.run(pred, feed_dict={a: X, b: [[1, 1, 1, 1, 1, 1, 1, 1]], batch_size: 1})

    # print(prediction)

Stack trace of error:

Traceback (most recent call last):
  File "C:\Users\akshar.b\PycharmProjects\hello\venv\lib\site-packages\tensorflow\python\client\session.py", line 1322, in _do_call
    return fn(*args)
  File "C:\Users\akshar.b\PycharmProjects\hello\venv\lib\site-packages\tensorflow\python\client\session.py", line 1307, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "C:\Users\akshar.b\PycharmProjects\hello\venv\lib\site-packages\tensorflow\python\client\session.py", line 1409, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_4' with dtype float and shape [?,1]
     [[Node: Placeholder_4 = 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:

Traceback (most recent call last):
  File "C:/Users/akshar.b/pythonfiles/Extract_Coordinates/restore.py", line 70, in <module>
    sess.run(test_init_op)
  File "C:\Users\akshar.b\PycharmProjects\hello\venv\lib\site-packages\tensorflow\python\client\session.py", line 900, in run
    run_metadata_ptr)
  File "C:\Users\akshar.b\PycharmProjects\hello\venv\lib\site-packages\tensorflow\python\client\session.py", line 1135, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\akshar.b\PycharmProjects\hello\venv\lib\site-packages\tensorflow\python\client\session.py", line 1316, in _do_run
    run_metadata)
  File "C:\Users\akshar.b\PycharmProjects\hello\venv\lib\site-packages\tensorflow\python\client\session.py", line 1335, 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_4' with dtype float and shape [?,1]
     [[Node: Placeholder_4 = Placeholder[dtype=DT_FLOAT, shape=[?,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

Caused by op 'Placeholder_4', defined at:
  File "C:/Users/akshar.b/pythonfiles/Extract_Coordinates/restore.py", line 56, in <module>
    a, b = tf.placeholder(tf.float32, shape=[None, len(X)]), tf.placeholder(tf.float32, shape=[None, 8])
  File "C:\Users\akshar.b\PycharmProjects\hello\venv\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1808, in placeholder
    return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
  File "C:\Users\akshar.b\PycharmProjects\hello\venv\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 5835, in placeholder
    "Placeholder", dtype=dtype, shape=shape, name=name)
  File "C:\Users\akshar.b\PycharmProjects\hello\venv\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\akshar.b\PycharmProjects\hello\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 3392, in create_op
    op_def=op_def)
  File "C:\Users\akshar.b\PycharmProjects\hello\venv\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): You must feed a value for placeholder tensor 'Placeholder_4' with dtype float and shape [?,1]
     [[Node: Placeholder_4 = Placeholder[dtype=DT_FLOAT, shape=[?,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
$\endgroup$
1
$\begingroup$

You're getting a placeholder exception because you are not feeding a concrete value to place_holder. It looks like a is not holding a concrete value when passed to feed_dict.

$\endgroup$
1
$\begingroup$

A few mistakes that I noticed in your code:

  • a, b = tf.placeholder(tf.float32, shape=[None, len(X)]), tf.placeholder(tf.float32, shape=[None, 8]). Python does not support assignments of a, b= c, d form.
  • I am positively sure you are feeding wrong dimensions for X in the placeholder.
  • I do not think creating a placeholder within a session is a good idea, since it is a part of the computational graph and must be present when the graph is created i.e. before the session is even run.
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.