当前位置:网站首页>【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可以避免这一点。
大概尝试的思路就这么多, 有遗漏的请路过大佬在评论区指正。
如上
边栏推荐
- Oracle RMAN automatic recovery script (migration of production data to test)
- oracle apex ajax process + dy 校验
- Message queue fnd in Oracle EBS_ msg_ pub、fnd_ Application of message in pl/sql
- Oracle APEX 21.2 installation et déploiement en une seule touche
- RMAN增量恢复示例(1)-不带未备份的归档日志
- ARP attack
- Oracle EBs and apex integrated login and principle analysis
- ORACLE EBS ADI 开发步骤
- Oracle 11g sysaux table space full processing and the difference between move and shrink
- UEditor . Net version arbitrary file upload vulnerability recurrence
猜你喜欢
DNS攻击详解
Sqli labs customs clearance summary-page4
IDEA2020中测试PySpark的运行出错
CAD secondary development object
Message queue fnd in Oracle EBS_ msg_ pub、fnd_ Application of message in pl/sql
Sqli-labs customs clearance (less18-less20)
Practice and thinking of offline data warehouse and Bi development
Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK
Sqli-labs customs clearance (less2-less5)
Ceaspectuss shipping company shipping artificial intelligence products, anytime, anywhere container inspection and reporting to achieve cloud yard, shipping company intelligent digital container contr
随机推荐
ORACLE EBS ADI 开发步骤
Oracle general ledger balance table GL for foreign currency bookkeeping_ Balance change (Part 1)
Principle analysis of spark
Oracle RMAN automatic recovery script (migration of production data to test)
php中的数字金额转换大写数字
SSM实验室设备管理
A summary of a middle-aged programmer's study of modern Chinese history
Brief analysis of PHP session principle
MySQL无order by的排序规则因素
Oracle apex 21.2 installation and one click deployment
[leetcode question brushing day 35] 1060 Missing element in ordered array, 1901 Find the peak element, 1380 Lucky number in matrix
oracle EBS标准表的后缀解释说明
ORACLE APEX 21.2安裝及一鍵部署
mapreduce概念和案例(尚硅谷学习笔记)
SQLI-LABS通關(less6-less14)
SQLI-LABS通关(less1)
Sqli labs customs clearance summary-page4
叮咚,Redis OM对象映射框架来了
TCP attack
Message queue fnd in Oracle EBS_ msg_ pub、fnd_ Application of message in pl/sql