【问题标题】:Difference between transformers schedulers and Pytorch schedulersTransformers 调度器和 Pytorch 调度器的区别
【发布时间】:2021-10-01 23:47:45
【问题描述】:

Transformers 还提供自己的学习率调度程序,例如 get_constant_scheduleget_constant_schedule_with_warmup 等。它们再次返回 torch.optim.lr_scheduler.LambdaLR(火炬调度程序)。 warmup_steps 是两者之间的唯一区别吗?

我们如何创建一个自定义的基于转换器的调度器,类似于lr_scheduler.MultiplicativeLRlr_scheduler.StepLRlr_scheduler.ExponentialLR 等其他 Torch 调度器?

【问题讨论】:

    标签: python pytorch huggingface-transformers


    【解决方案1】:

    您可以通过在类中创建一个函数来创建自定义调度器,该函数接收优化器及其状态字典并编辑其 param_groups 中的值。

    要了解如何在一个类中构造它,只需看看 Pytorch 如何创建其调度程序并使用相同的功能,只需根据自己的喜好更改功能即可。

    我发现的作为一个很好参考的永久链接超过了here

    在 cmets 之后编辑:

    这就像一个你可以使用的模板

    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[0]["lr"] 并返回新的优化器。

    【讨论】:

    • 您能否使用示例代码说明如何创建自定义的基于转换器的调度程序?另外,您没有回答问题的第一部分。
    • 我对我的答案进行了编辑。我也不完全确定第一个问题的答案是什么。我只知道第二个。
    • 部分回答
    猜你喜欢
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 2021-03-13
    • 2019-04-13
    • 2018-07-17
    相关资源
    最近更新 更多