NumPy interop

PyTorch · Essentials · 6 min

Data usually arrives as NumPy arrays and results often go back to NumPy, pandas or matplotlib.

PyTorch and NumPy convert cheaply in both directions:

  • torch.from_numpy(a) wraps a NumPy array as a tensor sharing the same memory: change one and you change the other. The dtype is kept, so a default NumPy float array becomes float64.
  • torch.tensor(a) and torch.as_tensor(a, dtype=...) with a different dtype copy.
  • t.numpy() returns an array sharing memory with a CPU tensor.

Two restrictions apply to .numpy(). The tensor must be on the CPU, and it must not require gradients. The standard idiom for getting results out of a model is therefore:

arr = out.detach().cpu().numpy()

detach() drops the autograd history, and cpu() copies from the GPU if needed (it is a no-op for CPU tensors).

Watch the dtype on the way in. Models use float32 weights, so feeding them float64 tensors raises expected scalar type Float but found Double. Convert with .float() or torch.from_numpy(a.astype(np.float32)). Integer labels should be int64.

For plotting single values or logging, prefer .item() or .tolist() over converting a whole array.

Example

import numpy as np
import torch

a = np.array([1.0, 2.0, 3.0])
t = torch.from_numpy(a)          # shares memory, dtype float64
a[0] = 100
print(t)                         # tensor([100.,   2.,   3.], dtype=torch.float64)

c = torch.tensor(a)              # independent copy
a[1] = -1
print(c[1].item())               # 2.0

x = t.float()                    # float32 for use with model weights
print(x.dtype)                   # torch.float32

w = torch.ones(2, requires_grad=True)
print(w.detach().cpu().numpy())  # [1. 1.]
# w.numpy() would raise: Can't call numpy() on Tensor that requires grad

Remember

  • from_numpy and .numpy() share memory; torch.tensor copies
  • Use .detach().cpu().numpy() to export model outputs
  • NumPy floats are float64: convert to float32 before feeding a model

Watch out for

  • Mutating a NumPy array after from_numpy and being surprised the tensor changed too.

Check yourself

  1. Why does model_output.numpy() fail inside a training script on a GPU?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: pytorch.org/docs/stable/generated/torch.from_numpy.html

Builds on

Unlocks

Nothing builds on this yet.