Changing the Shape of a Tensor

In this lesson, we will look at changing the shape of a tensor.

Reshaping a tensor

If you already have one tensor, but the shape is not what you expect, shape() is the function you need. It allows us to create a tensor with the same data and number of original tensor elements.

shape() requires two parameters. The first parameter input is the tensor to be reshaped. The second parameter is the shape which is a tuple of int, the new shape.

Notice: You can use -1 as an argument for one dimension, which means you don’t specify the size for this dimension. It would be inferred from the remaining dimensions.

Get hands-on with 1200+ tech skills courses.