Class FTRLOptimizer<T, TInput, TOutput>
- Namespace
- AiDotNet.Optimizers
- Assembly
- AiDotNet.dll
Represents a Follow The Regularized Leader (FTRL) optimizer for machine learning models.
public class FTRLOptimizer<T, TInput, TOutput> : GradientBasedOptimizerBase<T, TInput, TOutput>, IGradientBasedOptimizer<T, TInput, TOutput>, IOptimizer<T, TInput, TOutput>, IModelSerializer
Type Parameters
TThe numeric type used for calculations, typically float or double.
TInputTOutput
- Inheritance
-
OptimizerBase<T, TInput, TOutput>GradientBasedOptimizerBase<T, TInput, TOutput>FTRLOptimizer<T, TInput, TOutput>
- Implements
-
IGradientBasedOptimizer<T, TInput, TOutput>IOptimizer<T, TInput, TOutput>
- Inherited Members
- Extension Methods
Remarks
The FTRL optimizer is an online learning algorithm that adapts regularization in a per-coordinate fashion. It's particularly effective for sparse datasets and is widely used in click-through rate (CTR) prediction and other online learning scenarios.
For Beginners: FTRL is an advanced optimization technique that's good at handling large-scale, sparse data. It's often used in online advertising and recommendation systems.
Think of FTRL like a smart learning system that:
- Adjusts its learning speed for each feature independently
- Can handle situations where most features are zero (sparse data)
- Is good at balancing between finding a good solution and not overfitting
For example, in an online advertising system, FTRL might:
- Quickly learn which ad categories are important for a user
- Ignore or learn slowly from features that rarely appear
- Automatically adjust how much it learns from each new piece of data
This makes FTRL particularly good for systems that need to learn and predict in real-time with lots of data.
Constructors
FTRLOptimizer(IFullModel<T, TInput, TOutput>, FTRLOptimizerOptions<T, TInput, TOutput>?, IEngine?)
Initializes a new instance of the FTRLOptimizer class.
public FTRLOptimizer(IFullModel<T, TInput, TOutput> model, FTRLOptimizerOptions<T, TInput, TOutput>? options = null, IEngine? engine = null)
Parameters
modelIFullModel<T, TInput, TOutput>The model to optimize.
optionsFTRLOptimizerOptions<T, TInput, TOutput>The options for configuring the FTRL algorithm.
engineIEngineThe computation engine (CPU or GPU) for vectorized operations.
Remarks
For Beginners: This constructor sets up the FTRL optimizer with its initial configuration. You can customize various aspects of how it works, or use default settings if you're unsure.
Methods
Deserialize(byte[])
Deserializes the FTRL optimizer from a byte array.
public override void Deserialize(byte[] data)
Parameters
databyte[]The byte array containing the serialized optimizer state.
Remarks
For Beginners: This method takes a previously serialized optimizer state and reconstructs the optimizer from it. It's like restoring the optimizer's memory from a saved snapshot, allowing you to continue from where you left off or use a pre-trained optimizer.
Exceptions
- InvalidOperationException
Thrown when deserialization of optimizer options fails.
DisposeGpuState()
Disposes GPU-allocated optimizer state.
public override void DisposeGpuState()
GenerateGradientCacheKey(IFullModel<T, TInput, TOutput>, TInput, TOutput)
Generates a unique key for caching gradients.
protected override string GenerateGradientCacheKey(IFullModel<T, TInput, TOutput> model, TInput X, TOutput y)
Parameters
modelIFullModel<T, TInput, TOutput>The current model.
XTInputThe input data matrix.
yTOutputThe target values vector.
Returns
- string
A string that uniquely identifies the gradient for the given inputs.
Remarks
For Beginners: This method creates a unique identifier for storing and retrieving calculated gradients. It's like creating a label for a file drawer where you store specific calculations. This helps the optimizer avoid redoing calculations it has already performed, making it more efficient.
GetOptions()
Retrieves the current options of the FTRL optimizer.
public override OptimizationAlgorithmOptions<T, TInput, TOutput> GetOptions()
Returns
- OptimizationAlgorithmOptions<T, TInput, TOutput>
The current optimization algorithm options.
Remarks
For Beginners: This method lets you see what settings the optimizer is currently using. It's like checking the current settings on a machine to understand how it's configured.
InitializeAdaptiveParameters()
Initializes the adaptive parameters used in the FTRL algorithm.
protected override void InitializeAdaptiveParameters()
Remarks
For Beginners: This method sets up the initial learning rate and resets the time step. The learning rate determines how big the steps are when the algorithm is searching for the best solution.
InitializeGpuState(int, IDirectGpuBackend)
Initializes FTRL optimizer state on the GPU.
public override void InitializeGpuState(int parameterCount, IDirectGpuBackend backend)
Parameters
parameterCountintNumber of parameters.
backendIDirectGpuBackendGPU backend for memory allocation.
Optimize(OptimizationInputData<T, TInput, TOutput>)
Performs the main optimization process using the FTRL algorithm.
public override OptimizationResult<T, TInput, TOutput> Optimize(OptimizationInputData<T, TInput, TOutput> inputData)
Parameters
inputDataOptimizationInputData<T, TInput, TOutput>The input data for the optimization process.
Returns
- OptimizationResult<T, TInput, TOutput>
The result of the optimization process.
Remarks
For Beginners: This is the heart of the FTRL algorithm. It starts with a random solution and iteratively improves it. In each iteration, it: 1. Calculates how to improve the current solution (the gradient) 2. Updates the solution based on this calculation 3. Checks if this new solution is the best one found so far 4. Decides whether to stop or continue improving
It's like a climber trying to find the highest peak, taking steps based on the slope they feel under their feet.
DataLoader Integration: This method uses the DataLoader API for efficient batch processing. It creates a batcher using CreateBatcher(OptimizationInputData<T, TInput, TOutput>, int) and notifies the sampler of epoch starts using NotifyEpochStart(int).
ReverseUpdate(Vector<T>, Vector<T>)
Reverses an FTRL gradient update to recover original parameters.
public override Vector<T> ReverseUpdate(Vector<T> updatedParameters, Vector<T> appliedGradients)
Parameters
updatedParametersVector<T>Parameters after FTRL update
appliedGradientsVector<T>The gradients that were applied
Returns
- Vector<T>
Original parameters before the update
Remarks
FTRL's reverse update is complex due to its sparse regularization with thresholding. This method must be called immediately after UpdateParameters while the internal state (_z, _n) is fresh. Note: FTRL uses L1 regularization which causes sparsity through thresholding, making perfect reversal impossible. This implementation provides an approximate reverse based on the FTRL update formula.
For Beginners: This calculates where parameters were before an FTRL update. FTRL is designed for sparse data and uses thresholding (setting small values to zero), which is irreversible. This reverse method approximates the original parameters using FTRL's internal state.
Serialize()
Serializes the FTRL optimizer to a byte array.
public override byte[] Serialize()
Returns
- byte[]
A byte array representing the serialized state of the optimizer.
Remarks
For Beginners: This method converts the current state of the optimizer into a format that can be easily stored or transmitted. It's like taking a snapshot of the optimizer's memory, including all its settings and learned information, so you can save it or send it somewhere else.
UpdateAdaptiveParameters(OptimizationStepData<T, TInput, TOutput>, OptimizationStepData<T, TInput, TOutput>)
Updates the adaptive parameters based on the optimization progress.
protected override void UpdateAdaptiveParameters(OptimizationStepData<T, TInput, TOutput> currentStepData, OptimizationStepData<T, TInput, TOutput> previousStepData)
Parameters
currentStepDataOptimizationStepData<T, TInput, TOutput>Data from the current optimization step.
previousStepDataOptimizationStepData<T, TInput, TOutput>Data from the previous optimization step.
Remarks
For Beginners: This method adjusts how fast the algorithm learns based on its recent performance. If it's improving, it might speed up. If it's not improving, it might slow down to be more careful. It's like adjusting your study pace based on how well you're understanding the material.
UpdateOptions(OptimizationAlgorithmOptions<T, TInput, TOutput>)
Updates the options for the FTRL optimizer.
protected override void UpdateOptions(OptimizationAlgorithmOptions<T, TInput, TOutput> options)
Parameters
optionsOptimizationAlgorithmOptions<T, TInput, TOutput>The new options to be set.
Remarks
For Beginners: This method allows you to change the settings of the optimizer while it's running. It's like adjusting the controls on a machine while it's operating. This can be useful if you want to fine-tune the optimizer's behavior based on its performance or other factors.
Exceptions
- ArgumentException
Thrown when the provided options are not of type FTRLOptimizerOptions.
UpdateParameters(Vector<T>, Vector<T>)
Updates parameters using the FTRL (Follow The Regularized Leader) algorithm with L1/L2 regularization.
public override Vector<T> UpdateParameters(Vector<T> parameters, Vector<T> gradient)
Parameters
parametersVector<T>The current parameters.
gradientVector<T>The gradient at the current parameters.
Returns
- Vector<T>
The updated parameters with L1 sparsity promotion.
Remarks
FTRL uses a proximal gradient approach with soft-thresholding for L1 regularization, which drives small coefficients to exactly zero, promoting sparsity.
UpdateParametersGpu(IGpuBuffer, IGpuBuffer, int, IDirectGpuBackend)
Updates parameters on GPU using FTRL optimization.
public override void UpdateParametersGpu(IGpuBuffer parameters, IGpuBuffer gradients, int parameterCount, IDirectGpuBackend backend)
Parameters
parametersIGpuBuffergradientsIGpuBufferparameterCountintbackendIDirectGpuBackend
UpdateSolution(IFullModel<T, TInput, TOutput>, Vector<T>)
Updates the current solution using the FTRL update rule.
protected override IFullModel<T, TInput, TOutput> UpdateSolution(IFullModel<T, TInput, TOutput> currentSolution, Vector<T> gradient)
Parameters
currentSolutionIFullModel<T, TInput, TOutput>The current solution.
gradientVector<T>The calculated gradient.
Returns
- IFullModel<T, TInput, TOutput>
The updated solution.
Remarks
For Beginners: This method applies the FTRL algorithm's specific way of updating the solution. It's like adjusting the recipe based on how well the last batch of cookies turned out, but in a very sophisticated way that considers the history of all previous batches.