在使用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