PyTorch
reshape
view
tensor operations
machine learning

What's the difference between reshape and view in PyTorch?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In PyTorch, both view() and reshape() change the apparent shape of a tensor, but they do not make the same promise. view() requires a compatible contiguous memory layout, while reshape() is more flexible because it returns a view when possible and makes a copy when necessary.

What view() Does

view() reinterprets the existing storage with a new shape. It does not rearrange the tensor's underlying data.

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])

This is cheap because the data buffer is reused. But view() only works when the tensor's memory layout is compatible with the requested shape.

What reshape() Does

reshape() tries to give you the requested shape with less manual hassle:

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

If the tensor can be reshaped as a view, reshape() usually returns a view-like result. If not, PyTorch may create a copy behind the scenes.

That is the key difference:

  • 'view() is strict'
  • 'reshape() is flexible'

The Contiguity Problem

Many tensor operations such as transpose() or permute() change how a tensor is interpreted without making it contiguous in memory. That is where view() often fails.

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

Now try view():

python
t.view(12)

That often raises an error because the transposed tensor is not contiguous in the way view() expects.

But reshape() can still work:

python
flat = t.reshape(12)
print(flat.shape)  # torch.Size([12])

If needed, PyTorch allocates a contiguous copy so the reshape succeeds.

contiguous() with view()

If you want to keep using view() after a layout-changing operation, make the tensor contiguous first:

python
flat = t.contiguous().view(12)

This pattern is explicit and useful when you want to be very clear about when a copy happens.

Why the Difference Matters

The distinction matters for:

  • performance
  • memory usage
  • debugging unexpected copies

If you expect a cheap metadata-only change and instead reshape() must allocate new storage, that can affect speed and memory in large models or data pipelines.

At the same time, reshape() is often more ergonomic because it works in more situations without extra boilerplate.

Practical Rule of Thumb

Use reshape() when you want the shape change and do not want to think about contiguity every time:

python
batch = images.reshape(images.size(0), -1)

Use view() when:

  • you know the tensor is contiguous
  • you want stricter behavior
  • you want the code to fail rather than silently materialize a copy

That makes view() attractive in performance-sensitive code where accidental copies would be undesirable.

Shared Storage Behavior

When a true view is returned, changes can reflect in the same underlying storage:

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

With reshape(), this shared-storage behavior may or may not hold depending on whether a copy was needed. That is another reason not to assume reshape() always behaves exactly like view().

Common Pitfalls

The biggest mistake is assuming reshape() never copies. It often returns a view, but it is allowed to allocate new storage when the current layout cannot support the requested shape directly.

Another issue is using view() after transpose(), permute(), or slicing operations that produce non-contiguous layouts. If you see a contiguity error, either call contiguous() first or switch to reshape().

Finally, do not treat view() and reshape() as interchangeable in code that depends on aliasing or performance characteristics. They may produce the same shape but not the same memory behavior.

Summary

  • 'view() requires a compatible contiguous layout and does not copy data.'
  • 'reshape() is more flexible and may return a copy when needed.'
  • After layout-changing ops such as transpose(), view() often fails unless you call contiguous().
  • Use reshape() for convenience and view() for stricter control.
  • Shape equality does not guarantee identical memory behavior.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.