Tensorflow How to feed a placeholder variable with a tensor?
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
This question belongs mostly to TensorFlow 1.x graph mode, because placeholders are not part of normal TensorFlow 2 eager programming. In TensorFlow 1.x, a placeholder is fed with concrete values through feed_dict at session run time. If what you already have is another tensor in the graph, the usual answer is not to feed it into the placeholder at all. The usual answer is to connect the graph directly.
What a Placeholder Is
A placeholder is a symbolic input node. It does not hold data by itself. It only says, "a value with this dtype and shape will be supplied when the graph runs."
At this point x is not data. It is a slot in the graph.
Feeding with a Concrete Value
The normal way to use a placeholder is to supply a NumPy array, Python list, or scalar through feed_dict:
That is the intended placeholder workflow.
If You Already Have Another Tensor, Connect It Instead
Suppose you have:
If a is already a tensor in the graph, you usually do not want:
- placeholder
- session run
- fetch tensor value
- feed placeholder again
You usually want the downstream op to consume a directly:
That is simpler and more idiomatic graph construction.
Feeding a Placeholder from a Tensor Value
If you absolutely must use the value produced by one tensor as the feed for a placeholder, you need two steps:
- evaluate the source tensor
- feed the resulting concrete value
This works, but it is usually a sign that the graph structure could be simplified.
TensorFlow 2 Changes the Question
In TensorFlow 2, eager execution is the default and placeholders are not the normal API. You usually write Python functions or tf.function and pass tensors directly as arguments.
So if you are starting new work, the better answer is often "do not use placeholders at all."
When Placeholders Still Make Sense
You may still see placeholders in:
- old TensorFlow 1.x training code
- legacy tutorials
- graph-export pipelines
- compatibility-mode code under
tf.compat.v1
In those environments, placeholders are valid, but the rule remains the same: feed them with concrete runtime data, not with another symbolic tensor unless you deliberately evaluate that tensor first.
Common Pitfalls
- Trying to feed a placeholder with another symbolic tensor when a direct graph connection would be cleaner.
- Forgetting that placeholders belong to TensorFlow 1.x style graph execution.
- Expecting
feed_dictto take an unevaluated tensor from the same graph as if it were normal Python data. - Disabling eager execution unnecessarily in new TensorFlow 2 code.
- Using placeholders when a function argument or ordinary tensor operation would be simpler.
Summary
- Placeholders are TensorFlow 1.x graph inputs fed through
feed_dict. - The intended feed values are concrete arrays or scalars, not raw symbolic tensors.
- If another tensor already exists in the graph, connect the graph directly instead of routing through a placeholder.
- If necessary, evaluate the source tensor first and feed its concrete value.
- In TensorFlow 2, the better modern answer is usually to avoid placeholders entirely.
Related reading
- Tensorflow How to find the size of a tf.data.Dataset API object
- Tensorflow How to get a tensor by name?
- Tensorflow How to get all variables from rnn_cell.BasicLSTM rnn_cell.MultiRNNCell
- TensorFlow How to handle void labeled data in image segmentation?
- Tensorflow How to ignore specific labels during semantic segmentation?
- Tensorflow How to index a tensor using 2D-index like in numpy
- TensorFlow how to log GPU memory VRAM utilization?
- TensorFlow, how to look inside ''blob'', the response in through CNN
.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.