Transformers还提供了自己的调度器来学习速率,如get_constant_schedule、get_constant_schedule_with_warmup等。它们再次返回torch.optim.lr_scheduler.LambdaLR (火炬调度器)。这两者之间唯一的区别是warmup_steps吗?
我们如何创建一个自定义的基于转换器的调度器,类似于lr_scheduler.MultiplicativeLR、lr_scheduler.StepLR、lr_scheduler.ExponentialLR等其他torch调度器
发布于 2021-07-26 14:06:09
只需在接受优化器及其状态的类中创建一个函数,并编辑其param_groups中的值,即可创建自定义调度器。
要理解如何在类中构造它,只需看看Pytorch如何创建其调度器并使用相同的函数,只需根据您的喜好更改功能即可。
我发现的Permalink是一个很好的参考资料,它位于here之上
在注释后编辑:
这类似于您可以使用的模板
from torch.optim import lr_scheduler
class MyScheduler(lr_scheduler._LRScheduler # Optional inheritance):
def __init__(self, # optimizer, epoch, step size, whatever you need as input to lr scheduler, you can even use vars from LRShceduler Class that you can inherit from etc.):
super(MyScheduler, self).__init__(optimizer, last_epoch, verbose)
# Put variables that you will need for updating scheduler like gamma, optimizer, or step size etc.
self.optimizer = optimizer
def get_lr(self):
# How will you use the above variables to update the optimizer
for group in self.optimizer.param_groups:
group["lr"] = # Fill this out with updated optimizer
return self.optimizer您可以添加更多功能以增强功能。或者你可以只使用一个函数来更新你的学习率。这将接收优化器并将其更改为optimizer.param_groups,然后返回新的优化器。
https://stackoverflow.com/questions/68523070
复制相似问题