Skip to main content
TopMiniSite

Back to all posts

How to Print Tensor Shape In Tensorflow?

Published on
5 min read
How to Print Tensor Shape In Tensorflow? image

Best TensorFlow Books to Buy in February 2026

+
ONE MORE?

To print the shape of a tensor in TensorFlow, you can use the TensorFlow session to run the tensor and then use the shape attribute to access the shape of the tensor. Here is an example code snippet that demonstrates how to print the shape of a tensor in TensorFlow:

import tensorflow as tf

Create a sample tensor

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

Start a TensorFlow session

with tf.Session() as sess: # Run the tensor and get the shape shape = sess.run(tf.shape(tensor))

Print the shape of the tensor

print(shape)

In this code snippet, we first create a sample tensor using the tf.constant function. Then we start a TensorFlow session using the tf.Session() context manager. Inside the session, we run the tensor using sess.run(tf.shape(tensor)) to get the shape of the tensor. Finally, we print the shape of the tensor using the print function.

How to view tensor shape in tensorflow?

In TensorFlow, you can view the shape of a tensor using the .shape attribute. Here is an example of how to view the shape of a tensor in TensorFlow:

import tensorflow as tf

Create a tensor

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

View the shape of the tensor

print(tensor.shape)

This will output the shape of the tensor, which in this case is (2, 3) indicating that the tensor has 2 rows and 3 columns.

What is the technique to retrieve tensor shape in tensorflow?

In TensorFlow, you can retrieve the shape of a tensor using the tf.shape() function.

Here is an example code snippet that demonstrates how to retrieve the shape of a tensor in TensorFlow:

import tensorflow as tf

Create a tensor

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

Get the shape of the tensor

tensor_shape = tf.shape(tensor)

Print the shape of the tensor

print(tensor_shape)

This will output the shape of the tensor as a TensorFlow tensor object. If you want to retrieve the shape as a numpy array, you can do so by evaluating the tensor shape using a TensorFlow session:

import tensorflow as tf

Create a tensor

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

Get the shape of the tensor

tensor_shape = tf.shape(tensor)

Start a TensorFlow session

with tf.Session() as sess: # Evaluate the tensor shape shape = sess.run(tensor_shape)

Print the shape as a numpy array

print(shape)

This will output the shape of the tensor as a numpy array.

What is the function to print tensor shape in tensorflow?

In TensorFlow, you can print the shape of a tensor using the shape attribute. Here's an example:

import tensorflow as tf

Create a tensor

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

Print the shape of the tensor

print(tensor.shape)

This will output the shape of the tensor, which in this case is (2, 3) indicating that it is a 2-dimensional tensor with 2 rows and 3 columns.

How to retrieve tensor shape in tensorflow?

In TensorFlow, you can retrieve the shape of a tensor using the tf.shape() function. Here is an example code snippet demonstrating how to do this:

import tensorflow as tf

Create a tensor

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

Retrieve the shape of the tensor

tensor_shape = tf.shape(tensor)

Create a TensorFlow session

with tf.Session() as sess: # Run the session to get the shape shape = sess.run(tensor_shape) print(shape)

In this code snippet, we first create a tensor using tf.constant(), then use tf.shape() to retrieve the shape of the tensor. Finally, we run a TensorFlow session to compute and print out the shape of the tensor.

What is the technique to get tensor size in tensorflow?

To get the size of a tensor in TensorFlow, you can use the shape attribute of the tensor object. Here is an example:

import tensorflow as tf

Create a tensor

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

Get the shape of the tensor

tensor_size = tf.size(tensor)

Create a TensorFlow session

with tf.Session() as sess: print(sess.run(tensor_size))

This will output the total number of elements in the tensor, which in this case is 6.

How to determine tensor shape in tensorflow?

In TensorFlow, you can determine the shape of a tensor by using the tf.shape() function. This function returns the shape of the input tensor as a 1-D integer tensor.

Here is an example code snippet that demonstrates how to determine the shape of a tensor in TensorFlow:

import tensorflow as tf

Define a tensor

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

Get the shape of the tensor

shape = tf.shape(tensor)

Create a TensorFlow session

with tf.Session() as sess: print("Shape of the tensor: ", sess.run(shape))

When you run this code snippet, it will print out the shape of the tensor [2 3], indicating that the tensor has 2 rows and 3 columns.