How do I feed Tensorflow placeholders with numpy arrays?
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-style graph code, NumPy arrays are passed into placeholders through the feed_dict argument of Session.run(). The essential rule is simple: the NumPy array must match the placeholder's expected dtype and shape, including batch dimensions, and the whole setup belongs to TensorFlow 1 compatibility mode rather than normal TensorFlow 2 eager code.
Basic Placeholder Feeding
In TensorFlow 1 style, a placeholder is a symbolic input node:
The key in the feed dictionary is the placeholder tensor itself, and the value is the NumPy array you want TensorFlow to use for that run.
Match Shape and Dtype Carefully
The placeholder in the previous example has shape [None, 3], so the fed array must have three columns. The leading dimension is flexible because it represents batch size.
A quick debugging pattern is:
If the placeholder expects tf.float32 and your NumPy array is float64, TensorFlow may raise a type error or force an implicit conversion that you did not intend.
Feeding Multiple Placeholders
Most training steps feed more than one placeholder at once, typically features and labels:
Every placeholder used by the requested tensor must be satisfied by the feed dictionary or by other graph operations.
Feeding by Tensor Name
Most code should use the placeholder object itself as the feed_dict key. In older graph-loading workflows, you may also see code that resolves a placeholder by name first and then feeds it.
Single Samples Still Need Batch Shape
A frequent mistake is feeding one example without its batch dimension.
Wrong shape for [None, 2]:
Correct batched shape:
Even one example is still a batch of size one when the placeholder is batch-oriented.
Feeding During Training
A more realistic pattern is to feed placeholders into a training step:
This is the standard TensorFlow 1 workflow that older tutorials refer to when they say "feed placeholders with NumPy."
TensorFlow 2 Equivalent
Modern TensorFlow usually avoids placeholders entirely. Instead, you pass tensors directly to functions or Keras models:
For new code, this is usually the better approach. Placeholder feeding matters mainly when maintaining TensorFlow 1 graphs or compatibility code.
Common Pitfalls
The most common mistake is dtype mismatch, especially feeding NumPy's default float64 arrays into tf.float32 placeholders. Another common issue is forgetting the batch dimension for single examples, which produces shape errors even when the underlying data values are correct.
A third problem is mixing TensorFlow 1 graph mode and TensorFlow 2 eager assumptions in the same script. If you are using placeholders, make the execution mode explicit and stay consistent throughout the file.
Summary
- Feed NumPy arrays into TensorFlow 1 placeholders with
feed_dict. - Use the placeholder tensor itself as the dictionary key.
- Match both dtype and shape, including batch dimensions.
- Feed multiple placeholders together in the same
Session.run()call when needed. - For new projects, prefer TensorFlow 2 tensor-argument or Keras input patterns instead of placeholders.

