Class LambdaLRScheduler
- Namespace
- AiDotNet.LearningRateSchedulers
- Assembly
- AiDotNet.dll
Sets the learning rate using a user-defined lambda function.
public class LambdaLRScheduler : LearningRateSchedulerBase, ILearningRateScheduler
- Inheritance
-
LambdaLRScheduler
- Implements
- Inherited Members
Examples
// Custom schedule: lr = base_lr * (0.95 ^ epoch)
var scheduler = new LambdaLRScheduler(
baseLearningRate: 0.1,
lrLambda: step => Math.Pow(0.95, step)
);
// Warmup for 10 steps, then constant
var warmupScheduler = new LambdaLRScheduler(
baseLearningRate: 0.001,
lrLambda: step => step < 10 ? (step + 1) / 10.0 : 1.0
);
Remarks
LambdaLR provides maximum flexibility by allowing you to define any learning rate schedule as a function of the current step. The lambda function takes the step number and returns a multiplier that is applied to the base learning rate.
For Beginners: This scheduler lets you define your own custom learning rate schedule using a function. The function receives the current step number and returns a value that gets multiplied with the initial learning rate. For example, returning 0.5 would give half the initial learning rate. This is useful when you want a schedule that doesn't fit any of the standard patterns.
Constructors
LambdaLRScheduler(double, Func<int, double>, double)
Initializes a new instance of the LambdaLRScheduler class.
public LambdaLRScheduler(double baseLearningRate, Func<int, double> lrLambda, double minLearningRate = 0)
Parameters
baseLearningRatedoubleThe initial learning rate.
lrLambdaFunc<int, double>A function that takes the step number and returns a multiplier for the base learning rate.
minLearningRatedoubleMinimum learning rate floor. Default: 0
Methods
ComputeLearningRate(int)
Computes the learning rate for a given step.
protected override double ComputeLearningRate(int step)
Parameters
stepintThe step number.
Returns
- double
The computed learning rate.
GetState()
Gets the scheduler state for serialization/checkpointing.
public override Dictionary<string, object> GetState()
Returns
- Dictionary<string, object>
A dictionary containing the scheduler state.