当前位置:   article > 正文

【笔记】pytorch 中学习率调整函数(手动定义一个学习率衰减函数) : torch.optim.lr_scheduler ..._学习率函数

学习率函数

eg:

注1:

注2:

 注3:

正文:

 

  1. import torch
  2. import torch.nn as nn
  3. from torch.optim.lr_scheduler import LambdaLR
  4. import itertools
  5. initial_lr = 0.1
  6. class model(nn.Module):
  7. def __init__(self):
  8. super().__init__()
  9. self.conv1 = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3)
  10. self.conv2 = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3)
  11. def forward(self, x):
  12. pass
  13. net_1 = model()
  14. net_2 = model()
  15. optimizer_1 = torch.optim.Adam(net_1.parameters(), lr = initial_lr)
  16. print("******************optimizer_1*********************")
  17. print("optimizer_1.defaults:", optimizer_1.defaults)
  18. print("optimizer_1.param_groups长度:", len(optimizer_1.param_groups))
  19. print("optimizer_1.param_groups一个元素包含的键:", optimizer_1.param_groups[0].keys())
  20. print()
  21. optimizer_2 = torch.optim.Adam([*net_1.parameters(), *net_2.parameters()], lr = initial_lr)
  22. # optimizer_2 = torch.opotim.Adam(itertools.chain(net_1.parameters(), net_2.parameters())) # 和上一行作用相同
  23. print("******************optimizer_2*********************")
  24. print("optimizer_2.defaults:", optimizer_2.defaults)
  25. print("optimizer_2.param_groups长度:", len(optimizer_2.param_groups))
  26. print("optimizer_2.param_groups一个元素包含的键:", optimizer_2.param_groups[0].keys())
  27. print()
  28. optimizer_3 = torch.optim.Adam([{"params": net_1.parameters()}, {"params": net_2.parameters()}], lr = initial_lr)
  29. print("******************optimizer_3*********************")
  30. print("optimizer_3.defaults:", optimizer_3.defaults)
  31. print("optimizer_3.param_groups长度:", len(optimizer_3.param_groups))
  32. print("optimizer_3.param_groups一个元素包含的键:", optimizer_3.param_groups[0].keys())

 输出为:

  1. ******************optimizer_1*********************
  2. optimizer_1.defaults: {'lr': 0.1, 'betas': (0.9, 0.999), 'eps': 1e-08, 'weight_decay': 0, 'amsgrad': False}
  3. optimizer_1.param_groups长度: 1
  4. optimizer_1.param_groups一个元素包含的键: dict_keys(['params', 'lr', 'betas', 'eps', 'weight_decay', 'amsgrad'])
  5. ******************optimizer_2*********************
  6. optimizer_2.defaults: {'lr': 0.1, 'betas': (0.9, 0.999), 'eps': 1e-08, 'weight_decay': 0, 'amsgrad': False}
  7. optimizer_2.param_groups长度: 1
  8. optimizer_2.param_groups一个元素包含的键: dict_keys(['params', 'lr', 'betas', 'eps', 'weight_decay', 'amsgrad'])
  9. ******************optimizer_3*********************
  10. optimizer_3.defaults: {'lr': 0.1, 'betas': (0.9, 0.999), 'eps': 1e-08, 'weight_decay': 0, 'amsgrad': False}
  11. optimizer_3.param_groups长度: 2
  12. optimizer_3.param_groups一个元素包含的键: dict_keys(['params', 'lr', 'betas', 'eps', 'weight_decay', 'amsgrad'])

