问题
UserWarning: Detected call of lr_scheduler.step() before optimizer.step(). In PyTorch 1.1.0 and later, you should call them in the opposite order: optimizer.step() before lr_scheduler.step(). Failure to do this will result in PyTorch skipping the first value of the learning rate schedule. See more details at https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate warnings.warn("Detected call of lr_scheduler.step() before optimizer.step().
正常在训练的时候,对于优化器和参数更新的顺序这样就可以了
total_loss.backward()
self.optimizer.step()
self.scheduler.step()
方案
但是在使用amp进行混合精度训练出现的一个warning,调换代码顺序也不work。
这个警告是怎么产生的?是由于在算梯度的时候出现了nan,所以这一次更新会跳过,但是参数仍旧会更新所以出现了问题,按下面的做法加一个判断就行了。
self.optim

文章讨论了在PyTorch中,尤其是在使用自动混合精度(AMP)训练时,遇到的警告关于optimizer.step()和lr_scheduler.step()的调用顺序问题。警告源于可能的NaN梯度导致学习率更新被跳过。解决方案是调整计算梯度和优化器更新的顺序,并在适当情况下处理学习率调度。

5440

被折叠的 条评论
为什么被折叠?



