How to use resize() method In Python
This method is also used to resize tensors in PyTorch and the below syntax helps us to resize the tensor.
Syntax: tensor.resize_(no_of_tensors, no_of_rows, no_of_columns)
Parameters:
- no_of_tensors: represents the total number of tensors to be generated
- no_of_rows: represents the total number of rows in the new resized tensor
- no_of_columns: represents the total number of columns in the new resized tensor
Example 5:
The following program is to understand how to resize the tensor using resize() method.
Python
# import torch module import torch # Define an 1D tensor tens = torch.Tensor([ 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 ]) # display tensor print ( "\n Original 2D Tensor: \n" , tens) # resize the tensor to 4 tensors. # each tensor with 4 rows and 5 columns tens_1 = tens.resize_( 4 , 4 , 5 ) print ( "\n After resize tensor: \n" , tens_1) |
Output:
How to resize a tensor in PyTorch?
In this article, we will discuss how to resize a Tensor in Pytorch. Resize allows us to change the size of the tensor. we have multiple methods to resize a tensor in PyTorch. let’s discuss the available methods.