How to get the dimension of a Tensor

In this article, you will see how to get the dimension of a tensor in TensorFlow. First, import the TensorFlow framework, then create a tensor and use the tensor.ndim function to find the dimension of a tensor. I highly recommend you get this book, “Deep Learning with TensorFlow 2 and Keras” to learn Deep Learning.

# Import TensorFlow
import tensorflow as tf

# Create a tensor
a = tf.constant([[5,10],
                [30,70]])

# Display dimension of tensor
print("The dimension of a Tensor is", a.ndim)

Output:

The dimension of a Tensor is 2

Leave a Comment

Your email address will not be published.