Lesson 13 RuntimeError does not have a grad_fn

(James_Ying) #1

When I run the Lesson 13 notebook on pytorch 0.4 with windows 10.
Got error:
RuntimeError: element 0 of variables does not require grad and does not have a grad_fn

I found only one person have the same problem.
Maybe most of the person run on pytorch 0.3.

Change one line in core.py in fastai/dl2



(Jon Hunter) #2

Thank you so much!
I was also able to get it working in the lesson 13 example by changing the hook function itself in a similar way.
def hook_fn(self, module, input, output): self.features = to_gpu(torch.tensor(output,requires_grad=True))
And therefore not passing the features into V() in the loss function.
To anyone else searching, the symptom I had was originally a the fault listed above. I added in requires_grad=True in the loss functions and it would now run, but the loss did not change at all. I wasn’t sure if it was the images I was using, or the multipliers used in the loss function. But I guess a useful insight is that if there’s no change in loss at all, the gradient is not calculated all the way through?