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 |
More Related questions...