53

Calling tensor.numpy() gives the error:

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

tensor.cpu().detach().numpy() gives the same error.

1
  • 3
    Calling var.detach().numpy(), as stated, is what you want (where var is the name of the tensor you want to convert to a numpy array). If that's not working for some reason, you'll need to provide more details on what code you're running to get this error. The torch.exp(... line isn't relevant. What is cpu here? Is that the name of the tensor you want to convert? If so, do cpu.detach().numpy(). Or is it a function which returns a tensor?
    – Nathan
    Apr 2, 2019 at 3:10

5 Answers 5

51

 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).

3
  • 3
    This happened to me in second tutorial of fast.ai. In this case, instead of directly using nn.Parameter variables for plotting, copying the detached variables into a separate tensors and plotting them solved the issue.
    – dinesh ygv
    Apr 4, 2020 at 19:01
  • For further explanation on why the detach method is needed, please see this question on which I awarded a bounty. Sep 15, 2020 at 16:46
  • Use 'tensor1 = tensor1.detach().numpy()' instead of 'tensor1 = tensor1.numpy()'.
    – Tom J
    Jan 17 at 6:27
29

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

There is 2 steps 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)
    
4
  • What to do after importing fastai.basics ?
    – sunwarr10r
    Jun 25, 2019 at 16:01
  • Sorry couldn't answer sooner, once you've imported, you just copy/paste your code in the with torch.no_grad(): part and just let the magic go Sep 3, 2019 at 12:52
  • 6
    That is 1 way with 2 steps, not 2 ways.
    – Rub
    Mar 9, 2020 at 19:11
  • 1
    I encountered similar problem while doing plot. only doing 2nd step worked for me. Mar 1, 2022 at 3:00
6

I just ran into this problem when running through epochs, I recorded the loss to a list

final_losses.append(loss)

Once I ran through all the epochs I wanted to graph the output

plt.plot(range(epochs), final_loss)
plt.ylabel('RMSE Loss')
plt.xlabel('Epoch');

I was running this on my Mac, with no problem, but, I needed to run this on a windows PC and it produced the error noted above. So, I checked the type of each variable.

Type(range(epochs)), type(final_losses)

range, list

Seems like it should be OK.

It took a little bit of fidgeting to realize that the final_losses list was a list of tensors. I then converted them to an actual list with a new list variable fi_los.

fi_los = [fl.item() for fl in final_losses ]
plt.plot(range(epochs), fi_los)
plt.ylabel('RMSE Loss')
plt.xlabel('Epoch');

Success!

1
  • fi_los = [fl.item() for fl in final_losses], this worked really well in my case Jan 16 at 16:35
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'>
3

Best solution is to use torch.no_grad(): the context manager which disables the tracking of the gradient locally.

Just write your code inside this contact manager like:

with torch.no_grad():
   graph_x = some_list_of_numbers
   graph_y = some_list_of_tensors

   plt.plot(graph_x, graph_y)
   plt.show()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.