TorchLean API

NN.API.Trainer.Scheduler

Learning-Rate Schedules #

Pure learning-rate schedules used by TorchLean training loops and examples. A Config describes a schedule, and learningRateAt evaluates it at an optimizer step.

References #

Small learning-rate scheduler surface for higher-level training code.

This file keeps the interface compact: a Config is just a description of a schedule, and learningRateAt config stepIndex computes the learning rate at that optimizer step or epoch.

PyTorch mapping #

Config.step and Config.exponential correspond to the schedule math of:

  • torch.optim.lr_scheduler.StepLR
  • torch.optim.lr_scheduler.ExponentialLR

Config.warmupCosine is the schedule commonly used for Transformer pretraining: a short linear warm-up followed by cosine decay to a nonzero floor.

  • constant (learningRate : Float) : Config
  • step (baseLearningRate : Float) (stepSize : Nat) (decayFactor : Float := 0.1) : Config
  • exponential (baseLearningRate decayFactor : Float) : Config
  • warmupCosine (peakLearningRate minimumLearningRate : Float) (warmupSteps totalSteps : Nat) : Config
Instances For

    Constant learning-rate schedule.

    Instances For
      def TorchLean.Trainer.Scheduler.step (baseLearningRate : Float) (stepSize : Nat) (decayFactor : Float := 0.1) :

      Step decay learning-rate schedule.

      Instances For
        def TorchLean.Trainer.Scheduler.exponential (baseLearningRate decayFactor : Float) :

        Exponential learning-rate schedule.

        Instances For
          def TorchLean.Trainer.Scheduler.warmupCosine (peakLearningRate minimumLearningRate : Float) (warmupSteps totalSteps : Nat) :

          Linearly warm up to peakLearningRate, then follow a cosine curve down to minimumLearningRate.

          warmupSteps counts optimizer updates. The first update uses peakLearningRate / warmupSteps, and the last warm-up update reaches peakLearningRate. Once totalSteps updates have been scheduled, the learning rate remains at minimumLearningRate. A warm-up longer than the run is clamped to totalSteps. When totalSteps = 0, no update belongs to the schedule and learningRateAt returns minimumLearningRate.

          Instances For

            Reject a learning rate that is not a finite number at or above zero.

            Instances For

              Reject a decay factor outside [0, 1]. A factor above one grows the learning rate instead of decaying it, which in practice is a typo rather than an intention.

              Instances For

                Validate a learning-rate schedule before it is attached to optimizer state.

                The checks keep every scheduled rate finite and nonnegative. Step and exponential schedules use decay factors in [0, 1]; a zero step size is rejected instead of silently changing the schedule to a constant rate.

                Instances For

                  Also check that the largest scheduled rate remains finite in binary32 training.

                  Instances For

                    Learning rate at a given step or epoch index.

                    Instances For