'Can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu()

My code in colab worked in January and now it doesn't. It's a Style Transfer program using pytorch (https://github.com/leongatys/PytorchNeuralStyleTransfer). This is the relevant snippet:

max_iter = 500
show_iter = 50
optimizer = optim.LBFGS([opt_img]);
n_iter=[0]

while n_iter[0] <= max_iter:

def closure():
    optimizer.zero_grad()
    out = vgg(opt_img, loss_layers)
    layer_losses = [weights[a] * loss_fns[a](A, targets[a]) for a,A in enumerate(out)]
    loss = sum(layer_losses)
    loss.backward()
    n_iter[0]+=1
    if n_iter[0]%show_iter == (show_iter-1):
        print('Iteration: %d, loss: %f'%(n_iter[0]+1, loss.item()))
enumerate(layer_losses)])
    return loss

optimizer.step(closure)

out_img = postp(opt_img.data[0].cpu().squeeze())
imshow(out_img)
gcf().set_size_inches(10,10)

I get an error as follows:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-11-79d2e162c3f3> in <module>()
     20         return loss
     21 
---> 22     optimizer.step(closure)
     23 
     24 #display result

7 frames
/usr/local/lib/python3.7/dist-packages/torch/optim/optimizer.py in wrapper(*args, **kwargs)
     86                 profile_name = "Optimizer.step#{}.step".format(obj.__class__.__name__)
     87                 with torch.autograd.profiler.record_function(profile_name):
---> 88                     return func(*args, **kwargs)
     89             return wrapper
     90 

/usr/local/lib/python3.7/dist-packages/torch/autograd/grad_mode.py in decorate_context(*args, **kwargs)
     25         def decorate_context(*args, **kwargs):
     26             with self.clone():
---> 27                 return func(*args, **kwargs)
     28         return cast(F, decorate_context)
     29 

/usr/local/lib/python3.7/dist-packages/torch/optim/lbfgs.py in step(self, closure)
    309 
    310         # evaluate initial f(x) and df/dx
--> 311         orig_loss = closure()
    312         loss = float(orig_loss)
    313         current_evals = 1

/usr/local/lib/python3.7/dist-packages/torch/autograd/grad_mode.py in decorate_context(*args, **kwargs)
     25         def decorate_context(*args, **kwargs):
     26             with self.clone():
---> 27                 return func(*args, **kwargs)
     28         return cast(F, decorate_context)
     29 

<ipython-input-11-79d2e162c3f3> in closure()
     11         out = vgg(opt_img, loss_layers)
     12         layer_losses = [weights[a] * loss_fns[a](A, targets[a]) for a,A in enumerate(out)]
---> 13         loss = sum(layer_losses)
     14         loss.backward()
     15         n_iter[0]+=1

<__array_function__ internals> in sum(*args, **kwargs)

/usr/local/lib/python3.7/dist-packages/numpy/core/fromnumeric.py in sum(a, axis, dtype, out, keepdims, initial, where)
   2258 
   2259     return _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims,
-> 2260                           initial=initial, where=where)
   2261 
   2262 

/usr/local/lib/python3.7/dist-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     84                 return reduction(axis=axis, out=out, **passkwargs)
     85 
---> 86     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
     87 
     88 

/usr/local/lib/python3.7/dist-packages/torch/_tensor.py in __array__(self, dtype)
    730             return handle_torch_function(Tensor.__array__, (self,), self, dtype=dtype)
    731         if dtype is None:
--> 732             return self.numpy()
    733         else:
    734             return self.numpy().astype(dtype, copy=False)

RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.
    
    TypeError: can't convert cuda:0 device type tensor to numpy. 

I also previously got:

Use Tensor.cpu() to copy the tensor to host memory first.

I understand that the tensor cannot be converted to numpy. However, I've tried adding .cpu() to various positions in the code, but where is the tensor?

I've also tried modifying _tensor.py in dist-packages/torch/ with no luck. And changing the sum to torch.sum

Is it possible that this is related to this issue:

https://github.com/pytorch/pytorch/issues/44023

I do not have the GPU power to run this on a local machine. Thanks!

UPDATE: I've created a workaround by rolling back the versions of torch, torchvision and numpy in Colab. It's the interaction of the latest versions of numpy and pytorch that is causing the error.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source