当前位置:   article > 正文

torch.optim.lr_scheduler.CosineAnnealingWarmRestarts、OneCycleLR定义与使用

torch.optim.lr_scheduler.cosineannealingwarmrestarts

torch中有多种余弦退火学习率调整方法,包括:OneCycleLR、CosineAnnealingLR和CosineAnnealingWarmRestarts。

CosineAnnealingWarmRestarts(带预热的余弦退火)学习率方法定义

  1. torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer, T_0, T_mult=1, \
  2. eta_min=0, last_epoch=- 1, verbose=False)

CosineAnnealingWarmRestarts参数:

- optimizer (Optimizer) – Wrapped optimizer. 优化器

- T_0 (int) – Number of iterations for the first restart.学习率第一次回到初始值的epoch位置

- T_mult (int, optional) – A factor increases T_{i} mult应该是multiply的意思,即T_mult=2意思是周期翻倍,第一个周期是1,则第二个周期是2,第三个周期是4。

- eta_min (float, optional) – Minimum learning rate. Default: 0.

- last_epoch (int, optional) – The index of last epoch. Default: -1.

- verbose (bool) – If True, prints a message to stdout for each update. Default: False.

CosineAnnealingWarmRestarts使用

scheduler 初始化之后,在batch中使用scheduler.step()

  1. import torch
  2. from torch.optim.lr_scheduler import CosineAnnealingLR, CosineAnnealingWarmRestarts,StepLR, OneCycleLR
  3. import torch.nn as nn
  4. from torchvision.models import resnet18
  5. import matplotlib.pyplot as plt
  6. model = resnet18(pretrained=False)
  7. optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
  8. mode = 'cosineAnnWarm'
  9. #mode = 'cosineAnn'
  10. if mode == 'cosineAnn':
  11.     scheduler = CosineAnnealingLR(optimizer, T_max=5, eta_min=0.001)
  12. elif mode == 'cosineAnnWarm':
  13.     scheduler = CosineAnnealingWarmRestarts(optimizer, T_0=5, T_mult=2)
  14. plt.figure()
  15. max_epoch = 50
  16. iters = 200
  17. cur_lr_list = []
  18. for epoch in range(max_epoch):
  19.     for batch in range(iters):
  20.         optimizer.step()
  21.         scheduler.step()
  22.     cur_lr = optimizer.param_groups[-1]['lr']
  23.     cur_lr_list.append(cur_lr)
  24.     print('Cur lr:', cur_lr)
  25. x_list = list(range(len(cur_lr_list)))
  26. plt.plot(x_list, cur_lr_list)
  27. plt.show()

在这里插入图片描述

 OneCycleLR使用

        pytorch版本>=1.7

  1. mode = 'OneCycleLR'
  2. #mode = 'cosineAnn'
  3. max_epoch = 500
  4. iters = 200
  5. if mode == 'cosineAnn':
  6.     scheduler = CosineAnnealingLR(optimizer, T_max=5, eta_min=0.001)
  7. elif mode == 'cosineAnnWarm':
  8.     scheduler = CosineAnnealingWarmRestarts(optimizer, T_0=5, T_mult=2)
  9. elif mode == 'OneCycleLR':
  10.     scheduler = OneCycleLR(optimizer, max_lr=0.5, steps_per_epoch=iters, epochs=max_epoch, pct_start=o.3)
  11. plt.figure()
  12. cur_lr_list = []
  13. for epoch in range(max_epoch):
  14.     for batch in range(iters):
  15.         optimizer.step()
  16.         scheduler.step()
  17.     cur_lr = optimizer.param_groups[-1]['lr']
  18.     cur_lr_list.append(cur_lr)
  19.     #print('Cur lr:', cur_lr)
  20. x_list = list(range(len(cur_lr_list)))
  21. plt.plot(x_list, cur_lr_list)
  22. plt.show()

在这里插入图片描述

        在目前的pytorch1.9版本中新添加了一个three_phase参数,当这个three_phase=True

scheduler=torch.optim.lr_scheduler.OneCycleLR(optimizer,max_lr=0.9,steps_per_epoch=iters, epochs=max_epoch, pct_start=o.3,three_phase=True)

        得到的学习率变成下图:对称+陡降

————————————————
修改自:https://blog.csdn.net/qq_30129009/article/details/121732567

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/328463
推荐阅读
相关标签
  

闽ICP备14008679号