LevenbergMarquardtOptimizer<T, TInput, TOutput>
Models & Types · AiDotNet.Optimizers
Implements the Levenberg-Marquardt optimization algorithm for non-linear least squares problems.
Inheritance: OptimizerBase<T, TInput, TOutput> → GradientBasedOptimizerBase<T, TInput, TOutput> → LevenbergMarquardtOptimizer<T, TInput, TOutput>
For Beginners
This optimizer is like a smart problem-solver that’s really good at fitting curves to data points. It’s especially useful when the relationship between your inputs and outputs isn’t a straight line. It works by making small adjustments to its guess, getting closer to the best solution with each step.
How It Works
The Levenberg-Marquardt algorithm is a popular method for solving non-linear least squares problems. It combines the Gauss-Newton algorithm and the method of gradient descent, providing a robust solution that works well in many situations.
Example
using AiDotNet;
using AiDotNet.Data.Loaders;
using AiDotNet.Enums;
using AiDotNet.NeuralNetworks;
using AiDotNet.Optimizers;
using AiDotNet.Tensors.LinearAlgebra;
var rng = new Random(0);
var trainX = new Tensor<double>(new[] { 32, 8 });
var trainY = new Tensor<double>(new[] { 32, 2 });
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 8; j++) trainX[new[] { i, j }] = rng.NextDouble();
trainY[new[] { i, i % 2 }] = 1.0;
}
var model = new NeuralNetwork<double>(new NeuralNetworkArchitecture<double>(
inputFeatures: 8, numClasses: 2, complexity: NetworkComplexity.Simple));
var result = await new AiModelBuilder<double, Tensor<double>, Tensor<double>>()
.ConfigureModel(model)
.ConfigureOptimizer(new LevenbergMarquardtOptimizer<double, Tensor<double>, Tensor<double>>(model))
.ConfigureDataLoader(DataLoaders.FromTensors(trainX, trainY))
.BuildAsync();
Console.WriteLine("Trained with LevenbergMarquardtOptimizer.");
Constructors
| Constructor | Summary |
|---|---|
LevenbergMarquardtOptimizer(IFullModel<T, TInput, TOutput> model, LevenbergMarquardtOptimizerOptions<T, TInput, TOutput> options, IEngine engine) |
Initializes a new instance of the LevenbergMarquardtOptimizer class. |
Methods
| Method | Summary |
|---|---|
LevenbergMarquardtOptimizer<T, TInput, TOutput> CreateForFunction(LevenbergMarquardtOptimizerOptions<T, TInput, TOutput> options) |
Creates a Levenberg-Marquardt optimizer for minimizing a plain function, with no model attached. |
OptimizationAlgorithmOptions<T, TInput, TOutput> GetOptions() |
Retrieves the current options of the optimizer. |
OptimizationResult<T, TInput, TOutput> Optimize(OptimizationInputData<T, TInput, TOutput> inputData) |
Performs the optimization process using the Levenberg-Marquardt algorithm. |
void UpdateParametersGpu(IGpuBuffer parameters, IGpuBuffer gradients, int parameterCount, IDirectGpuBackend backend) |
Updates parameters using GPU-accelerated Levenberg-Marquardt. |