【E-03】RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

这个是因为没有将requires_grad设为True,l=LOSS(out,label)中的l默认是requires_grad为false,这个l其实也是一个张量Tensor类型,将其的requires_grad改为True后,使用backward函数就可以得到requires_grad为True的所有参数的梯度。

上面的loss = loss.requires_grad_(),是我添加的部分。

本错误纠正的参考的内容如下:

   

参考文献https://cloud.tencent.com/developer/article/1161882

原文地址:https://www.cnblogs.com/yifanrensheng/p/13381934.html