Zero-copy NumPy interoperability
Convert CPU-side NumPy preprocessing output into a PyTorch tensor without duplicating its storage.
PyTorch · Function
Creates a PyTorch Tensor from a NumPy ndarray while sharing the same memory. Changes made through either object are visible through the other, and the resulting tensor is not resizable.
The function wraps a NumPy ndarray as a PyTorch tensor without copying its storage. Both objects observe changes made through the other, while the resulting tensor cannot be resized; writable use is supported for the documented NumPy dtypes.
torch.from_numpy(ndarray) → Tensor| Name | Type | Description |
|---|---|---|
ndarray | numpy.ndarray | The NumPy ndarray to wrap as a tensor. |
A Tensor sharing memory with the input NumPy ndarray; it is not resizable.
import numpy as np
import torch
a = np.array([1, 2, 3], dtype=np.int32)
t = torch.from_numpy(a)
t[0] = -1
assert a[0] == -1
print(t)
print(a)Creates a tensor sharing storage with a NumPy array, then demonstrates that a tensor update changes the original array.
Convert CPU-side NumPy preprocessing output into a PyTorch tensor without duplicating its storage.
Edit or inspect data through either the ndarray or tensor representation when shared updates are desirable.
Feed supported NumPy numeric or boolean arrays into PyTorch code.
It creates a tensor from a NumPy ndarray and shares the same memory with that array. Consequently, modifications through either object are visible through the other.
Follow-up: What happens if the NumPy array is read-only? Writing through the resulting tensor is unsupported and can produce undefined behavior.
The function accepts ndarrays with documented dtypes including float16, float32, float64, complex64, complex128, int8, int16, int32, int64, uint8, and bool. Other dtypes are not listed as supported by this API page.
Follow-up: How would you handle an unsupported dtype? Convert the ndarray to one of the documented supported dtypes before calling the function.
The returned tensor is not resizable. It also retains shared storage with the source ndarray, so tensor operations must account for that aliasing relationship.
Follow-up: Why might resizing be unsafe here? Resizing would conflict with the tensor's fixed view of storage owned by the NumPy array.
torch.as_tensortorch.asarraytorch.tensortorch.Tensor.numpy