0
0
PytorchHow-ToBeginner · 2 min read

PyTorch How to Convert Tensor to NumPy Array

To convert a PyTorch tensor to a NumPy array, use the tensor.numpy() method if the tensor is on CPU; if it is on GPU, first move it to CPU with tensor.cpu().numpy().
📋

Examples

Inputtensor = torch.tensor([1, 2, 3])
Outputarray([1, 2, 3])
Inputtensor = torch.tensor([[1.5, 2.5], [3.5, 4.5]])
Outputarray([[1.5, 2.5], [3.5, 4.5]])
Inputtensor = torch.tensor([1, 2, 3], device='cuda')
Outputarray([1, 2, 3])
🧠

How to Think About It

To convert a tensor to a NumPy array, first check if the tensor is stored on the CPU or GPU. If it is on the CPU, you can directly call .numpy(). If it is on the GPU, you must move it to the CPU first using .cpu() before calling .numpy() because NumPy does not support GPU tensors.
📐

Algorithm

1
Get the PyTorch tensor as input.
2
Check if the tensor is on GPU.
3
If yes, move the tensor to CPU using <code>.cpu()</code>.
4
Call <code>.numpy()</code> on the CPU tensor to get a NumPy array.
5
Return the NumPy array.
💻

Code

pytorch
import torch

tensor_cpu = torch.tensor([1, 2, 3])
numpy_array_cpu = tensor_cpu.numpy()
print('CPU tensor to NumPy:', numpy_array_cpu)

if torch.cuda.is_available():
    tensor_gpu = torch.tensor([4, 5, 6], device='cuda')
    numpy_array_gpu = tensor_gpu.cpu().numpy()
    print('GPU tensor to NumPy:', numpy_array_gpu)
Output
CPU tensor to NumPy: [1 2 3] GPU tensor to NumPy: [4 5 6]
🔍

Dry Run

Let's trace converting a CPU tensor [1, 2, 3] to NumPy array.

1

Create tensor

tensor_cpu = torch.tensor([1, 2, 3])

2

Call .numpy()

numpy_array_cpu = tensor_cpu.numpy() -> array([1, 2, 3])

StepTensorActionResult
1[1, 2, 3]Create tensortensor([1, 2, 3])
2tensor([1, 2, 3])Call .numpy()array([1, 2, 3])
💡

Why This Works

Step 1: Tensor storage

PyTorch tensors can be stored on CPU or GPU memory. NumPy arrays only work with CPU memory.

Step 2: Direct conversion on CPU

If the tensor is on CPU, calling .numpy() returns a NumPy array sharing the same memory.

Step 3: GPU tensor handling

If the tensor is on GPU, you must first move it to CPU with .cpu() before calling .numpy().

🔄

Alternative Approaches

Using .detach() before .numpy()
pytorch
tensor = torch.tensor([1, 2, 3], requires_grad=True)
numpy_array = tensor.detach().numpy()
print(numpy_array)
Use .detach() to avoid tracking gradients if tensor requires grad; otherwise, .numpy() may error.
Using .to('cpu') before .numpy()
pytorch
tensor_gpu = torch.tensor([4, 5, 6], device='cuda')
numpy_array = tensor_gpu.to('cpu').numpy()
print(numpy_array)
Alternative to .cpu(), .to('cpu') moves tensor to CPU before conversion.

Complexity: O(n) time, O(n) space

Time Complexity

Conversion takes linear time proportional to the number of elements because data must be copied or shared.

Space Complexity

The NumPy array shares memory with the CPU tensor, so no extra space if tensor is on CPU; if moved from GPU, extra space is used for the CPU copy.

Which Approach is Fastest?

Direct .numpy() on CPU tensor is fastest; moving from GPU adds overhead.

ApproachTimeSpaceBest For
Direct .numpy() on CPU tensorO(n)O(1) shared memoryFast conversion when tensor is on CPU
.cpu().numpy() for GPU tensorO(n)O(n) extra memoryConverting GPU tensors to NumPy arrays
.detach().numpy()O(n)O(1) shared memoryTensors requiring gradient tracking
💡
Always move GPU tensors to CPU with .cpu() before converting to NumPy.
⚠️
Trying to call .numpy() directly on a GPU tensor causes an error because NumPy does not support GPU memory.