This error occurs when you attempt to convert a PyTorch tensor residing on the GPU (cuda:0 device) directly to a NumPy array. To resolve this, you need to first transfer the tensor to the CPU using the '.cpu()' method before converting it to a NumPy array.

Here's an example:

import torch

# Create a tensor on the GPU
x = torch.randn(2, 3).cuda()

# Attempt to convert the tensor to a NumPy array
y = x.numpy()  # This will raise the TypeError

# Move the tensor to the CPU and then convert to a NumPy array
y = x.cpu().numpy()  # This will work correctly

In this example, we initially create a tensor 'x' on the GPU using the '.cuda()' method. Trying to convert it to a NumPy array directly with 'x.numpy()' results in the TypeError. To rectify this, we first move the tensor to the CPU using '.cpu()' and then perform the conversion to a NumPy array using '.numpy()'. This approach will execute without encountering any errors.

TypeError: Converting CUDA Tensor to NumPy Array: Solution and Example

原文地址: https://www.cveoy.top/t/topic/oXYk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录