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 |
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
