Backward error : 'NoneType' object has no attribute 'data'

hi. i want to compute the gradient with pytorch autograd in a loop (because i have may training set):

Basicaly it compute the gradient hust for the first loop.
for the second loop, it show this error :

please what i am doing wrong or how can i solve it.
thanks

i have change the update of weights and bias like this

        w=Variable((w+(alpha*(-1)*y_hat_w_grad)),requires_grad=True)
        b=Variable((b+(alpha*(-1)*y_hat_b_grad)),requires_grad=True)

the code doesn’t not show any error but i still wonder if it compute good the gradient (with these two lines

)

Hi,
First of all, could you please reformat your code section using ```???
It is really hard to read.

And note that Variable has been removed from PyTorch, and now if you want to enable gradient computing on a tensor use following snippet:

b = torch.tensor([0.7])
b.requires_grad = True

But about this part:

I think you first need to clone then detach. If you detach first, your w will no longer have grad because it is detached from computing graph.
So the solution would be something like this:

y_hat_w_grad=w.grad.data.clone().detach()

Bests
Nik

1 Like

thank you for the info about “Variable”. what you proposed works.