Resolving 'TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.' with Lists in PyTorch
Error
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
Despite dealing with a list, not a PyTorch tensor or a NumPy array, the above error can occur. If you follow the instruction and use the .cpu()
or .numpy()
methods, you will encounter the following error.
AttributeError: 'list' object has no attribute 'cpu'
Solution
The reason this error occurs is that the elements of the list are PyTorch tensors. Therefore, to solve this error, you should use torch.stack()
to convert the list into a 1D tensor.
This issue does not arise in a Windows 10/Python 3.9.2
/torch==1.8.1+cu111
environment, but it occurs in a Linux/Python 3.9.7
/torch==1.10.1+cu113
environment, making it incredibly frustrating.
Environment
- OS: Ubuntu 20.04.3
- Version: Python 3.9.7, torch 1.10.1+cu113