Bug: Torch.argmax() 导致梯度没有回传

因为argmax函数不能求导,也就不会有梯度

backward的反向传播的梯度到loss是终止了

其实也很容易看出这个问题,如果最后一步使用了argmax,loss的属性 requires_grad=False,直接调用backward会提示你需要给loss增加梯度属性,但这“治表不治本 ”

网上有两个ref参考

https://discuss.pytorch.org/t/torch-argmax-cause-loss-backward-dont-work/64782/2

https://discuss.pytorch.org/t/backpropagation-issue-when-using-argmax/100335/6

以及 TORCH.TENSOR.GRAD 的文档 

This attribute is None by default and becomes a Tensor the first time a call to backward() computes gradients for self. The attribute will then contain the gradients computed and future calls to backward() will accumulate (add) gradients into it.

这段话说明了梯度的两个特点:一是第一次调用backward才会计算梯度,之前是None,二是梯度是可以累加的,如果没有手动清掉的话

补充:

知乎直接给我推了这个问题...: 怎么克服神经网络训练中argmax的不可导性

不过通常来说还是loss设计有问题,比如我本该用回归loss用成了分类loss,才被迫使用argmax

原文地址:https://www.cnblogs.com/lfri/p/15552933.html