Bug: Torch.argmax() 导致梯度没有回传
BUG 没有 导致 梯度 torch
2023-09-27 14:27:45 时间
因为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
相关文章
- BUG_vector iterator not dereferencable
- 由于博客园BUG太多,一直没有更新
- Android studio 一个项目中添加两个module遇到的bug
- 如何跟程序员谈一场没有Bug的恋爱
- 关于AFNetworking 安全bug的回复
- ****使用ftp软件上传下载php文件时换行符丢失bug
- unzip 解压缩存在Bug-- 这个方法不行啊
- Oracle:真正解决在 x64位 linux 上安装oracle10g报错和bug问题!
- 关于GreenOdoo的一个Bug
- 如何提交一个高质量的bug?
- Windows 7 bug: nonexistent Java Runtime Enviroment
- Dynamics CRM2016 站点地图Bug之KnowledgeArticle不显示
- HBase多次加载-ROOT-和META的bug
- springboot + mybatis-pagehelper 参数查询不分页的bug。。。
- 解决一个偶现的503 bug,花了俺不少时间
- Delphi 完整的Bug决议工具EurekaLog的使用
- oracle已知会导致错误结果的bug列表(Bug Issues Known to cause Wrong Results)
- CTF-WEB:攻防世界-bug(综合应用)
- 记一次使用 android 自带 WebView 做富文本编辑器之API、机型的兼容及各种奇葩bug的解决
- 编程语言之 Python入门到精通.(基本知识:函数,Bug的由来及分类,异常处理机制等.)