TensorFlow
feed dictionary
scalar
machine learning
deep learning

How do I pass a scalar via a TensorFlow feed dictionary

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In TensorFlow 1.x, the feed dictionary (feed_dict) was used to pass values into a computation graph through tf.placeholder nodes during session.run(). For scalar values, you create a tf.placeholder with shape () and feed a Python number or NumPy scalar. In TensorFlow 2.x (eager mode), placeholders are replaced by function arguments — you pass Python values directly. This article covers both approaches and how to migrate from feed dicts to modern TF2 patterns.

TensorFlow 1.x: Scalar Placeholder with feed_dict

python
1import tensorflow as tf
2
3# Disable TF2 eager mode to use TF1-style sessions
4tf.compat.v1.disable_eager_execution()
5
6# Create scalar placeholder (shape=() means 0-dimensional)
7learning_rate = tf.compat.v1.placeholder(dtype=tf.float32, shape=(), name='lr')
8dropout_rate = tf.compat.v1.placeholder(dtype=tf.float32, shape=(), name='dropout')
9
10# Use in computation
11x = tf.constant([1.0, 2.0, 3.0])
12scaled = x * learning_rate
13
14# Run with feed_dict
15with tf.compat.v1.Session() as sess:
16    result = sess.run(scaled, feed_dict={
17        learning_rate: 0.01,  # Python float → scalar tensor
18        dropout_rate: 0.5
19    })
20    print(result)  # [0.01, 0.02, 0.03]

Passing Different Scalar Types

python
1# Integer scalar
2epochs = tf.compat.v1.placeholder(dtype=tf.int32, shape=())
3
4# Boolean scalar
5is_training = tf.compat.v1.placeholder(dtype=tf.bool, shape=())
6
7# String scalar
8model_name = tf.compat.v1.placeholder(dtype=tf.string, shape=())
9
10with tf.compat.v1.Session() as sess:
11    result = sess.run(
12        [epochs, is_training, model_name],
13        feed_dict={
14            epochs: 100,           # Python int
15            is_training: True,      # Python bool
16            model_name: "resnet50"  # Python string
17        }
18    )

Dynamic Training Parameters

The most common use case — varying hyperparameters during training:

python
1import numpy as np
2
3lr_placeholder = tf.compat.v1.placeholder(tf.float32, shape=(), name='lr')
4keep_prob = tf.compat.v1.placeholder(tf.float32, shape=(), name='keep_prob')
5
6# Model operations using the placeholders
7# optimizer = tf.train.AdamOptimizer(learning_rate=lr_placeholder)
8
9with tf.compat.v1.Session() as sess:
10    for epoch in range(100):
11        # Decay learning rate
12        current_lr = 0.01 * (0.95 ** epoch)
13
14        sess.run(train_op, feed_dict={
15            lr_placeholder: current_lr,
16            keep_prob: 0.8 if epoch < 50 else 0.9,
17            # ... other placeholders
18        })

TensorFlow 2.x: No Placeholders Needed

In TF2 with eager execution, pass values directly as function arguments:

python
1import tensorflow as tf
2
3# TF2 — just use Python values directly
4learning_rate = 0.01
5x = tf.constant([1.0, 2.0, 3.0])
6result = x * learning_rate
7print(result)  # [0.01, 0.02, 0.03]
8
9# With tf.function for graph optimization
10@tf.function
11def train_step(data, lr, dropout_rate):
12    # Use lr and dropout_rate directly — no feed_dict
13    scaled = data * lr
14    return scaled
15
16result = train_step(
17    tf.constant([1.0, 2.0, 3.0]),
18    tf.constant(0.01),
19    tf.constant(0.5)
20)

Migration: TF1 feed_dict to TF2

python
1# TF1 style
2placeholder = tf.compat.v1.placeholder(tf.float32, shape=())
3output = some_model(placeholder)
4with tf.compat.v1.Session() as sess:
5    result = sess.run(output, feed_dict={placeholder: 42.0})
6
7# TF2 equivalent
8@tf.function
9def compute(value):
10    return some_model(value)
11
12result = compute(tf.constant(42.0))
13
14# Or simply:
15result = some_model(42.0)  # Eager mode, no session needed

Using tf.Variable for Mutable Scalars

For values that change during training (like learning rate schedules):

python
1# TF2 approach with Variable
2lr = tf.Variable(0.01, dtype=tf.float32, trainable=False)
3
4# Update during training
5for epoch in range(100):
6    lr.assign(0.01 * (0.95 ** epoch))
7    # Use lr in optimizer or computation
8    optimizer = tf.keras.optimizers.Adam(learning_rate=lr)

Keras Learning Rate Schedules

The modern replacement for feeding learning rate scalars:

python
1# Instead of manually feeding lr via feed_dict:
2schedule = tf.keras.optimizers.schedules.ExponentialDecay(
3    initial_learning_rate=0.01,
4    decay_steps=1000,
5    decay_rate=0.95
6)
7
8optimizer = tf.keras.optimizers.Adam(learning_rate=schedule)
9
10# Or with a callback
11class LRLogger(tf.keras.callbacks.Callback):
12    def on_epoch_end(self, epoch, logs=None):
13        lr = self.model.optimizer.learning_rate
14        if callable(lr):
15            lr = lr(self.model.optimizer.iterations)
16        print(f"Epoch {epoch}: lr = {lr:.6f}")
17
18model.fit(data, epochs=100, callbacks=[LRLogger()])

Common Pitfalls

  • Shape mismatch: Passing a list [0.01] instead of a scalar 0.01 to a shape=() placeholder raises ValueError: Cannot feed value of shape (1,) for Tensor with shape (). Pass a plain Python number, not a list.
  • Wrong dtype: Passing a Python int to a float32 placeholder works (auto-cast), but passing a string to a float32 placeholder raises TypeError. Match the Python type to the placeholder dtype.
  • Using feed_dict in TF2: tf.compat.v1.placeholder and feed_dict work in TF2 with disable_eager_execution(), but they are deprecated. Migrate to @tf.function with direct arguments for new code.
  • Forgetting to feed all placeholders: If the computation graph uses placeholders A and B but feed_dict only provides A, TensorFlow raises InvalidArgumentError: You must feed a value for placeholder tensor 'B'.
  • Performance with feed_dict: In TF1, feed_dict copies data from Python to the TF runtime on every sess.run(). For training loops with fixed hyperparameters, use tf.Variable instead to avoid the copy overhead.

Summary

  • In TF1, create tf.placeholder(dtype, shape=()) for scalar values and pass them via feed_dict in sess.run()
  • In TF2, pass Python values or tf.constant directly — no placeholders or sessions needed
  • Use tf.Variable for mutable scalars like learning rates that change during training
  • Keras learning rate schedules replace manual learning rate feeding in modern code
  • Always match the shape (() for scalar) and dtype between the placeholder and the fed value

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.