当使用pytorch1.1.0及其以上版本时,会报以下警告
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
“https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate”, UserWarning)
只要把lr_scheduler放在trian() 和 valid()步骤之后就可以。如:
for epoch in range(begin_epoch, end_epoch):
#training
train( train_loader, model, criterion, optimizer, epoch)
# evaluate
validate(valid_loader, valid_dataset, model, criterion)
lr_scheduler.step() ##把 lr_scheduler.step() 放在这里,即训练和评估之后即可