Python / PyTorch Fundamentals Interview Questions
What is the difference between.view(),.reshape(), and.contiguous() in PyTorch, and why does it matter?
These three methods deal with how a tensor's underlying memory is interpreted as a different shape. Understanding the difference prevents a class of confusing runtime errors related to tensor memory layout.
import torch x = torch.arange(12).reshape(3, 4) # shape (3, 4), contiguous memory # ââ .view(): ALWAYS returns a view (no copy), but requires contiguous memory y = x.view(4, 3) # works â x is contiguous print(y.shape) # (4, 3) # ââ Transpose breaks contiguity â the data is NOT rearranged in memory, # only the strides describing how to read it change xt = x.t() # transpose â x.t() is a VIEW with different strides print(xt.is_contiguous()) # False! # This FAILS â view() cannot reinterpret non-contiguous memory try: xt.view(3, 4) except RuntimeError as e: print(f"Error: {e}") # RuntimeError: view size is not compatible with input tensor's size and stride # ââ .reshape(): tries view() first; falls back to copying if needed z = xt.reshape(3, 4) # WORKS â automatically copies if necessary print(z.shape) # (3, 4) # ââ .contiguous(): explicitly forces a contiguous copy in memory xt_contig = xt.contiguous() print(xt_contig.is_contiguous()) # True xt_contig.view(3, 4) # now works, since it is contiguous # Strides explain WHY this happens print(x.stride()) # (4, 1) â contiguous: move 1 step = 1 memory address print(xt.stride()) # (1, 4) â transposed: strides reflect the swap, no copy made
| Method | Copies data? | Requires contiguous input? | Safety |
|---|---|---|---|
| .view() | Never — always a view | Yes — raises RuntimeError otherwise | Fails loudly on non-contiguous tensors |
| .reshape() | Only if necessary | No — handles either case automatically | Safer general-purpose choice |
| .contiguous() | Yes, if not already contiguous | N/A — this is what fixes it | Use before .view() on a transposed/permuted tensor |
More Related questions...