Python / PyTorch Fundamentals Interview Questions
How do torch.no_grad() and tensor.detach() differ, and when do you use each?
Both torch.no_grad() and .detach() stop gradient tracking, but they work at different levels and serve different purposes.
import torch
model_param = torch.tensor(2.0, requires_grad=True)
# ── torch.no_grad(): context manager — disables ALL grad tracking
# Use for inference and validation loops
with torch.no_grad():
out = model_param * 3 # no graph built
print(out.requires_grad) # False
print(out.grad_fn) # None
# Faster + less memory — standard pattern for eval
# ── .detach(): detaches a SPECIFIC tensor from the graph
# The tensor still knows about grad, but is cut off from history
a = model_param * 4
print(a.requires_grad) # True (still tracking)
b = a.detach() # b shares data with a
print(b.requires_grad) # False (disconnected)
print(b.data_ptr() == a.data_ptr()) # True — SAME memory!
# Common use case: compute a "stop gradient" target
# in actor-critic / target networks
target = a.detach() # stop gradient through target
loss = (a - target) ** 2 # gradient only flows through a, not target
# ── @torch.no_grad() decorator variant
@torch.no_grad()
def predict(x):
return model_param * x # no grad even without with block
# Validation loop pattern
def validate(model, loader):
model.eval() # turns off dropout, batchnorm train mode
with torch.no_grad(): # no gradient computation
for x, y in loader:
pred = model(x)
# compute metrics...| Feature | torch.no_grad() | tensor.detach() |
|---|---|---|
| Scope | All ops within the context block | One specific tensor |
| Memory saved | Yes — no graph built | Partial — graph still exists upstream |
| Typical use | Inference, validation loops | Target networks, stop-gradient |
| Output requires_grad | False | False |
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...
