9951 explained code solutions for 126 technologies


python-tensorflowHow do I reshape a tensor in Python using TensorFlow?


Reshaping a tensor in Python using TensorFlow is a relatively simple process. To reshape a tensor, you can use the tf.reshape() function. This function takes a tensor as its first argument and a shape as its second argument. The shape argument is a tuple that specifies the desired shape of the output tensor.

For example, the following code reshapes a 2x3 tensor into a 3x2 tensor:

import tensorflow as tf

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

print(reshaped_tensor)

Output example

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

The code consists of the following parts:

  1. Importing the TensorFlow library as tf
  2. Creating a 2x3 tensor
  3. Reshaping the tensor using the tf.reshape() function
  4. Printing the reshaped tensor

Helpful links

Edit this code on GitHub