You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何将PyTorch张量(Torch Tensor)转换为NumPy数组?

Converting PyTorch Tensors to NumPy Arrays: Step-by-Step Guide

Got it, converting a PyTorch tensor to a NumPy array is straightforward once you know the small nuances depending on where your tensor lives and whether it tracks gradients. Let’s walk through all the common scenarios with code examples.

1. Basic Conversion for CPU Tensors

If your tensor is already on the CPU, you can use the built-in .numpy() method directly:

import torch
import numpy as np

# Create a CPU-based PyTorch tensor
cpu_tensor = torch.tensor([1, 2, 3, 4])
# Convert to NumPy array
numpy_array = cpu_tensor.numpy()

print(type(numpy_array))  # Output: <class 'numpy.ndarray'>
print(numpy_array)        # Output: [1 2 3 4]

Important Note: Memory Sharing

When converting a CPU tensor to NumPy, the two objects share the same underlying memory. That means modifying one will affect the other:

# Modify the original tensor
cpu_tensor[0] = 100
# The NumPy array reflects the change
print(numpy_array)  # Output: [100   2   3   4]

If you want to avoid this (create an independent copy), clone the tensor first:

numpy_array = cpu_tensor.clone().numpy()

2. Converting CUDA (GPU) Tensors

NumPy arrays live on the CPU, so you can’t directly call .numpy() on a GPU tensor. First move the tensor to the CPU with .cpu(), then convert:

# Create a tensor on GPU
gpu_tensor = torch.tensor([1, 2, 3, 4]).cuda()
# Move to CPU then convert to NumPy
numpy_array = gpu_tensor.cpu().numpy()

print(type(numpy_array))  # Output: <class 'numpy.ndarray'>

Alternatively, you can use .to('cpu') for the same result:

numpy_array = gpu_tensor.to('cpu').numpy()

3. Converting Tensors with Gradients (requires_grad=True)

Tensors that track gradients (used for training) will throw an error if you try to call .numpy() directly. You need to detach the gradient first using .detach():

# Create a tensor with gradient tracking
grad_tensor = torch.tensor([1, 2, 3, 4], requires_grad=True)
# Detach gradients then convert
numpy_array = grad_tensor.detach().numpy()

For GPU tensors with gradients, combine the CPU move and detach steps:

grad_gpu_tensor = torch.tensor([1, 2, 3, 4], requires_grad=True).cuda()
numpy_array = grad_gpu_tensor.cpu().detach().numpy()

Why .detach()?

.detach() creates a new tensor that doesn’t track gradients, which makes it safe to convert to NumPy. Avoid using .data for this—it’s deprecated and can lead to unexpected behavior with autograd.

Common Pitfalls to Avoid

  • Forgetting to move GPU tensors to CPU: This will throw a TypeError since NumPy can’t handle GPU memory.
  • Converting gradient-tracking tensors directly: You’ll get a RuntimeError saying you can’t convert a tensor with requires_grad=True to NumPy.
  • Unexpected memory sharing: Always clone the tensor first if you don’t want changes to propagate between the tensor and array.

内容的提问来源于stack exchange,提问作者Gulzar

火山引擎 最新活动