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

PyTorch · Module

tensor.numpy

Converts a tensor to a NumPy ndarray. By default, conversion requires a CPU tensor with no gradients or unresolved conjugate bit and a NumPy-compatible dtype and layout; force=True permits preparation steps and possible copying.

How it works

Tensor.numpy() exposes a tensor as a NumPy array when the default constraints are satisfied. With force=True, it detaches the tensor, moves it to CPU, resolves conjugate and negative views, and then performs the conversion; this may require a copy.

Syntax

Tensor.numpy(*, force=False) → numpy.ndarray

Parameters

NameTypeDescription
forceboolWhen true, the returned ndarray may be a copy instead of always sharing memory; defaults to false.

Returns

numpy.ndarray

Key points

  • The method converts a PyTorch tensor into a NumPy ndarray.
  • Successful default conversion creates a shared-storage view rather than an independent result.
  • Mutations made through either object are observable through the other when storage is shared.
  • Forced conversion combines detachment, CPU placement, and resolution of conjugate and negative representations.
  • The force option provides a compact alternative to spelling out the preparation steps.

Common mistakes

  • The default conversion requires a CPU tensor.
  • Default conversion also requires no gradient tracking, no conjugate bit, and NumPy-compatible dtype and layout.
  • Using force=True can produce an array that does not share storage, especially for non-CPU or flagged tensors.

Example: Convert a CPU tensor to NumPy

import torch

x = torch.tensor([1.0, 2.0, 3.0])
a = x.numpy()
a[0] = 9.0
print(x)  # tensor([9., 2., 3.])

Creates a CPU tensor, converts it to an ndarray, and demonstrates that both objects observe the same storage.

When to use it

Interoperate with NumPy

Use a CPU tensor as input to NumPy-based analysis or visualization code.

Share storage for efficient data exchange

Inspect or edit CPU tensor data through an ndarray without allocating another buffer.

Handle tensors outside default constraints

Convert tensors that require gradients or reside on an accelerator after explicitly detaching and moving them.

Interview questions

QWhat does Tensor.numpy() return, and when does the default conversion work?

It returns the tensor as a NumPy ndarray. By default, conversion is allowed only for a CPU tensor that does not require gradients, has no conjugate bit, and uses a NumPy-supported dtype and layout.

Follow-up: What does force=True change? It applies detach, CPU transfer, conjugate resolution, and negative-bit resolution before conversion, so the result may be a copy.

QWhat is the memory relationship between the returned array and the tensor?

When conversion succeeds under the default rules, the ndarray and tensor share storage. Therefore, modifying one is visible through the other.

Follow-up: Can storage sharing be assumed with force=True? No. The documentation says the result may be a copy when the tensor is not on CPU or has conjugate or negative bits.

QHow does the force argument affect conversion?

force=True is shorthand for t.detach().cpu().resolve_conj().resolve_neg().numpy(). It is useful when the tensor does not meet the default conversion requirements, but sharing is not guaranteed.

Follow-up: Why might a caller prefer the default? It preserves the stricter zero-copy conditions and exposes unsupported situations instead of silently permitting a copy.

  • torch.from_numpy
  • torch.as_tensor
  • torch.Tensor.detach
  • torch.Tensor.cpu
  • torch.Tensor.resolve_conj
  • torch.Tensor.resolve_neg

Source: torch.Tensor.numpy. Checked 2026-10-16.