2.1   torch.optim.lr_scheduler.LambdaLR

  1. import torch
  2. import torch.nn as nn
  3. from torch.optim.lr_scheduler import LambdaLR
  4. initial_lr = 0.1
  5. class model(nn.Module):
  6. def __init__(self):
  7. super().__init__()
  8. self.conv1 = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3)
  9. def forward(self, x):
  10. pass
  11. net_1 = model()
  12. optimizer_1 = torch.optim.Adam(net_1.parameters(), lr = initial_lr)
  13. scheduler_1 = LambdaLR(optimizer_1, lr_lambda=lambda epoch: 1/(epoch+1))
  14. print("初始化的学习率:", optimizer_1.defaults['lr'])
  15. for epoch in range(1, 11):
  16. # train
  17. optimizer_1.zero_grad()
  18. optimizer_1.step()
  19. print("第%d个epoch的学习率:%f" % (epoch, optimizer_1.param_groups[0]['lr']))
  20. scheduler_1.step()

 输出:

  1. 初始化的学习率: 0.1
  2. 1个epoch的学习率:0.100000
  3. 2个epoch的学习率:0.050000
  4. 3个epoch的学习率:0.033333
  5. 4个epoch的学习率:0.025000
  6. 5个epoch的学习率:0.020000
  7. 6个epoch的学习率:0.016667
  8. 7个epoch的学习率:0.014286
  9. 8个epoch的学习率:0.012500
  10. 9个epoch的学习率:0.011111
  11. 10个epoch的学习率:0.010000

补充:
cycleGAN中使用torch.optim.lr_scheduler.LambdaLR实现了前niter个epoch用initial_lr为学习率,之后的niter_decay个epoch线性衰减lr,直到最后一个epoch衰减为0。详情参考:https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/models/networks.py 的第52~55行。

2.2   torch.optim.lr_scheduler.StepLR

 下面举例说明:

  1. import torch
  2. import torch.nn as nn
  3. from torch.optim.lr_scheduler import StepLR
  4. import itertools
  5. initial_lr = 0.1
  6. class model(nn.Module):
  7. def __init__(self):
  8. super().__init__()
  9. self.conv1 = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3)
  10. def forward(self, x):
  11. pass
  12. net_1 = model()
  13. optimizer_1 = torch.optim.Adam(net_1.parameters(), lr = initial_lr)
  14. scheduler_1 = StepLR(optimizer_1, step_size=3, gamma=0.1)
  15. print("初始化的学习率:", optimizer_1.defaults['lr'])
  16. for epoch in range(1, 11):
  17. # train
  18. optimizer_1.zero_grad()
  19. optimizer_1.step()
  20. print("第%d个epoch的学习率:%f" % (epoch, optimizer_1.param_groups[0]['lr']))
  21. scheduler_1.step()

 

