We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
Devices and Data Movement
PyTorchWrite code that respects tensor devices and avoids unnecessary data movement. Examine transfer and copy behavior using the checks provided by each exercise.
4 exercises
Explore freely. Sign in to keep your progress.
Your learning sequence
- 1 Not solved yet. How many copies did that cost Practice · Medium
- 2 Not solved yet. Where does a new tensor go Practice · Easy
- 3 Not solved yet. Accumulate losses without extracting Python scalars Practice · Medium
- 4 Not solved yet. Register a buffer that follows module conversions 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 a supported NumPy dtype and explicit CPU placement, which tensor shares the array's data?
Choose one answer to reveal the explanations. Answers are kept only while this page is open.
import numpy as np
import torch
a = np.ones(3, dtype=np.float32)
copied = torch.tensor(a, device="cpu")
shared = torch.as_tensor(a, device="cpu")
a[0] = 7.0
print(copied.tolist())
print(shared.tolist())