TensorFlow
sess.run
feed_dict
Python programming
machine learning

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.

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5x = tf.compat.v1.placeholder(tf.float32, name="x")
6y = tf.compat.v1.placeholder(tf.float32, name="y")
7result = x + y
8
9with tf.compat.v1.Session() as sess:
10    value = sess.run(result, feed_dict={x: 2.0, y: 3.5})
11    print(value)

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.

python
1import numpy as np
2import tensorflow as tf
3
4tf.compat.v1.disable_eager_execution()
5
6features = tf.compat.v1.placeholder(tf.float32, shape=[None, 2], name="features")
7weights = tf.constant([[2.0], [3.0]])
8output = tf.matmul(features, weights)
9
10batch = np.array([
11    [1.0, 2.0],
12    [3.0, 4.0]
13], dtype=np.float32)
14
15with tf.compat.v1.Session() as sess:
16    value = sess.run(output, feed_dict={features: batch})
17    print(value)

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:

python
feed_dict={x: 2.0, y: 3.0}

Not the usual pattern:

python
feed_dict={"x": 2.0}

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.

python
1import numpy as np
2import tensorflow as tf
3
4tf.compat.v1.disable_eager_execution()
5
6x = tf.compat.v1.placeholder(tf.float32, shape=[None, 1])
7y = tf.compat.v1.placeholder(tf.float32, shape=[None, 1])
8
9w = tf.Variable([[0.0]])
10b = tf.Variable([0.0])
11prediction = tf.matmul(x, w) + b
12loss = tf.reduce_mean(tf.square(prediction - y))
13train_op = tf.compat.v1.train.GradientDescentOptimizer(0.1).minimize(loss)
14
15x_batch = np.array([[1.0], [2.0], [3.0]], dtype=np.float32)
16y_batch = np.array([[2.0], [4.0], [6.0]], dtype=np.float32)
17
18with tf.compat.v1.Session() as sess:
19    sess.run(tf.compat.v1.global_variables_initializer())
20
21    for _ in range(200):
22        sess.run(train_op, feed_dict={x: x_batch, y: y_batch})
23
24    final_loss = sess.run(loss, feed_dict={x: x_batch, y: y_batch})
25    print(final_loss)

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_dict supplies concrete runtime values to placeholders when sess.run(...) executes a TensorFlow 1.x graph.'
  • It lets one graph run with many different input values.
  • The keys in feed_dict are 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.

Course illustration
Course illustration

All Rights Reserved.