WeightDecayScheduler#
- class WeightDecayScheduler(base_value=0.04, final_value=0.2, warmup_ratio=1.0)[source]#
Bases:
HookBaseApply a cosine schedule to optimizer parameter-group weight decay.
In
before_trainit builds aCosineSchedulerspanningcfg.scheduler.total_steps * warmup_ratioiterations (advanced to the current step so resumes continue smoothly). Inbefore_stepit steps the schedule and writes the resulting weight decay onto every optimizer group flaggedapply_wd=True(groups flaggedFalsekeep their original, e.g.0.0), then logs it to the writer asparams/wd. Registered asWeightDecayScheduler.- Parameters:
base_value (float) – Starting weight decay. Defaults to
0.04.final_value (float) – Ending weight decay after the schedule. Defaults to
0.2.warmup_ratio (float) – Fraction of
cfg.scheduler.total_stepsover which the cosine schedule runs (scales the schedule length). Defaults to1.0.
Note
The
apply_wdflags are produced byWeightDecayExclusion; without it every group defaults toapply_wd=Trueand is scheduled. Place this hook afterWeightDecayExclusion.Example
Add to
cfg.hooksafterWeightDecayExclusion; it cosine-schedules weight decay each step:hooks = [ dict(type="WeightDecayExclusion"), dict(type="WeightDecayScheduler", base_value=0.04, final_value=0.2, warmup_ratio=1.0), ] # → every step (before_step) sets param_group["weight_decay"] (cosine # 0.04 -> 0.2) on every apply_wd=True group and writes the scalar # "params/wd" to the writer; apply_wd=False groups keep weight_decay=0.0