2.3   torch.optim.lr_scheduler.MultiStepLR 

  1. import torch
  2. import torch.nn as nn
  3. from torch.optim.lr_scheduler import MultiStepLR
  4. import itertools
  5. initial_lr = 0.1
  6. class model(nn.Module):
  7. def __init__(self):
  8. super().__init__()
  9. self.conv1 = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3)
  10. def forward(self, x):
  11. pass
  12. net_1 = model()
  13. optimizer_1 = torch.optim.Adam(net_1.parameters(), lr = initial_lr)
  14. scheduler_1 = MultiStepLR(optimizer_1, milestones=[3, 7], gamma=0.1)
  15. print("初始化的学习率:", optimizer_1.defaults['lr'])
  16. for epoch in range(1, 11):
  17. # train
  18. optimizer_1.zero_grad()
  19. optimizer_1.step()
  20. print("第%d个epoch的学习率:%f" % (epoch, optimizer_1.param_groups[0]['lr']))
  21. scheduler_1.step()

 输出为:

  1. 初始化的学习率: 0.1
  2. 1个epoch的学习率:0.100000
  3. 2个epoch的学习率:0.100000
  4. 3个epoch的学习率:0.100000
  5. 4个epoch的学习率:0.010000
  6. 5个epoch的学习率:0.010000
  7. 6个epoch的学习率:0.010000
  8. 7个epoch的学习率:0.010000
  9. 8个epoch的学习率:0.001000
  10. 9个epoch的学习率:0.001000
  11. 10个epoch的学习率:0.001000

 

 

  1. import torch
  2. import torch.nn as nn
  3. from torch.optim.lr_scheduler import ExponentialLR
  4. import itertools
  5. initial_lr = 0.1
  6. class model(nn.Module):
  7. def __init__(self):
  8. super().__init__()
  9. self.conv1 = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3)
  10. def forward(self, x):
  11. pass
  12. net_1 = model()
  13. optimizer_1 = torch.optim.Adam(net_1.parameters(), lr = initial_lr)
  14. scheduler_1 = ExponentialLR(optimizer_1, gamma=0.1)
  15. print("初始化的学习率:", optimizer_1.defaults['lr'])
  16. for epoch in range(1, 11):
  17. # train
  18. optimizer_1.zero_grad()
  19. optimizer_1.step()
  20. print("第%d个epoch的学习率:%f" % (epoch, optimizer_1.param_groups[0]['lr']))
  21. scheduler_1.step()
  1. 初始化的学习率: 0.1
  2. 1个epoch的学习率:0.100000
  3. 2个epoch的学习率:0.010000
  4. 3个epoch的学习率:0.001000
  5. 4个epoch的学习率:0.000100
  6. 5个epoch的学习率:0.000010
  7. 6个epoch的学习率:0.000001
  8. 7个epoch的学习率:0.000000
  9. 8个epoch的学习率:0.000000
  10. 9个epoch的学习率:0.000000
  11. 10个epoch的学习率:0.000000

  1. import torch
  2. import torch.nn as nn
  3. from torch.optim.lr_scheduler import CosineAnnealingLR
  4. import itertools
  5. import matplotlib.pyplot as plt
  6. initial_lr = 0.1
  7. class model(nn.Module):
  8. def __init__(self):
  9. super().__init__()
  10. self.conv1 = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3)
  11. def forward(self, x):
  12. pass
  13. net_1 = model()
  14. optimizer_1 = torch.optim.Adam(net_1.parameters(), lr = initial_lr)
  15. scheduler_1 = CosineAnnealingLR(optimizer_1, T_max=20)
  16. print("初始化的学习率:", optimizer_1.defaults['lr'])
  17. lr_list = [] # 把使用过的lr都保存下来,之后画出它的变化
  18. for epoch in range(1, 101):
  19. # train
  20. optimizer_1.zero_grad()
  21. optimizer_1.step()
  22. print("第%d个epoch的学习率:%f" % (epoch, optimizer_1.param_groups[0]['lr']))
  23. lr_list.append(optimizer_1.param_groups[0]['lr'])
  24. scheduler_1.step()
  25. # 画出lr的变化
  26. plt.plot(list(range(1, 101)), lr_list)
  27. plt.xlabel("epoch")
  28. plt.ylabel("lr")
  29. plt.title("learning rate's curve changes as epoch goes on!")
  30. plt.show()

 

 下面举例说明:

  1. import torch
  2. import torch.nn as nn
  3. from torch.optim.lr_scheduler import ReduceLROnPlateau
  4. import itertools
  5. initial_lr = 0.1
  6. class model(nn.Module):
  7. def __init__(self):
  8. super().__init__()
  9. self.conv1 = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3)
  10. def forward(self, x):
  11. pass
  12. net_1 = model()
  13. optimizer_1 = torch.optim.Adam(net_1.parameters(), lr = initial_lr)
  14. scheduler_1 = ReduceLROnPlateau(optimizer_1, mode='min', factor=0.1, patience=2)
  15. print("初始化的学习率:", optimizer_1.defaults['lr'])
  16. for epoch in range(1, 15):
  17. # train
  18. test = 2
  19. optimizer_1.zero_grad()
  20. optimizer_1.step()
  21. print("第%d个epoch的学习率:%f" % (epoch, optimizer_1.param_groups[0]['lr']))
  22. scheduler_1.step(test)
  1. 初始化的学习率: 0.1
  2. 1个epoch的学习率:0.100000
  3. 2个epoch的学习率:0.100000
  4. 3个epoch的学习率:0.100000
  5. 4个epoch的学习率:0.100000
  6. 5个epoch的学习率:0.010000
  7. 6个epoch的学习率:0.010000
  8. 7个epoch的学习率:0.010000
  9. 8个epoch的学习率:0.001000
  10. 9个epoch的学习率:0.001000
  11. 10个epoch的学习率:0.001000
  12. 11个epoch的学习率:0.000100
  13. 12个epoch的学习率:0.000100
  14. 13个epoch的学习率:0.000100
  15. 14个epoch的学习率:0.000010

 附:

pytorch中的学习率调整函数 - 慢行厚积 - 博客园
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/328451
推荐阅读
相关标签
  

闽ICP备14008679号