I'm new to PyTorch
and am working on a toy example to understand how weight decay works in learning rate passed into the optimizer. When I use MultiStepLR
, I was expecting to decrease the learning rate in given epoch numbers, however, it does not work as I intended. What am I doing wrong?
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
The expected behavior in learning rate is [0.1, 0.1, 0.01, 0.01, 0.001]