undestanding feed_dict in sess.run
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, feed_dict is how you inject runtime values into a graph execution. It lets you keep the computation graph fixed while changing the actual data used when sess.run(...) executes.
The Basic Idea
TensorFlow 1.x separates graph construction from graph execution. You define placeholders and operations first, then call sess.run later to evaluate them.
feed_dict is the bridge between those phases.
Here, x and y are placeholders. They have no concrete values until feed_dict supplies them at runtime.
Why feed_dict Exists
The graph is reusable. You can evaluate the same operation with many different inputs without rebuilding the graph each time.
That is useful for:
- feeding training batches
- evaluating validation data
- testing small examples during debugging
- switching behavior based on runtime placeholders
Without feed_dict, placeholders would remain abstract and the session would not know what values to use.
Feeding Arrays and Batches
feed_dict is not limited to scalars. In machine learning code, it is more often used with vectors, matrices, or minibatches.
The placeholder shape [None, 2] means each row has two values and the batch size can vary.
feed_dict Keys Are Graph Tensors
A common beginner mistake is thinking feed_dict uses string names as keys. Usually you pass the placeholder objects themselves.
Correct:
Not the usual pattern:
The session resolves graph tensors directly, not generic variable names from your Python scope.
Training Loop Example
A classic TensorFlow 1.x training loop repeatedly feeds different minibatches into the same graph.
The graph stays the same across iterations. Only the fed data changes.
Why This Feels Different in TensorFlow 2
If you mostly know TensorFlow 2, feed_dict can seem strange because eager execution changed the default model. In TensorFlow 2 style code, you usually pass tensors directly to Python-callable model or layer objects instead of staging data through placeholders and sessions.
So feed_dict is mainly a TensorFlow 1.x or tf.compat.v1 concept.
Performance Considerations
feed_dict is flexible, but it is not always the most efficient input pipeline for large-scale training. TensorFlow 1.x users often preferred tf.data pipelines or queue-based input systems once performance became important.
Still, for debugging, simple experiments, and small examples, feed_dict remains the easiest way to understand graph execution.
Common Pitfalls
The biggest mistake is forgetting to feed a placeholder at all. If an operation depends on an unfed placeholder, sess.run fails.
Another mistake is feeding data with the wrong shape or dtype. A placeholder defined as tf.float32 with shape [None, 2] cannot accept arbitrary input.
Developers also confuse placeholders with variables. Variables hold state inside the graph; placeholders expect external values from feed_dict.
Finally, if you are writing modern TensorFlow 2 code, do not build new code around feed_dict unless you intentionally rely on tf.compat.v1. It belongs to the older session-based execution model.
Summary
- '
feed_dictsupplies concrete runtime values to placeholders whensess.run(...)executes a TensorFlow 1.x graph.' - It lets one graph run with many different input values.
- The keys in
feed_dictare usually placeholder tensors, not plain string names. - It is useful for debugging and classic training loops, but it is part of the session-based TensorFlow 1.x model.
- In modern TensorFlow 2 code, direct eager execution usually replaces this pattern.

