How to resize a tensor in PyTorch?

To resize a PyTorch tensor, we use the .view() method. We can increase or decrease the dimension of the tensor, but we have to make sure that the total number of elements in a tensor must match before and after the resize.

Steps

  • Import the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.

  • Create a PyTorch tensor and print it.

  • Resize the above-created tensor using .view() and assign the value to a variable. .view() does not resize the original tensor; it only gives a view with the new size, as its name suggests.

  • Finally, print the tensor after the resize.

Using .view() with Explicit Dimensions

The .view() method allows you to reshape a tensor by specifying new dimensions. The total number of elements must remain the same −

# Python program to resize a tensor in PyTorch
import torch

# Create a tensor
T = torch.Tensor([1, 2, 3, 4, 5, 6])
print("Original tensor:")
print(T)

# Resize T to 2x3
x = T.view(2, 3)
print("Tensor after resize:")
print(x)
Original tensor:
tensor([1., 2., 3., 4., 5., 6.])
Tensor after resize:
tensor([[1., 2., 3.],
        [4., 5., 6.]])

Using .view() with Automatic Dimension Calculation

You can use -1 as a placeholder for one dimension, and PyTorch will automatically calculate it based on the total number of elements −

import torch

T = torch.Tensor([1, 2, 3, 4, 5, 6])

# Let PyTorch calculate the first dimension (-1, 3)
x1 = T.view(-1, 3)
print("Using view(-1, 3):")
print(x1)

# Let PyTorch calculate the second dimension (2, -1)
x2 = T.view(2, -1)
print("Using view(2, -1):")
print(x2)
Using view(-1, 3):
tensor([[1., 2., 3.],
        [4., 5., 6.]])
Using view(2, -1):
tensor([[1., 2., 3.],
        [4., 5., 6.]])

Resizing Multi-dimensional Tensors

The same principles apply when working with multi-dimensional tensors. Here's how to reshape a 4x3 tensor into different dimensions −

import torch

# Create a tensor shape 4x3
T = torch.Tensor([[1, 2, 3], [2, 1, 3], [2, 3, 5], [5, 6, 4]])
print("Original 4x3 tensor:")
print(T)

# Resize T to 3x4
x1 = T.view(3, 4)
print("Resized to 3x4:")
print(x1)

# Resize T to 2x6
x2 = T.view(2, 6)
print("Resized to 2x6:")
print(x2)

# Resize to 1D tensor
x3 = T.view(-1)
print("Flattened to 1D:")
print(x3)
Original 4x3 tensor:
tensor([[1., 2., 3.],
        [2., 1., 3.],
        [2., 3., 5.],
        [5., 6., 4.]])
Resized to 3x4:
tensor([[1., 2., 3., 2.],
        [1., 3., 2., 3.],
        [5., 5., 6., 4.]])
Resized to 2x6:
tensor([[1., 2., 3., 2., 1., 3.],
        [2., 3., 5., 5., 6., 4.]])
Flattened to 1D:
tensor([1., 2., 3., 2., 1., 3., 2., 3., 5., 5., 6., 4.])

Key Points

  • .view() returns a new view of the tensor with different dimensions

  • The total number of elements must remain constant

  • Use -1 to let PyTorch automatically calculate one dimension

  • The original tensor remains unchanged

Conclusion

The .view() method is essential for reshaping tensors in PyTorch. Remember that the total number of elements must stay the same, and you can use -1 for automatic dimension calculation. This method creates a new view without modifying the original tensor.

Updated on: 2026-03-26T18:40:12+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements