当前位置:网站首页>【Torch】解决tensor参数有梯度,weight不更新的若干思路
【Torch】解决tensor参数有梯度,weight不更新的若干思路
2022-07-02 06:25:00 【lwgkzl】
问题:
在torch类里面用nn.Parameter声明了一个可学的Tensor参数, 结果每次梯度回传之后,可以看到变量梯度,但是该参数的weight始终不变,一直保持着初始值。
思路:
遇到某参数的weight一直不更新,解决思路有以下几种:
1. 检查该变量的梯度是否为0或者为None, 对于pytorch的中间变量,输出梯度的方式见博客: https://www.jianshu.com/p/ad66f2e38f2f
如果是None或者0,说明梯度没有传到该变量,顺着代码往下一直输出变量的梯度,直到梯度出现为止,然后检查为啥梯度消失了。
2. 输出梯度后, 检查梯度乘上学习率是否过小, 比如梯度为5e-2,学习率为1e-4,而变量的值只保留五位小数,那么此时由于学习率过小使得更新被变量忽略,需要把学习率调高。
3. 检查该变量是否在optimal step函数之前被替换, 即梯度回传之后, step函数之前,该参数被重新赋值。
4. 最重要的是, 检查参数所在的类,是否加入了optimal的优化参数序列中:(, 不然梯度虽然回传了,但优化器并不会对你的参数产生反应。
如果是一个模型类的列表: 请不要用list类型,使用nn.ModuleList , 如果一个list中包含了三个A类, 把list作为B类的参数的时候(在init函数中赋值),那么这个list里面所有的参数(A类中的参数)都不会被优化, 使用nn.ModuleList可以避免这一点。
大概尝试的思路就这么多, 有遗漏的请路过大佬在评论区指正。
如上
边栏推荐
- Pyspark build temporary report error
- 软件开发模式之敏捷开发(scrum)
- ORACLE 11G利用 ORDS+pljson来实现json_table 效果
- oracle apex ajax process + dy 校验
- php中通过集合collect的方法来实现把某个值插入到数组中指定的位置
- Check log4j problems using stain analysis
- MySQL中的正则表达式
- Explanation of suffix of Oracle EBS standard table
- sqli-labs通关汇总-page1
- 使用 Compose 实现可见 ScrollBar
猜你喜欢

ssm超市订单管理系统

软件开发模式之敏捷开发(scrum)

spark sql任务性能优化(基础)

Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK

Changes in foreign currency bookkeeping and revaluation general ledger balance table (Part 2)

Oracle 11g uses ords+pljson to implement JSON_ Table effect

TCP attack

Sqli - Labs Clearance (less6 - less14)

Brief analysis of PHP session principle

離線數倉和bi開發的實踐和思考
随机推荐
Oracle 11.2.0.3 handles the problem of continuous growth of sysaux table space without downtime
sqli-labs通关汇总-page1
PM2 simple use and daemon
第一个快应用(quickapp)demo
ORACLE EBS DATAGUARD 搭建
Yolov5 practice: teach object detection by hand
Brief analysis of PHP session principle
A summary of a middle-aged programmer's study of modern Chinese history
Agile development of software development pattern (scrum)
Oracle RMAN automatic recovery script (migration of production data to test)
Check log4j problems using stain analysis
如何高效开发一款微信小程序
sqli-labs通關匯總-page2
Differences between ts and JS
IDEA2020中PySpark的两表关联(字段名相同)
php中判断版本号是否连续
離線數倉和bi開發的實踐和思考
The boss said: whoever wants to use double to define the amount of goods, just pack up and go
2021-07-19c CAD secondary development creates multiple line segments
Oracle general ledger balance table GL for foreign currency bookkeeping_ Balance change (Part 1)