feeding tips
placeholder care
placeholder diet
nurturing placeholders
placeholder maintenance

How to feed a placeholder?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In TensorFlow 1.x, a placeholder is a symbolic input that receives data at runtime. "Feeding a placeholder" means supplying a concrete value for it through feed_dict when you execute part of the graph.

What a placeholder is

A placeholder defines the dtype and usually the shape of an input, but it does not contain actual data by itself. That is why trying to evaluate a graph that depends on a placeholder without feeding it causes an error.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5x = tf.compat.v1.placeholder(tf.float32, shape=(None, 2), name="x")
6y = tf.reduce_sum(x, axis=1)
7
8with tf.compat.v1.Session() as sess:
9    result = sess.run(y, feed_dict={x: [[1.0, 2.0], [3.0, 4.0]]})
10    print(result)

The key part is the feed_dict argument. It maps the placeholder object x to real input data with a compatible shape and dtype.

How feeding works at runtime

When sess.run(...) executes, TensorFlow substitutes the provided value for the placeholder in that run only. The graph structure does not change; you are just supplying one batch of input for the current computation.

That makes placeholders useful for training loops, evaluation, and inference in the older graph-based TensorFlow style. You can reuse the same graph many times with different feed values.

Shape and dtype still matter

Feeding is not just about passing any Python object. The value must be compatible with the placeholder definition. If the placeholder expects tf.float32 with shape (None, 2), then a feed such as [[1.0, 2.0], [3.0, 4.0]] works, but a scalar or a tensor with three columns does not.

That is why placeholder errors often mention shape mismatches or dtype mismatches rather than the idea of feeding itself.

Feeding multiple placeholders

It is common to feed several placeholders in one run, such as inputs and labels during training.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5features = tf.compat.v1.placeholder(tf.float32, shape=(None, 2))
6labels = tf.compat.v1.placeholder(tf.float32, shape=(None, 1))
7prediction = tf.reduce_sum(features, axis=1, keepdims=True)
8loss = tf.reduce_mean(tf.square(prediction - labels))
9
10with tf.compat.v1.Session() as sess:
11    value = sess.run(
12        loss,
13        feed_dict={
14            features: [[1.0, 2.0], [2.0, 1.0]],
15            labels: [[3.0], [2.5]],
16        },
17    )
18    print(value)

This is the normal TensorFlow 1.x pattern: build the graph once, then feed placeholders with new mini-batches as needed.

A note about modern TensorFlow

In TensorFlow 2.x, placeholders are no longer the normal API because eager execution is on by default. Most new code works directly with tensors, tf.data pipelines, and Keras models. If you are reading older tutorials or maintaining old graph-mode code, placeholder feeding is still a real concept, but it belongs to the TensorFlow 1.x style.

That distinction matters because many confusing errors come from mixing TensorFlow 1.x tutorials with TensorFlow 2.x defaults.

If you are staying on graph mode intentionally, placeholders are still fine. The main thing is to be explicit about that choice so the rest of the code and documentation match the same TensorFlow execution model.

Common Pitfalls

  • Trying to run a graph that depends on a placeholder without supplying feed_dict.
  • Feeding data with the wrong dtype or incompatible shape.
  • Passing the placeholder name string instead of the actual placeholder object in feed_dict.
  • Forgetting that the feed applies only to the current sess.run call.
  • Mixing TensorFlow 1.x placeholder examples with TensorFlow 2.x eager-execution expectations.

Summary

  • In TensorFlow 1.x, a placeholder is a symbolic input that must be fed at runtime.
  • Use sess.run(..., feed_dict={placeholder: value}) to supply data.
  • The fed value must match the placeholder's expected dtype and shape.
  • Multiple placeholders can be fed in the same run.
  • In TensorFlow 2.x, placeholders are mostly legacy because eager execution replaced the old graph-feeding style.

Course illustration
Course illustration

All Rights Reserved.