What does view do 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.
Introduction
In PyTorch, view reshapes a tensor without copying its underlying data when the memory layout allows it. That makes it fast and memory efficient, but it also means the returned tensor shares storage with the original one. The method is often described as "reshape," but the important detail is that it creates a view on the same data rather than a brand-new tensor.
Basic view usage
You can use view when the new shape has the same total number of elements as the old shape.
The tensor x has 12 elements, and 3 x 4 also needs 12 elements, so the reshape is valid. If the element count does not match, PyTorch raises an error.
You can also let PyTorch infer one dimension by using -1.
PyTorch computes the missing size automatically as long as the rest of the shape is consistent.
view shares data with the base tensor
The reason view is efficient is that it does not copy data. The view and the original tensor point at the same storage.
Changing y also changes x because they are different views of the same underlying memory. That behavior is useful, but it can also surprise you if you expect a fully independent tensor.
Contiguous memory matters
view only works when the tensor is laid out in memory in a way that matches the requested shape. After operations such as transpose or permute, tensors often become non-contiguous, and view will fail until you make the layout contiguous.
This is one of the biggest reasons beginners think view is unreliable. The method is strict because it is preserving the existing storage layout instead of silently copying data.
view versus reshape
reshape is often more forgiving. It tries to return a view when possible, but it may allocate a copy when necessary. By contrast, view requires that a real view be possible.
That difference matters when you care about:
- Whether data is copied
- Whether the result shares storage with the input
- Whether non-contiguous tensors should be accepted automatically
If you want explicit control and understand the memory layout, view is excellent. If you mainly want a tensor with a different shape and do not care whether a copy happens, reshape is often simpler.
Typical deep learning use case
One common use of view is flattening activations before feeding them to a linear layer.
The first dimension stays as the batch size, and the remaining dimensions are collapsed into one feature dimension. This is a classic pattern in convolutional models.
Common Pitfalls
The first pitfall is forgetting that view shares storage. If you mutate the view, the base tensor changes too.
Another issue is using view on a non-contiguous tensor after transpose, permute, or certain slicing operations. In that case, call .contiguous() first or use reshape if a copy is acceptable.
A third mistake is miscounting elements. view(3, 5) cannot work on a tensor with 12 elements because the requested shape needs 15.
Finally, do not assume view is the right answer for every reshape. When your goal is convenience rather than storage-level control, reshape can be the better fit.
Summary
- '
viewreshapes a tensor without copying data when the layout allows it.' - The returned tensor shares storage with the original tensor.
- The new shape must preserve the same total number of elements.
- '
viewrequires compatible contiguous memory layout.' - Use
reshapewhen you want a more flexible reshape operation.
Related reading
- what does x tf.placeholdertf.float32, None, 784 means?
- what exactly does 'tf.contrib.rnn.DropoutWrapper'' in tensorflow do? three citical questions
- What exactly does the forward function output in Pytorch?
- What exactly is a device in TensorFlow?
- What happens when we call cpu.data.numpy on a PyTorch tensor?
- What is tape-based autograd in Pytorch?
- What exactly is Keras's CategoricalCrossEntropy doing?
- What if the sample size is not divisible by batch_size in Keras model
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.