We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
Memory, Copies and In-Place
PyTorchCompare clone, detach, copy_, output buffers and in-place updates. Track shared storage and learn when mutation conflicts with the required behavior.
6 exercises
Explore freely. Sign in to keep your progress.
Your learning sequence
- 1 Not solved yet. Clone, detach, or both Practice · Medium
- 2 Not solved yet. Write into the buffer you were given Practice · Easy
- 3 Not solved yet. Fill the caller's buffer Practice · Easy
- 4 Not solved yet. Save independent tensor snapshots Practice · Medium
- 5 Not solved yet. Updating a parameter by hand Practice · Medium
- 6 Not solved yet. Accumulate into an existing tensor Practice · Medium
Check yourself
4 questions · not saved while you're signed outUse these questions to check your understanding and compare explanations. Your score does not affect track completion.
With gradient recording enabled, how do clone() and detach() differ for this tensor?
Choose one answer to reveal the explanations. Answers are kept only while this page is open.
import torch
with torch.enable_grad():
z = torch.ones(2, requires_grad=True)
detached = z.detach()
cloned = z.clone()
print(detached.requires_grad, detached.data_ptr() == z.data_ptr())
print(cloned.requires_grad, cloned.data_ptr() == z.data_ptr())