tensorflow
placeholder
tensor
machine learning
tutorial

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.

Practice ML system design

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

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5x = tf.compat.v1.placeholder(tf.float32, shape=[None, 2], name="x")
6y = x * 2.0

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:

python
1import numpy as np
2import tensorflow as tf
3
4tf.compat.v1.disable_eager_execution()
5
6x = tf.compat.v1.placeholder(tf.float32, shape=[None, 2])
7y = x * 2.0
8
9with tf.compat.v1.Session() as sess:
10    result = sess.run(y, feed_dict={
11        x: np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)
12    })
13    print(result)

That is the intended placeholder workflow.

If You Already Have Another Tensor, Connect It Instead

Suppose you have:

python
a = tf.constant([[1.0, 2.0]])

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:

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5a = tf.constant([[1.0, 2.0]], dtype=tf.float32)
6y = a * 2.0
7
8with tf.compat.v1.Session() as sess:
9    print(sess.run(y))

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:

  1. evaluate the source tensor
  2. feed the resulting concrete value
python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5source = tf.constant([[1.0, 2.0]], dtype=tf.float32)
6x = tf.compat.v1.placeholder(tf.float32, shape=[None, 2])
7z = x + 5.0
8
9with tf.compat.v1.Session() as sess:
10    source_value = sess.run(source)
11    result = sess.run(z, feed_dict={x: source_value})
12    print(result)

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.

python
1import tensorflow as tf
2
3@tf.function
4def double_tensor(x):
5    return x * 2.0
6
7value = tf.constant([[1.0, 2.0]], dtype=tf.float32)
8print(double_tensor(value))

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_dict to 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free 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.