RuntimeError: element 0 of variables does not require grad and does not have a grad_fn

I am having serious issue with this error:

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

I think the biggest problem is that it doesn’t tell me which tensor is causing this error. I check and re check and everything seems right. But I can’t seem to find who is causing this. My model seems fine.

Can we have a more informative error message here please? Perhaps once it’s caught it can print the tensor or something to identify who is the culprit?


Addendum:

My error was caused by a torch.no_grad(). I used it but I was doing meta-learning (evaluation/testing phase) so grads are needed for things like MAML…new territory for the bug explored!

regardless, a better error msg would be great.

3 Likes