Skip to content
← Learn PyTorch

Devices and Data Movement

PyTorch

Write 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. 1 Not solved yet. How many copies did that cost Practice · Medium
  2. 2 Not solved yet. Where does a new tensor go Practice · Easy
  3. 3 Not solved yet. Accumulate losses without extracting Python scalars Practice · Medium
  4. 4 Not solved yet. Register a buffer that follows module conversions Practice · Medium

Check yourself

4 questions · not saved while you're signed out

Use these questions to check your understanding and compare explanations. Your score does not affect track completion.

0 / 4 correct

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())
Question 1 of 4