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 becomesfloat64.torch.tensor(a)andtorch.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 gradRemember
from_numpyand.numpy()share memory;torch.tensorcopies- 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_numpyand being surprised the tensor changed too.