Convert PyTorch Tensor to Numpy

In my decade-plus career as a Python developer, I’ve found that converting PyTorch tensors to NumPy arrays is a fundamental skill when working with deep learning projects.

This conversion is crucial for leveraging PyTorch’s computational power and NumPy’s data manipulation capabilities.

In this article, I’ll share various methods to convert PyTorch tensors to NumPy arrays, along with practical examples that you can apply to your projects right away.

PyTorch Tensor

PyTorch tensors are similar to NumPy arrays but with the added advantage of GPU acceleration. They form the foundation of all operations in PyTorch.

When building machine learning models, you often need to move data between PyTorch and NumPy formats, especially for data preprocessing and result visualization.

Convert PyTorch Tensor to Numpy

Let me explain to you the methods to convert a PyTorch tensor to numpy.

Read PyTorch MSELoss

Method 1: Use the .numpy() Method

The simplest way to convert a PyTorch tensor to a NumPy array is by using the built-in .numpy() method.

import torch
import numpy as np

# Create a PyTorch tensor
pytorch_tensor = torch.tensor([1, 2, 3, 4, 5])

# Convert to NumPy array
numpy_array = pytorch_tensor.numpy()

print("PyTorch Tensor:", pytorch_tensor)
print("NumPy Array:", numpy_array)
print("Type:", type(numpy_array))

Output:

PyTorch Tensor: tensor([1, 2, 3, 4, 5])
NumPy Array: [1 2 3 4 5]
Type: <class 'numpy.ndarray'>

You can refer to the screenshot below to see the output:

torch tensor to numpy

This method works perfectly for tensors that are already on the CPU. I’ve used this approach countless times for quick conversions in data preprocessing pipelines.

Method 2: Convert GPU Tensors with .cpu().numpy()

If your tensor is on a GPU, you’ll need to move it to the CPU before converting it to a NumPy array using .cpu().numpy().

# Create a tensor on GPU (if available)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
gpu_tensor = torch.tensor([10, 20, 30, 40, 50]).to(device)

# Convert GPU tensor to NumPy array
numpy_array = gpu_tensor.cpu().numpy()

print("NumPy Array from GPU tensor:", numpy_array)

Output:

NumPy Array from GPU tensor: [10 20 30 40 50]

You can refer to the screenshot below to see the output:

torch to numpy

This is crucial because NumPy arrays don’t support GPU memory, and attempting to directly convert would result in a runtime error.

Method 3: Use .detach().numpy() for Gradient Tensors

When dealing with tensors that have gradients attached (like model parameters during training), you’ll want to use .detach().numpy():

# Create a tensor with gradients
x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)

# Convert to NumPy array while detaching from computation graph
numpy_array = x.detach().numpy()

print("Original tensor (with gradients):", x)
print("NumPy array (detached):", numpy_array)

Output:

Original tensor (with gradients): tensor([1., 2., 3.], requires_grad=True)
NumPy array (detached): [1. 2. 3.]

You can refer to the screenshot below to see the output:

convert tensor to numpy

I often use this when I need to visualize model weights or gradients during the training process without affecting the computational graph.

Method 4: Handle Complex Tensors

For more complex tensors like multi-dimensional arrays representing image data, the conversion process remains the same:

# Creating a 3D tensor (batch of images)
image_batch = torch.rand(4, 28, 28)  # 4 grayscale images of size 28x28

# Convert to NumPy
np_images = image_batch.numpy()

print("Tensor shape:", image_batch.shape)
print("NumPy array shape:", np_images.shape)

I’ve found this particularly useful when preprocessing image datasets like MNIST or CIFAR-10 for U.S.-based computer vision applications.

Method 5: Convert and Reshape in One Go

Sometimes, you need to reshape your data during conversion. You can combine these operations:

# Create a PyTorch tensor
sales_data = torch.tensor([
    [1200, 1500, 1100],  # Q1, Q2, Q3 sales for Product A
    [950, 1050, 1200]    # Q1, Q2, Q3 sales for Product B
])

# Convert to NumPy and reshape
quarterly_sales = sales_data.numpy().reshape(-1)

print("Flattened sales data:", quarterly_sales)

This example flattens quarterly sales data for different products into a single array, which I’ve used for time series analysis of retail data.

Check out PyTorch Early Stopping

Performance Considerations

When working with large tensors, conversion time can be significant. In my experience, converting from PyTorch tensors to NumPy arrays can sometimes be slow, especially for GPU tensors.

Here’s a comparison of conversion times for different-sized tensors:

import time

# Test conversion performance
sizes = [(1000, 1000), (5000, 5000), (10000, 10000)]

for size in sizes:
    tensor = torch.rand(size)

    start_time = time.time()
    numpy_array = tensor.numpy()
    conversion_time = time.time() - start_time

    print(f"Tensor size {size}: Conversion took {conversion_time:.4f} seconds")

For critical applications, I recommend batching these conversions or keeping data in the same format as much as possible.

Common Issues and Solutions

Now, I will explain the common issues that are faced while working with

Issue 1: “Can’t convert CUDA tensor to NumPy”

If you see this error, your tensor is on the GPU. Always use .cpu() before .numpy():

# Solution
numpy_array = gpu_tensor.cpu().numpy()

Issue 2: Gradient computation affected

If you’re modifying values and need to maintain the gradient computation graph:

# Wrong way (breaks gradient flow)
weights = model.layer1.weight.numpy()  # This detaches from the graph

# Right way (keeps a copy without affecting the original)
weights_copy = model.layer1.weight.detach().clone().numpy()

Read PyTorch Model Eval

Real-World Example: Stock Price Analysis

Let’s see a practical example where I use both PyTorch and NumPy for analyzing stock price data:

import torch
import numpy as np
import matplotlib.pyplot as plt

# Sample stock prices for a U.S. tech company (5 days)
stock_prices = torch.tensor([
    [142.5, 145.2, 144.3, 146.8, 145.5],  # Opening prices
    [145.0, 146.0, 145.8, 148.2, 147.3],  # Closing prices
])

# Convert to NumPy for analysis
np_prices = stock_prices.numpy()

# Calculate daily returns using NumPy
daily_returns = (np_prices[1] - np_prices[0]) / np_prices[0] * 100

print("Daily returns (%):", daily_returns)

# Plotting with matplotlib
plt.figure(figsize=(10, 6))
plt.bar(["Mon", "Tue", "Wed", "Thu", "Fri"], daily_returns)
plt.title("Daily Stock Returns (%)")
plt.ylabel("Return (%)")
plt.axhline(y=0, color='r', linestyle='-', alpha=0.3)
plt.show()

This example demonstrates how I might use PyTorch for the initial data handling and then convert to NumPy for statistical analysis.

When to Use PyTorch Tensor vs. NumPy Array

Based on my experience, here’s when to use each:

Use PyTorch Tensors for:

  • Deep learning model training
  • Operations that need GPU acceleration
  • Automatic differentiation (gradient calculations)

Use NumPy Arrays for:

  • Data preprocessing and exploration
  • Statistical analysis
  • Integration with other data science libraries
  • Visualization

Converting between the two gives you the best of both worlds.

PyTorch tensors and NumPy arrays are two powerful tools in the Python ecosystem for numerical computing. Knowing how to convert between them efficiently opens up possibilities for building sophisticated data pipelines and machine learning models.

The methods I’ve shared here have served me well across numerous projects, from simple data analysis to complex deep learning applications. The key is to understand when to use each format and how to move between them seamlessly.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.