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

Posted yifanrensheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了E-03RuntimeError: 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

以上是关于E-03RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn的主要内容,如果未能解决你的问题,请参考以下文章

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

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

PyTorch:RuntimeError:变量元组的元素0是易失性的

RuntimeError:张量的元素 0 不需要 grad 并且没有 grad_fn

Pytorch RuntimeError:张量的元素 0 不需要 grad 并且没有 grad_fn

RuntimeError:python 多处理错误