How to convert a list of cuda tensors to a list of cpu tensors?

So I have a big list of cuda tensors containing loss values of each iteration but I can’t even scatter plot it. How can I convert it to a list of cpu tensors or even numpy array without iterating over the indices? Thank You!

Hi,

In this post I have written a code to transfer a list of CPU tensors to GPU which all have the same shape (in your case all have shape of (1, )).

Bests

1 Like

Thank you so much @Nikronic .

Just creating a new tensor with torch.tensor() worked. Then simply plotted the scatter plot on torch tensor (with device = cpu).

new_tensor = torch.tensor(list_of_cuda_tensors, device = 'cpu')

2 Likes

But, what if you want to keep it as a list of tensors after the transfer from gpu to cpu

1 Like

Thanks! This helped me with my code which ran on cpu but running a plot in gpu. :slight_smile:

In a context where performance is a concern, you’d be better off stacking the scalar tensors first then moving to cpu: torch.stack(list_of_losses).cpu(). Otherwise [t.item() for t in list_of_losses] seems more idiomatic to me if you want the result as a list of floats.