You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何固定Huggingface Trainer的学习率?warmup_steps配置后仍下降的问题

问题根源

Hugging Face的Seq2SeqTrainingArguments默认使用线性学习率调度器(lr_scheduler_type="linear")。这个调度器的逻辑是:先通过warmup_steps把学习率从0提升到你设定的learning_rate,之后就会从这个值开始线性衰减,直到训练结束时降到0。你设置warmup_steps=1只是让warmup阶段仅执行1步,但warmup结束后,学习率还是会进入预设的线性衰减流程,这就是你看到学习率持续下降的原因。

固定学习率的解决方案

要实现warmup后保持学习率固定,需要修改lr_scheduler_type参数,有两种适配场景的选择:

  • 如果不需要warmup阶段,直接设置lr_scheduler_type="constant":学习率从训练开始就维持你设定的1.25e-5。
  • 如果需要保留1步warmup,之后固定学习率,设置lr_scheduler_type="constant_with_warmup":完成1步warmup后,学习率会一直保持在1.25e-5,不再衰减。

结合你的需求,推荐第二种方案,修改后的参数配置如下:

Seq2SeqTrainingArguments(
    output_dir                   = "./out", 
    overwrite_output_dir         = True,
    do_train                     = True,
    do_eval                      = True,
    
    per_device_train_batch_size  = 2, 
    gradient_accumulation_steps  = 4,
    per_device_eval_batch_size   = 8, 
    
    learning_rate                = 1.25e-5,
    warmup_steps                 = 1,
    lr_scheduler_type            = "constant_with_warmup",  # 新增该参数
    
    save_total_limit             = 1,
       
    evaluation_strategy          = "epoch",
    save_strategy                = "epoch",
    logging_strategy             = "epoch",  
    num_train_epochs             = 5,   
    
    gradient_checkpointing       = True,
    fp16                         = True,    
        
    predict_with_generate        = True,
    generation_max_length        = 225,
          
    report_to                    = ["tensorboard"],
    load_best_model_at_end       = True,
    metric_for_best_model        = "wer",
    greater_is_better            = False,
    push_to_hub                  = False,
)

这样配置后,训练会在1步warmup完成后,始终保持1.25e-5的学习率直到训练结束。

内容的提问来源于stack exchange,提问作者user3668129

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 19:32:48