TF 2.0 print tensor values
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
TensorFlow 2.0 uses eager execution by default, which means tensors evaluate immediately and their values can be printed with standard Python print(). In TF 1.x, tensors were symbolic graph nodes and printing them showed metadata (shape, dtype) instead of values. TF 2.0 eliminates this friction — print(tensor) shows the actual values. For tensors inside @tf.function (graph mode), use tf.print() instead of Python print(). This article covers all printing approaches for debugging TensorFlow code.
Eager Execution (Default in TF 2.0)
print(tensor) shows the tensor wrapper with shape and dtype. .numpy() extracts the raw NumPy array for cleaner output.
Printing Inside @tf.function
Inside @tf.function, Python print() only executes during the tracing phase (first call). Use tf.print() for output on every call.
tf.print for Detailed Output
tf.print() is a TensorFlow operation that executes within the graph. It supports summarize to control how many elements are shown for large tensors.
Printing Large Tensors
For large tensors, print slices or summary statistics instead of the full array.
Debugging During Training
Comparing TF 1.x vs TF 2.0
Tensor Properties
Common Pitfalls
- Using print() inside @tf.function: Python
print()only runs during tracing (first call). On subsequent calls with the same input signature, it does not print. Usetf.print()for consistent output. - Calling .numpy() on GPU tensors in hot loops:
.numpy()copies data from GPU to CPU, which is slow. Avoid calling it inside training loops. Usetf.print()instead, which prints from the device directly. - Large tensor output truncation: Both
print()andtf.print()truncate large tensors by default. Usenp.set_printoptions(threshold=np.inf)ortf.print(tensor, summarize=-1)to see all values. - Printing inside tf.data pipelines:
print()inside.map()functions only executes during tracing. Usetf.print()ortf.py_functionwrapper for side effects inside data pipelines. - Expecting TF 1.x behavior: In TF 1.x,
print(tensor)showed only metadata. In TF 2.0, it shows actual values. Code migrated from TF 1.x may have unnecessarysess.run()calls that should be removed.
Summary
- TF 2.0 uses eager execution —
print(tensor)shows values directly - Use
.numpy()to extract raw NumPy arrays from tensors - Use
tf.print()inside@tf.functionfor output on every call - Use
tf.print(tensor, summarize=-1)to print all elements of large tensors - Avoid
.numpy()in training loops — it copies data from GPU to CPU - Print tensor properties with
.shape,.dtype, and.deviceattributes
Related reading
- TF Keras how to get expected input shape when loading a model?
- tf.data vs keras.utils.sequence performance
- tf.data.Dataset from tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory?
- tf.gradients sums over ys, does it?
- Tf 2.0 RuntimeError GradientTape.gradient can only be called once on non-persistent tapes
- TF 2.0 Where can I find the upgrade of tf.contrib.training?
- TF keras API with TF dataset problem - steps_per_epoch argument problem
- tf object detection api - extract feature vector for each detection bbox
.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.