course-v3 icon indicating copy to clipboard operation
course-v3 copied to clipboard

Can't call numpy() on Variable that requires grad

Open srikarplus opened this issue 5 years ago • 2 comments

Hi,

In lesson2-sgd.ipynb there are a few lines that are giving the error

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

fastai - 1.0.60 pytorch - 1.3.1

Accordingly I made the following changes

plt.scatter(x[:,0],y)
plt.scatter(x[:,0],x@a);

to

plt.scatter(x[:,0],y)
with torch.no_grad():
    plt.scatter(x[:,0],x@a);

and also

fig = plt.figure()
plt.scatter(x[:,0], y, c='orange')
line, = plt.plot(x[:,0], x@a)
plt.close()

def animate(i):
    update()
    line.set_ydata(x@a)
    return line,

to

fig = plt.figure()
plt.scatter(x[:,0], y, c='orange')
with torch.no_grad():
    line, = plt.plot(x[:,0], x@a)
plt.close()

def animate(i):
    update()
    with torch.no_grad():
        line.set_ydata(x@a)
    return line,

srikarplus avatar Jan 02 '20 10:01 srikarplus

Thank you!

jpoechill avatar Mar 20 '20 00:03 jpoechill

Thanks. This solved my problem.

SeriousHyena avatar Jun 03 '20 12:06 SeriousHyena