Issue feeding a list into feed_dict in TensorFlow
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, feed_dict maps placeholder tensors to values for session.run(). The most common issue is a shape mismatch — feeding a Python list with the wrong dimensions for the placeholder's expected shape. The fix is to reshape the data to match the placeholder's dimensions exactly, typically converting lists to NumPy arrays with the correct shape. In TensorFlow 2.x, feed_dict is replaced by direct function calls with eager execution.
The Problem
The placeholder expects a 2D tensor (batch dimension + feature dimension), but a flat list is 1D.
Fix: Correct the Shape
Feeding Multiple Placeholders
Common Shape Mismatches
Feeding Scalar Values
TensorFlow 2.x: No More feed_dict
TensorFlow 2.x uses eager execution by default — no placeholders or feed_dict:
Migrating from feed_dict to TF2
Common Pitfalls
- Shape
(n,)vs(n, 1)vs(1, n): A 1D array(n,)is not the same as a column vector(n, 1)or row vector(1, n). Placeholders withshape=[None, 1]require 2D input. Use.reshape(-1, 1)to add the second dimension. - Feeding Python lists instead of NumPy arrays: Python lists work but are slower because TensorFlow must convert them internally. For large datasets, always convert to NumPy arrays before feeding.
- Forgetting the batch dimension: Most placeholders include a batch dimension (
None). A single sample must still have the batch dimension: shape(1, 784)not(784,). Usenp.expand_dims(data, axis=0). - Type mismatch: If the placeholder expects
tf.float32but you feedintdata, TensorFlow may raise an error or silently cast. Ensure types match withdata.astype(np.float32). - Using
feed_dictin TF2: TensorFlow 2.x does not use sessions or placeholders. If you are starting a new project, use Keras models and eager execution instead of the TF1feed_dictpattern.
Summary
feed_dicterrors are almost always shape mismatches — check placeholder shape vs data shape- Add the batch dimension to single samples:
data.reshape(1, -1)ornp.expand_dims(data, 0) - Use NumPy arrays instead of Python lists for better performance
- Match data types:
np.float32fortf.float32placeholders - In TensorFlow 2.x, use eager execution and Keras models instead of
feed_dict - Use
tensor.shapeanddata.shapeto debug dimension mismatches
Related reading
- Issue installing Tensorflow -- not a CUDA/CuDNN issue
- Issue of batch sizes when using custom loss functions in Keras
- Issue with add method in tensorflow AttributeError module 'tensorflow.python.framework.ops' has no attribute '_TensorLike
- Issue with BERT Preprocessor model in TF2 and python
- Issue in training hidden markov model and usage for classification
- Issue NaN with Adam solver
- Issue in connecting kafka from outside
- Issue in establishing connection with Rabbit MQ
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free 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.