IGradientBasedOptimizer<T, TInput, TOutput>
Interfaces · AiDotNet.Interfaces
Defines an interface for optimization algorithms that use gradients to find the best parameters for a model.
Implements: IOptimizer<T, TInput, TOutput>
Known implementations: GradientBasedOptimizerBase<T, TInput, TOutput>
How It Works
For Beginners: This interface defines methods for algorithms that help machine learning models learn efficiently.
Imagine you’re trying to find the lowest point in a hilly landscape while blindfolded:
- You can feel which way is downhill from your current position (this is the “gradient”)
- You take steps in the downhill direction to try to reach the lowest point
- Sometimes you might take bigger steps, sometimes smaller steps
- You need to decide how far to step each time
In machine learning:
- The “hills” represent how wrong your model’s predictions are
- The “lowest point” is where your model makes the fewest mistakes
- The “gradient” tells you which direction to adjust your model’s parameters
- The “optimizer” decides how big of an adjustment to make
Gradient-based optimizers are special because they use this directional information (the gradient) to make smarter adjustments to your model, helping it learn faster and better than if you were just making random changes.
Common examples include:
- Gradient Descent: Takes simple steps in the downhill direction
- Adam: A more sophisticated approach that adapts the step size for each parameter
- RMSProp: Adjusts step sizes based on recent gradient history
Properties
| Property | Summary |
|---|---|
Vector<T> LastComputedGradients { get; } |
Gets the gradients computed during the last optimization step. |
bool SupportsGpuUpdate { get; } |
Gets whether this optimizer supports GPU-accelerated parameter updates. |
Methods
| Method | Summary |
|---|---|
IFullModel<T, TInput, TOutput> ApplyGradients(Vector<T> gradients, IFullModel<T, TInput, TOutput> model) |
Applies pre-computed gradients to a model’s parameters. |
IFullModel<T, TInput, TOutput> ApplyGradients(Vector<T> originalParameters, Vector<T> gradients, IFullModel<T, TInput, TOutput> model) |
Applies pre-computed gradients to explicit original parameters (double-step safe). |
Matrix<T> UpdateParameters(Matrix<T> parameters, Matrix<T> gradient) |
Updates parameters based on their gradients. |
Vector<T> ReverseUpdate(Vector<T> updatedParameters, Vector<T> appliedGradients) |
Reverses a gradient update to recover original parameters before the update was applied. |
Vector<T> UpdateParameters(Vector<T> parameters, Vector<T> gradient) |
Updates parameters based on their gradients. |
double GetCurrentLearningRate() |
Gets the optimizer’s current learning rate, which may have been adjusted by a scheduler. |
void DisposeGpuState() |
Disposes GPU-allocated optimizer state. |
void InitializeGpuState(int parameterCount, IDirectGpuBackend backend) |
Initializes optimizer state on the GPU for a given parameter count. |
void Step(TapeStepContext<T> context) |
Performs a parameter update step using the optimizer’s update rule and the provided training context. |
void UpdateParameters(List<ILayer<T>> layers) |
Updates the parameters of all layers in a model based on their calculated gradients. |
void UpdateParametersGpu(IGpuBuffer parameters, IGpuBuffer gradients, int parameterCount, IDirectGpuBackend backend) |
Updates parameters on the GPU using optimizer-specific GPU kernels. |