Open In App

Change view of Tensor in PyTorch

Last Updated : 18 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to change the shape of tensors using the PyTorch view function. We will also look at the multiple ways in which we can change the shape of the tensors. Also, we can use the view function to convert lower-dimensional matrices to higher dimensions.

What is the necessary condition to use the PyTorch view() function on a tensor?

As long as the number of elements in your original tensor is the same as the number of elements that must be there to form a matrix of the desired shape you can use the view function. For example, we have a tensor of shape, 1 x 16 we can convert it to 4 x 4, 2 x 2 x 4 but not into the shape of 2 x 4 x 4 because this tensor needs 32 elements but the input tensor has only 16.

How to apply the view() function on PyTorch tensors?

Example 1: Python program to create a tensor with 10 elements and view with 5 rows and 2 columns and vice versa.

Python3




# importing torch module
import torch
 
# create one dimensional tensor 10 elements
a = torch.FloatTensor([10, 20, 30, 40, 50, 1, 2, 3, 4, 5])
 
# view tensor in 5 rows and 2 columns
print(a.view(5, 2))
 
# view tensor in 2 rows and 5 columns
print(a.view(2, 5))


Output:

tensor([[10., 20.],
        [30., 40.],
        [50.,  1.],
        [ 2.,  3.],
        [ 4.,  5.]])
tensor([[10., 20., 30., 40., 50.],
        [ 1.,  2.,  3.,  4.,  5.]])

Example 2: In this example, we will see how can we use -1 for one of the dimensions of the desired tensors shape.

Python3




# importing torch module
import torch
 
# create one dimensional tensor 10 elements
a = torch.FloatTensor([10, 20, 30, 40, 50, 1, 2, 3, 4, 5])
 
# view tensor in 5 rows
print(a.view(5, -1))
 
# view tensor in 5 columns
print(a.view(-1, 5))


Output:

tensor([[10., 20.],
        [30., 40.],
        [50.,  1.],
        [ 2.,  3.],
        [ 4.,  5.]])
tensor([[10., 20., 30., 40., 50.],
        [ 1.,  2.,  3.,  4.,  5.]])

When the weight matrices or shapes of the input and output of deep neural networks become confusing then passing -1 for one of the dimensions saves us from all the mind-boggling shapes of the tensors.

How to use the view() function to obtain more than two-dimensional tensors?

In the below example, we will see how to get a three or more-dimensional vector using the view function. This function helps a lot while handling image data as they are three-dimensional having RGB channels. Also, when we make a batch of these images it increases to four dimensions.

Python3




# importing torch module
import torch
import numpy as np
 
# create one dimensional tensor having 18 elements
a = torch.FloatTensor(np.arange(18))
 
# view tensor in 2 x 3 x 3
print(a.view(2, 3, 3))


Output:

tensor([[[ 0.,  1.,  2.],
         [ 3.,  4.,  5.],
         [ 6.,  7.,  8.]],

        [[ 9., 10., 11.],
         [12., 13., 14.],
         [15., 16., 17.]]])


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads