Tensorflow 2 – How to Print only the Value of a Tensor

Last updated on September 13, 2021 The Plumber Loading... Post a comment

This concise post shows you 2 ways to print only the value of a tensor in Tensorflow 2.

Using the tf.print() function

Example:

import tensorflow as tf

x = tf.constant([
    [1, 2, 3],
    [4, 5, 6]                           
])

tf.print(x)

Output:

[[1 2 3]
 [4 5 6]]

Using the print() function will result in the following:

tf.Tensor(
[[1 2 3]
 [4 5 6]], shape=(2, 3), dtype=int32)

Reference: https://www.tensorflow.org/api_docs/python/tf/print

Using the numpy() method

Example:

import tensorflow as tf

a = tf.constant([
                 [1, 1, 2, 3],
                 [5, 8, 13, 21]
])

print(a.numpy())

Output:

[[ 1  1  2  3]
 [ 5  8 13 21]]

References: https://www.tensorflow.org/guide/tf_numpy

Final Words

You’ve seen 2 examples of printing the value of a tensor object in Tensorflow 2. If you’d like to explore more basic stuff in machine learning and Python, take a look at the following posts:

You can also check out our Machine Learning category page or Python category page for more tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You May Also Like