TensorFlow
Tensor object
Python
Machine Learning
Debugging

How to print the value of a Tensor object in TensorFlow?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

How you print a TensorFlow tensor depends on the execution mode. In TensorFlow 2 eager mode, you usually inspect the value directly or call .numpy(). In graph-style code or inside traced functions, the correct tool is often tf.print, not plain Python print.

In TensorFlow 2 Eager Mode, Print the Tensor Directly

TensorFlow 2 uses eager execution by default, which means tensors already hold concrete values.

python
1import tensorflow as tf
2
3t = tf.constant([[1, 2], [3, 4]])
4print(t)
5print(t.numpy())

print(t) shows a TensorFlow representation that includes shape and dtype. t.numpy() gives you the raw NumPy-style value. Both are useful:

  • 'print(t) for debugging TensorFlow context'
  • 'print(t.numpy()) for plain numerical inspection'

If the tensor lives on a GPU, TensorFlow still handles the transfer for you when you call .numpy() in eager mode.

Use tf.print Inside tf.function

Inside a function decorated with tf.function, normal Python print runs during tracing, not necessarily every time the graph executes. For runtime values, use tf.print.

python
1import tensorflow as tf
2
3@tf.function
4def compute(x):
5    y = x * 2
6    tf.print("runtime value:", y)
7    return y
8
9compute(tf.constant([1, 2, 3]))

This is the right choice when you need to inspect intermediate values in traced training steps, custom layers, or graph-compiled utility functions.

Using plain print in that context often confuses people because it may execute only once during tracing or display symbolic information instead of concrete values.

TensorFlow 1 Style Code Needs a Session

If you are reading legacy TensorFlow 1 code, tensors are symbolic until evaluated in a session.

python
1import tensorflow.compat.v1 as tf
2
3tf.disable_v2_behavior()
4
5t = tf.constant([[1, 2], [3, 4]])
6
7with tf.Session() as sess:
8    value = sess.run(t)
9    print(value)

In that model, trying to print the tensor object itself does not give you the actual computed array. You must run it.

Printing During Model Training

When debugging training loops, it helps to print targeted information instead of entire tensors. Large tensors flood logs quickly.

python
1import tensorflow as tf
2
3weights = tf.random.normal((2, 3))
4print("shape:", weights.shape)
5print("dtype:", weights.dtype)
6print("first row:", weights[0].numpy())

For large models, you often want:

  • shape
  • dtype
  • a slice or summary statistic
  • min, max, or mean
python
print("mean:", tf.reduce_mean(weights).numpy())
print("max:", tf.reduce_max(weights).numpy())

This gives you useful debugging information without drowning in output.

tf.print Is Better for Graph-Compatible Debugging

tf.print is not just a workaround for tf.function. It is also the correct graph-friendly way to emit debug information from TensorFlow ops.

python
1import tensorflow as tf
2
3x = tf.constant([1.0, 2.0, 3.0])
4y = tf.math.square(x)
5tf.print("squared:", y)

It understands tensors natively and works cleanly in compiled execution paths. If you are debugging TensorFlow code that mixes eager and traced execution, prefer tf.print whenever you are unsure which mode is active.

Be Careful with Large or Frequent Prints

Tensor printing is useful for debugging, but it is expensive if done on every training step or on very large tensors. Printing entire model activations inside a tight training loop can dominate runtime and make logs unusable.

A more disciplined pattern is printing on intervals or printing summaries only.

python
1for step in range(100):
2    value = tf.constant(step * 2)
3    if step % 20 == 0:
4        tf.print("step", step, "value", value)

This keeps debugging useful instead of destructive.

Common Pitfalls

  • Using .numpy() in code paths that are actually graph-traced and expecting it to always behave like eager mode.
  • Using Python print inside tf.function and assuming it reflects runtime tensor values.
  • Printing symbolic TensorFlow 1 tensors without evaluating them in a session.
  • Dumping huge tensors to logs when shape or summary statistics would be enough.
  • Confusing the tensor object's representation with the actual numerical value it contains.

Summary

  • In TensorFlow 2 eager mode, print(tensor) and tensor.numpy() are the usual options.
  • Inside tf.function, use tf.print for runtime values.
  • In TensorFlow 1 style code, evaluate tensors with sess.run before printing the result.
  • Prefer printing shapes and summaries over full large tensors.
  • Match the printing method to the execution mode you are actually using.

Course illustration
Course illustration

All Rights Reserved.