欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Detected call of `lr_scheduler.step()` before `optimizer.step()`.

发布时间:2024/7/23 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Detected call of `lr_scheduler.step()` before `optimizer.step()`. 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

在使用pytorch的指数衰减学习率时,出现报错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.

原因是如报错所说,在“optimizer.step()”之前对“lr_scheduler.step()”的进行了调用,如下错误的代码所示:

for i in range(epoch):net.scheduler.step()net.set_mode_train(True)for j, (x, y) in enumerate(train_loader):cost_pred, err = net.fit(x, y)net.epoch = i

所以应该把lr_scheduler.step()放在每次epoch训练完成之后:

for i in range(epoch):net.set_mode_train(True)for j, (x, y) in enumerate(train_loader):cost_pred, err = net.fit(x, y)net.scheduler.step()net.epoch = i

 

总结

以上是生活随笔为你收集整理的Detected call of `lr_scheduler.step()` before `optimizer.step()`.的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。