Tensor.cpu() problem

I am struggling with this tensor.cpu() problem

It seems like the latest pytorch upstream code had not solved this completely yet.

TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.


This is the expected behaviour. If you want a cuda tensor as a numpy array, you have to first send it to the cpu with .cpu() then you can get the numpy array with .numpy().

in this case, how should I modify this affected line of code ? It seems like .cpu() is used in the next line

This line seems fine. First, it does the operation on tensor v as v = v / np.sqrt(torch.sum(v * v)) and then net line it will send that to CPU: ... = v.cpu()

but look again at the back trace given at https://github.com/jacobgil/pytorch-pruning/issues/16#issuecomment-429922322

It is pointing the problem at line 101

Someone told me: we can’t mix gpu and numpy variables in an algorithm or chunk of code

So, he told me to find a pytorch alternative for numpy.sqrt()

numpy is cpu only. So you can’t use it to perform gpu operations. You can use torch.sqrt() to replace that :wink: