Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.from_numpy

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.

How it works

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.

Syntax

torch.from_numpy(ndarray) → Tensor

Parameters

NameTypeDescription
ndarraynumpy.ndarrayThe NumPy ndarray to wrap as a tensor.

Returns

A Tensor sharing memory with the input NumPy ndarray; it is not resizable.

Key points

  • Pass a NumPy ndarray to obtain a PyTorch Tensor.
  • The conversion is zero-copy with shared storage rather than an independent data buffer.
  • Updates are bidirectional: changing either representation changes what the other sees.
  • Supported floating-point dtypes include NumPy float16, float32, and float64.
  • The documented supported types include complex64, complex128, int64, int32, and int16.
  • The supported list also includes int8, uint8, and boolean arrays.

Common mistakes

  • Do not write through the tensor when its source NumPy array is read-only; the documentation says this results in undefined behavior.
  • Operations that resize the returned tensor are incompatible with this tensor's storage.

Example: Shared storage with NumPy

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.

When to use it

Zero-copy NumPy interoperability

Convert CPU-side NumPy preprocessing output into a PyTorch tensor without duplicating its storage.

Shared data pipelines

Edit or inspect data through either the ndarray or tensor representation when shared updates are desirable.

Supported dtype conversion

Feed supported NumPy numeric or boolean arrays into PyTorch code.

Interview questions

QDoes torch.from_numpy copy the NumPy data?

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.

QWhich NumPy dtypes does torch.from_numpy accept?

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.

QWhat constraints apply to the returned tensor?

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_tensor
  • torch.asarray
  • torch.tensor
  • torch.Tensor.numpy

Source: torch.from_numpy — PyTorch main documentation. Checked 2026-10-16.