Pytorch: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead

大城市里の小女人 提交于 2020-12-04 15:08:09

问题


I have an error in my code which is not getting fixed any which way I try.

The Error is simple, I return a value:

torch.exp(-LL_total/T_total)

and get the error later in the pipeline:

RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

Solutions such as cpu().detach().numpy() give the same error.

How could I fix it? Thanks.


回答1:


 Error reproduced

import torch

tensor1 = torch.tensor([1.0,2.0],requires_grad=True)

print(tensor1)
print(type(tensor1))

tensor1 = tensor1.numpy()

print(tensor1)
print(type(tensor1))

which leads to the exact same error for the line tensor1 = tensor1.numpy():

tensor([1., 2.], requires_grad=True)
<class 'torch.Tensor'>
Traceback (most recent call last):
  File "/home/badScript.py", line 8, in <module>
    tensor1 = tensor1.numpy()
RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

Process finished with exit code 1

Generic solution

this was suggested to you in your error message, just replace var with your variable name

import torch

tensor1 = torch.tensor([1.0,2.0],requires_grad=True)

print(tensor1)
print(type(tensor1))

tensor1 = tensor1.detach().numpy()

print(tensor1)
print(type(tensor1))

which returns as expected

tensor([1., 2.], requires_grad=True)
<class 'torch.Tensor'>
[1. 2.]
<class 'numpy.ndarray'>

Process finished with exit code 0

Some explanation

You need to convert your tensor to another tensor that isn't requiring a gradient in addition to its actual value definition. This other tensor can be converted to a numpy array. Cf. this discuss.pytorch post. (I think, more precisely, that one needs to do that in order to get the actual tensor out of its pytorch Variable wrapper, cf. this other discuss.pytorch post).




回答2:


I had the same error message but it was for drawing a scatter plot on matplotlib.

There is 2 ways I could get out of this error message :

  1. import the fastai.basics library with : from fastai.basics import *

  2. If you only use the torch library, remember to take off the requires_grad with :

    with torch.no_grad():
        (your code)
    



回答3:


For existing tensor

from torch.autograd import Variable

type(y)  # <class 'torch.Tensor'>

y = Variable(y, requires_grad=True)
y = y.detach().numpy()

type(y)  #<class 'numpy.ndarray'>


来源:https://stackoverflow.com/questions/55466298/pytorch-cant-call-numpy-on-variable-that-requires-grad-use-var-detach-num

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!