Change view of Tensor in PyTorch
Last Updated :
18 Oct, 2022
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.]]])
Similar Reads
Creating a Tensor in Pytorch
All the deep learning is computations on tensors, which are generalizations of a matrix that can be indexed in more than 2 dimensions. Tensors can be created from Python lists with the torch.tensor() function. The tensor() Method: To create tensors with Pytorch we can simply use the tensor() method:
6 min read
How to Get the Value of a Tensor in PyTorch
When working with PyTorch, a powerful and flexible deep learning framework, you often need to access and manipulate the values stored within tensors. Tensors are the core data structures in PyTorch, representing multi-dimensional arrays that can store various types of data, including scalars, vector
5 min read
Tensors in Pytorch
A Pytorch Tensor is basically the same as a NumPy array. This means it does not know anything about deep learning or computational graphs or gradients and is just a generic n-dimensional array to be used for arbitrary numeric computation. However, the biggest difference between a NumPy array and a P
6 min read
Way to Copy a Tensor in PyTorch
In deep learning, PyTorch has become a popular framework for building and training neural networks. At the heart of PyTorch is the tensorâa multi-dimensional array that serves as the fundamental building block for all operations in the framework. There are many scenarios where you might need to copy
5 min read
Convert PyTorch Tensor to Python List
PyTorch, a widely-used open-source machine learning library, is known for its flexibility and ease of use in building deep learning models. A fundamental component of PyTorch is the tensor, a multi-dimensional array that serves as the primary data structure for model training and inference. However,
3 min read
PyTorch Tensor vs NumPy Array
PyTorch and NumPy can help you create and manipulate multidimensional arrays. This article covers a detailed explanation of how the tensors differ from the NumPy arrays. What is a PyTorch Tensor?PyTorch tensors are the data structures that allow us to handle multi-dimensional arrays and perform math
8 min read
One-Dimensional Tensor in Pytorch
In this article, we are going to discuss a one-dimensional tensor in Python. We will look into the following concepts: Creation of One-Dimensional TensorsAccessing Elements of TensorSize of TensorData Types of Elements of TensorsView of TensorFloating Point TensorIntroduction The Pytorch is used to
5 min read
Two-Dimensional Tensors in Pytorch
PyTorch is a python library developed by Facebook to run and train machine learning and deep learning models. In PyTorch everything is based on tensor operations. Two-dimensional tensors are nothing but matrices or vectors of two-dimension with specific datatype, of n rows and n columns. Representat
3 min read
Difference between Tensor and Variable in Pytorch
In this article, we are going to see the difference between a Tensor and a variable in Pytorch. Pytorch is an open-source Machine learning library used for computer vision, Natural language processing, and deep neural network processing. It is a torch-based library. It contains a fundamental set of
3 min read
PyTorch Lightning with TensorBoard
Pytorch-Lightning is a popular deep learning framework. It basically works with PyTorch models to simplify the training and testing of the models. This library is useful for distributed training as one can train the model seamlessly without much complex codes. Now to get the metrics in an user inter
5 min read