aipixel / AEMatter

Another matter.
GNU General Public License v2.0
52 stars 2 forks source link

训练学习率设置 #11

Closed astro-fits closed 3 months ago

astro-fits commented 3 months ago

请教作者 关于训练的时候 学习率的策略。

我目前使用 cosine学习率(学习率峰值为 2.5e-6,半周期为60000 step)的策略来训练。 训练到数万step之后出错。 具体表现是 该step训练样本正常、output正常、 和前几个step相比计算的loss也正常。 但是在 loss.backward函数之后,打印 网络当前的梯度中 就已经出现 nan矩阵。 导致该step中 网络权重update之后全部变成nan。

而使用 with autograd.detect_anomaly():
loss.backward 检测之后,报错信息是: 'ScaledDotProductEfficientAttentionBackward0' returned nan values in its 0th output.

网上有人说 注意力机制中的softmax函数的问题。 github官网也有一些讨论: https://zhuanlan.zhihu.com/p/668904493 https://github.com/pytorch/pytorch/issues/41508#issuecomment-1723119580

然后我将学习率调小1个量级做测试,暂时没有发现异常。问题是可能会影响收敛。 不清楚这个问题的根源,梯度爆炸?

我的训练代码是自己根据其他matting网络改的。 不知作者有无同样经历,以及求问学习率设置。 多谢

Windaway commented 3 months ago

你好,在Pytorch2.0下的确出现了这个问题,同时我们发现在fp16/amp训练也有这个问题,这应该是attention处理的时候值超了,在pytorch1.12下用fp32问题少很多。另外,我们实际在aeal里用pre norm也能解决,记得那种配置训了下,似乎mse是2.27。

astro-fits commented 3 months ago

非常感谢您