如何将PyTorch张量(Torch Tensor)转换为NumPy数组?
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
TypeErrorsince NumPy can’t handle GPU memory. - Converting gradient-tracking tensors directly: You’ll get a
RuntimeErrorsaying you can’t convert a tensor withrequires_grad=Trueto 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




