PyTorch
view function
tensor manipulation
reshape tensors
PyTorch tutorial

What does view do in PyTorch?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

view in PyTorch reshapes a tensor without changing its underlying data layout, as long as that layout is compatible. It is fast because it usually returns a new tensor object that shares memory with the original tensor. The main caveat is contiguity: view requires tensor storage to match the requested shape interpretation.

What view Actually Does

view changes how dimensions are interpreted, not the number of elements.

python
1import torch
2
3x = torch.arange(12)
4y = x.view(3, 4)
5
6print(x.shape)  # torch.Size([12])
7print(y.shape)  # torch.Size([3, 4])

The product of target dimensions must equal original element count. Here, 12 elements are viewed as 3 x 4.

Memory Sharing Behavior

view usually shares storage with the source tensor.

python
1import torch
2
3x = torch.tensor([1, 2, 3, 4])
4y = x.view(2, 2)
5
6y[0, 0] = 99
7print(x)  # tensor([99, 2, 3, 4])

Because storage is shared, editing one view can affect the other. This is useful for performance but can surprise users expecting a copy.

Using -1 for Dimension Inference

You can use one -1 to let PyTorch infer that dimension.

python
1import torch
2
3x = torch.arange(24)
4y = x.view(2, -1, 3)
5
6print(y.shape)  # torch.Size([2, 4, 3])

Only one dimension can be -1. Multiple inferred dimensions would be ambiguous.

Contiguity Requirement

After operations such as transpose or permute, tensors are often non-contiguous. view then raises an error.

python
1import torch
2
3x = torch.arange(6).view(2, 3)
4z = x.t()  # transpose, often non-contiguous
5
6print(z.is_contiguous())
7
8try:
9    z.view(6)
10except RuntimeError as e:
11    print("view error:", e)

To use view, make tensor contiguous first.

python
flat = z.contiguous().view(6)
print(flat)

view Versus reshape

reshape is often more convenient because it can return a view when possible and fall back to a copy when needed.

python
1import torch
2
3x = torch.arange(6).view(2, 3)
4z = x.t()
5
6r = z.reshape(6)
7print(r)

Use view when you want strict behavior and know contiguity assumptions. Use reshape when you want a simpler API and can accept possible copying.

Typical Deep Learning Use Cases

view is common in model code for flattening features before linear layers.

python
1import torch
2import torch.nn as nn
3
4batch = torch.randn(8, 16, 4, 4)
5
6# Flatten all dimensions except batch
7flat = batch.view(batch.size(0), -1)
8
9layer = nn.Linear(16 * 4 * 4, 10)
10out = layer(flat)
11print(out.shape)  # torch.Size([8, 10])

This pattern is common in convolutional networks between convolution blocks and classifier heads.

Debugging Shape Errors Quickly

When a view call fails, check these in order:

  1. total element count compatibility
  2. tensor contiguity
  3. batch dimension assumptions

Useful debug statements:

python
print(tensor.shape)
print(tensor.numel())
print(tensor.is_contiguous())

Most reshape bugs become obvious after these three checks.

Autograd Considerations

view is autograd-friendly, and gradient tracking works through view operations. The key caution is in-place writes on tensors participating in gradient computation. In-place edits on shared storage can cause autograd errors or incorrect gradients if done carelessly.

For training code, prefer functional transformations and avoid unnecessary in-place mutations on viewed tensors unless you understand gradient implications.

Common Pitfalls

A common pitfall is assuming view always works after transpose or permute, then hitting contiguity errors. Another is forgetting that view shares memory and accidentally mutating source data. Teams also misuse -1 with incompatible target shapes and get runtime failures. Confusing view and reshape semantics can introduce hidden copies or unexpected strictness. Finally, flattening with incorrect batch assumptions can break model output dimensions.

Summary

  • view reshapes tensors without changing element count and usually without copying data.
  • It typically shares memory with the original tensor.
  • view requires compatible contiguous layout; use .contiguous() when needed.
  • reshape is more flexible when contiguity is uncertain.
  • Check shape, numel, and contiguity first when debugging tensor reshape errors.

Course illustration
Course illustration

All Rights Reserved.