I have one of the common issues of type conversion “can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.”
So, I tried to solve like the answer comment " .cpu().numpy() ".
But unfortunately, another issue “list object has no attribute cpu.”
By trying to solve with “.cpu().detach().numpy()”, I got the error “list object has no attribute cpu”.
I also tried one of the related suggestions in this forum like “new_tensor = torch.tensor(old_tensor.item(), device = ‘cpu’)” but still have another issue “4. only one element tensors can be converted to Python scalars”.
The final attempt is trying using with .item() but still " AttributeError ‘list’ object has no attribute ‘item’ ".
So, if you have some idea or advice for me, please feel free to share.
In fact, I’ve also tested that way using .tensor function but got the value error “ValueError: only one element tensors can be converted to Python”.
For the second way of using .cpu().numpy() in the loop got the same error " TypeError: can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first. " as in my very first screenshot.
Thank you, Ms. Srishti. The append function is already used for the variable label (Tensor object) to the variable ratings (a list of Tensor). Here is the code that I am testing and got the above error.
Hi, please consider going through my previous reply again.
Use this:
ratings_arr = [t.cpu().numpy() for t in ratings]
ratings_i = np.vstack(ratings_arr)
predictions_arr = [t.cpu().numpy() for t in predictions]
predictions_i = np.vstack(predictions_arr)
Hi, Srishti-git110, you sure are of help in this forum. I’d appreciate if I can get your help on this similar question;
I get an error from this line:
line3, = plt.plot(range(len(PredyList)),predyList, alpha = 0.8,label = ‘Predicted’)
The Predy list is now a one D list. It initially was two before i flatten. The element lists are tensors
i.e. [tensor([9.5960], device=‘cuda:0’, grad_fn=), tensor([10.0286],
This is the error i get:
can’t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.