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

Exception has occurred: RuntimeError       
(note: full exception trace is shown but execution is paused at: _run_module_as_main)
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..
出现这个问题的绝大部分原因是:梯度反向传播时,有参数没有grad_fn属性值。查看自己写的代码:

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

很明显,这里的参数 temp 丢失了 requires_grad属性,所以导致没能梯度更新。
但其实,这段代码还有另外一个更加严重的问题: 模型中的参数根本得不到更新,因为这里数据的源头到temp就断了。虽然temp参数 是通过logits 变化得到的,但是因为梯度更新时是无法处理这个for循环的,导致model中的参数不会得到更新。

上一篇:Python 实现自动微分


下一篇:OpenCV之更多梯度算子