Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute
menu
close

Need Help? Talk to an Expert

+91 8000107255
Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute

Need Help? Talk to an Expert

+91 8000107255

How to Use PyTorch view() Method

Home How to Use PyTorch view() Method
PyTorch torch.view() Function
  • Written by krunallathiya21
  • April 22, 2025
  • 0 Com
PyTorch

The view() method in PyTorch reshapes a contiguous tensor without copying data, offering an efficient way to adjust dimensions. It returns a new tensor underlying the same data as the input, but with a new shape.

It maintains the original data’s element count, and the tensor must be contiguous.

Basic working of torch.view() method

In the above figure, you can see that only the view has been changed from 1D to 2D. The data remains the same. It is just a view, so it shares the memory with the original tensor. This is the magic of the view().

If you are working with a non-contiguous tensor, you need to make it contiguous using the .contiguous() method and then use the .view() method.

Contiguous Tensor vs Non-Contiguous Tensor The above figure shows the difference between a contiguous and a non-contiguous tensor.

For inferring dimensions, use view(-1) (e.g., tensor.view(-1, 256)).

Function Signature

Tensor.view(*shape) → Tensor

Arguments

Name Value
shape (torch.Size or int) It is the desired shape of the output tensor.

Quick reshape

Let’s define a tensor with 1D dimension and view it in 2D without having to copy it.

import torch

tensor = torch.arange(4)

print(tensor.shape)

# Output: torch.Size([4])

# Reshape 1D to 2D
viewed_tensor = tensor.view(2, 2)

print(viewed_tensor.shape)

# Output: torch.Size([2, 2])

From the output, you can see that the .view(2, 2) method returns a view of the original tensor with the same data, but reinterpreted in a 2×2 layout.

The total number of elements must match: 2×2 = 4 (same as before).

The data in the normal tensor and the viewed tensor look like this:
import torch

tensor = torch.arange(4)

print(tensor)

# Output: tensor([0, 1, 2, 3])

# Reshape 1D to 2D
viewed_tensor = tensor.view(2, 2)

print(viewed_tensor)

# Output: tensor([[0, 1],
#                 [2, 3]])

Inferring Dimensions with -1

Use view(-1) when you need to collapse all the dimensions into a single one.

torch.view(-1)

For example, in the above section, we converted a 1D tensor to a 2D tensor, using view(-1), we can convert a 2D tensor to a 1D tensor without changing or copying the data.

import torch

tensor = torch.randn(2, 2)

print(tensor.shape)
# Output: torch.Size([2, 2])

inferred_tensor = tensor.view(-1)

print(inferred_tensor.shape)
# Output: torch.Size([4])

The total number of elements must match: 4 = 2 × 2 (same as before).

Flattening a Multi‑Dimensional Tensor

Let’s flatten [batch, C, H, W] → [batch, C*H*W].

import torch

batch = torch.randn(8, 3, 32, 32)

print(batch.shape)
# Output: torch.Size([8, 3, 32, 32])

flatten = batch.view(batch.size(0), -1)

print(flatten.shape)
# Output: torch.Size([8, 3072])

Adding or Removing Dimensions

You can use .unsqueeze() or .view() to add or remove dimensions from the input tensor.

import torch

tensor_1d = torch.tensor([1, 2, 3])

print(tensor_1d.shape)
# Output: torch.Size([3])

viewed_tensor = tensor_1d.view(1, 1, 3)

print(viewed_tensor.shape)
# Output: torch.Size([1, 1, 3])

# OR

unsqueezed_tensor = tensor_1d.unsqueeze(0).unsqueeze(0)

print(unsqueezed_tensor.shape)
# Output: torch.Size([1, 1, 3])

Handling non-contiguous tensors

To check if a tensor is contiguous, you can use the “.is_contiguous()” method. It returns True if it is contiguous and False otherwise.

As we know, we can’t pass a non-contiguous tensor to the .view() function. But for the deep understanding, let’s do it and see what happens:

import torch

non_contiguous_tensor = torch.randn(3, 4).t()

print(non_contiguous_tensor.is_contiguous())

# Output: False

viewed_tensor = non_contiguous_tensor.view()

print(viewed_tensor)

# Output: TypeError: view() received an invalid combination of arguments - got (), but expected one of:
#            * (torch.dtype dtype)
#            * (tuple of ints size)

The above output shows that we got the TypeError: view() received an invalid combination of arguments – got (), but expected one of. To fix this error, we need to convert an input tensor from non-contiguous to contiguous using the .contiguous() method and then pass that result into the .view() method.

import torch

non_contiguous_tensor = torch.randn(3, 4).t()

viewed_tensor = non_contiguous_tensor.contiguous().view(12)

print(viewed_tensor.shape)

# Output: torch.Size([12])
That’s all!
Post Views: 15
LEAVE A COMMENT Cancel reply
Please Enter Your Comments *

krunallathiya21

All Categories
  • JavaScript
  • Python
  • PyTorch
site logo

Address:  TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

sprintchasetechnologies@gmail.com

(+91) 8000107255.

ABOUT US
  • About
  • Team Members
  • Testimonials
  • Contact

Copyright by @SprintChase  All Rights Reserved

  • PRIVACY
  • TERMS & CONDITIONS