python 多步LR在PyTorch中的工作原理

hwamh0ep  于 2023-02-11  发布在  Python
关注(0)|答案(1)|浏览(152)

我是PyTorch的新手,我正在做一个玩具例子来理解权重衰减是如何影响传递到优化器的学习速率的。当我使用MultiStepLR时,我希望在给定的时期数内降低学习速率,但是,它并没有像我预期的那样工作。我做错了什么?

import random
import torch
import pandas as pd
import numpy as np
from torch import nn
from torch.utils.data import Dataset,DataLoader,TensorDataset
from torchvision import datasets, transforms

model = nn.Sequential(nn.Linear(n_input, n_hidden),
                      nn.ReLU(),
                      nn.Linear(n_hidden, n_out),
                      nn.ReLU())
loss_function = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.1)

scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[2,4], gamma=0.1)
for e in range(5):
    scheduler.step()
    print(e, ' : lr', scheduler.get_lr()[0],"\n")

0  : lr 0.1 

1  : lr 0.0010000000000000002 

2  : lr 0.010000000000000002 

3  : lr 0.00010000000000000003 

4  : lr 0.0010000000000000002

学习速率的预期行为为[0.1, 0.1, 0.01, 0.01, 0.001]

xxhby3vn

xxhby3vn1#

当运行你的代码时,我得到以下警告:

/home/user/anaconda3/envs/eai/lib/python3.8/site-packages/torch/optim/lr_scheduler.py:138: 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()`. "

/home/user/anaconda3/envs/eai/lib/python3.8/site-packages/torch/optim/lr_scheduler.py:429: UserWarning: To get the last learning rate computed by the scheduler, please use `get_last_lr()`.
  warnings.warn("To get the last learning rate computed by the scheduler, "

可以按照警告消息并使用get_last_lr修复代码:

import random
import torch
import pandas as pd
import numpy as np
from torch import nn
from torch.utils.data import Dataset,DataLoader,TensorDataset
from torchvision import datasets, transforms

model = nn.Sequential(nn.Linear(4, 4),
                      nn.ReLU(),
                      nn.Linear(4, 4),
                      nn.ReLU())
loss_function = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.1)

scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[2,4], gamma=0.1)
for e in range(5):
    scheduler.step()
    print(e, ' : lr', scheduler.get_last_lr(),"\n")

输出:

0  : lr [0.1] 

1  : lr [0.010000000000000002] 

2  : lr [0.010000000000000002] 

3  : lr [0.0010000000000000002] 

4  : lr [0.0010000000000000002]

如果您希望学习率在每个时期降低,则应删除milestones参数。

相关问题