TensorFlow
Session.run
Tensor.eval
Python
Machine Learning

In TensorFlow, what is the difference between Session.run and Tensor.eval?

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.x, both Session.run() and Tensor.eval() execute graph computations and return concrete values. The difference is mostly about scope and convenience: Session.run() is the general execution API, while Tensor.eval() is a shortcut for evaluating one tensor in an available session.

Session.run Is the General Mechanism

TensorFlow 1.x builds a computation graph first and executes it later inside a Session. Session.run() is the method that actually asks the runtime to compute values.

It can fetch many kinds of graph outputs:

  • one tensor
  • several tensors
  • operations with no returned value
  • nested structures of graph elements
python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5a = tf.constant(5.0)
6b = tf.constant(6.0)
7c = a * b
8
9with tf.compat.v1.Session() as sess:
10    value = sess.run(c)
11    print(value)  # 30.0

Session.run() is also the method you use when you need feed_dict, multiple fetches, or precise control over what part of the graph runs.

Tensor.eval Is a Convenience Shortcut

Tensor.eval() is a helper on a specific tensor object. Conceptually, it is just a shorthand for "run the session and fetch this tensor."

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5a = tf.constant(5.0)
6b = tf.constant(6.0)
7c = a * b
8
9with tf.compat.v1.Session() as sess:
10    print(c.eval(session=sess))  # 30.0

If a default session is active, you can even omit the explicit session= argument:

python
with tf.compat.v1.Session() as sess:
    with sess.as_default():
        print(c.eval())  # 30.0

This is nice in notebooks and quick experiments, but it is not fundamentally different execution behavior.

Practical Differences

The easiest way to think about the difference is:

  • use Session.run() when the session is the main thing you are controlling
  • use Tensor.eval() when you already have a specific tensor and want a shorter expression

Session.run() is more flexible:

python
1x = tf.compat.v1.placeholder(tf.float32)
2y = x * 2
3z = x + 3
4
5with tf.compat.v1.Session() as sess:
6    doubled, shifted = sess.run([y, z], feed_dict={x: 10.0})
7    print(doubled, shifted)  # 20.0 13.0

Doing the same work with eval() is possible for one tensor at a time, but it is less natural when you need multiple outputs or want to emphasize the overall graph execution step.

Default Sessions Matter

One subtle point is that Tensor.eval() depends on a session being available. If you call it outside a default session and do not pass session=..., it fails.

That is why eval() is common in interactive code with InteractiveSession or a with sess.as_default(): block. In larger programs, many developers prefer Session.run() because it makes the session dependency explicit.

TensorFlow 2.x Changes the Story

In TensorFlow 2.x, eager execution is the default, so most code no longer uses sessions at all. Tensors are typically evaluated immediately, and you inspect values directly with methods such as .numpy() when working in eager mode.

That means Session.run() and Tensor.eval() are mostly legacy concepts today, used when maintaining older TensorFlow 1.x code or code written through tf.compat.v1.

Common Pitfalls

Using Tensor.eval() without a default session or explicit session= argument causes runtime errors because the tensor has nowhere to execute.

Assuming eval() is faster than run() is incorrect. It is mainly a convenience wrapper, not a different execution engine.

Calling eval() repeatedly for many tensors can be less clear and less efficient than fetching them together in one Session.run() call.

Mixing TensorFlow 1.x session-based examples with TensorFlow 2.x eager code causes confusion. Check which execution model the project is using first.

Trying to use Session.run() or Tensor.eval() in modern eager-only examples is usually unnecessary unless you are deliberately using tf.compat.v1.

Summary

  • In TensorFlow 1.x, both APIs execute graph computations inside a session.
  • 'Session.run() is the general-purpose execution method.'
  • 'Tensor.eval() is a convenience shortcut for fetching one tensor's value.'
  • 'eval() needs a default session or an explicit session= argument.'
  • In TensorFlow 2.x, eager execution usually replaces both patterns.

Course illustration
Course illustration

All Rights Reserved.