def adjust_learning_rate(optimizer, lr, gamma, step):
lr = lr * (gamma ** (step))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
调用
optimizer = optim.Adam(net.parameters(),lr=lr)
for epoch in range(Start_iter,max_epoch):
adjust_learning_rate(optimizer,lr,0.9,epoch)
lr = adjust_learning_rate(optimizer,lr,0.9,epoch)
print("lr:",lr)
2
def adjust_learning_rate(optimizer, cur_epoch, base_lr, lr_schedule):
"""
Sets the learning rate
"""
lr = 0
for i, e in enumerate(lr_schedule):
if cur_epoch < e:
lr = base_lr * (0.1 ** i)
break
if lr == 0:
lr = base_lr
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
调用
# update lr
learning_rate = 0.001
lr_schedule = [5, 30, 80, 130] # 最后一个数必须大于max_epoch
lr = adjust_learning_rate(optimizer, epoch, learning_rate, lr_schedule)
3.按步数
def adjust_learning_rate(optimizer, gamma, lr, step):
"""Sets the learning rate to the initial LR decayed by 10 at every
specified step
# Adapted from PyTorch Imagenet example:
# https://github.com/pytorch/examples/blob/master/imagenet/main.py
"""
lr = lr * (gamma ** (step))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
调用
epoch_size = len(train_dataset) // train_batch_size
max_iter = max_epoch * epoch_size
# max_iter 可自己设定
lr_steps = [8000,100000,150000]
step_index = 0
for iteration in range(start_iter, max_iter):
if iteration in cfg['lr_steps']:
step_index += 1
adjust_learning_rate(optimizer, 0.1, lr,step_index)
# 加载数据