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.
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
Passing Different Scalar Types
Dynamic Training Parameters
The most common use case — varying hyperparameters during training:
TensorFlow 2.x: No Placeholders Needed
In TF2 with eager execution, pass values directly as function arguments:
Migration: TF1 feed_dict to TF2
Using tf.Variable for Mutable Scalars
For values that change during training (like learning rate schedules):
Keras Learning Rate Schedules
The modern replacement for feeding learning rate scalars:
Common Pitfalls
- Shape mismatch: Passing a list
[0.01]instead of a scalar0.01to ashape=()placeholder raisesValueError: Cannot feed value of shape (1,) for Tensor with shape (). Pass a plain Python number, not a list. - Wrong dtype: Passing a Python
intto afloat32placeholder works (auto-cast), but passing astringto afloat32placeholder raisesTypeError. Match the Python type to the placeholder dtype. - Using feed_dict in TF2:
tf.compat.v1.placeholderandfeed_dictwork in TF2 withdisable_eager_execution(), but they are deprecated. Migrate to@tf.functionwith direct arguments for new code. - Forgetting to feed all placeholders: If the computation graph uses placeholders A and B but
feed_dictonly provides A, TensorFlow raisesInvalidArgumentError: You must feed a value for placeholder tensor 'B'. - Performance with feed_dict: In TF1,
feed_dictcopies data from Python to the TF runtime on everysess.run(). For training loops with fixed hyperparameters, usetf.Variableinstead to avoid the copy overhead.
Summary
- In TF1, create
tf.placeholder(dtype, shape=())for scalar values and pass them viafeed_dictinsess.run() - In TF2, pass Python values or
tf.constantdirectly — no placeholders or sessions needed - Use
tf.Variablefor 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
- How do I pass a scalar via a TensorFlow feed dictionary
- How do I plot a Keras/Tensorflow subclassing API model?
- How do I print the model summary in PyTorch?
- How do I print the model summary in PyTorch?
- How do I pass an OpenCV Mat into a C Tensorflow graph?
- How do I print inside the loss function during training in Keras?
- How do I plot a classification graph of a SVM in R
- How do I predict new data's cluster after clustering training data?

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 courseTrack 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.