RpropOptimizer<T, TInput, TOutput>
Models & Types · AiDotNet.Optimizers
Implements Rprop (resilient backpropagation): a per-weight adaptive step size driven by the SIGN of the gradient, with the magnitude discarded entirely.
Inheritance: OptimizerBase<T, TInput, TOutput> → GradientBasedOptimizerBase<T, TInput, TOutput> → RpropOptimizer<T, TInput, TOutput>
For Beginners
Nearly every other optimizer moves further when the gradient is large. Rprop throws the size away and keeps only the direction. Each weight carries its own step size: keep pushing the same way and the step grows by 20%, reverse direction and you must have gone too far, so the step is halved and this turn is skipped. That makes it completely immune to gradients that are vanishingly small or explosively large – but it also means it needs an honest gradient computed over all your data, not a noisy one from a small batch.
How It Works
Faithful to the pseudocode of Riedmiller and Braun, “A Direct Adaptive Method for Faster Backpropagation Learning: The RPROP Algorithm” (Proc. IEEE International Conference on Neural Networks, 1993, pp. 586-591, doi:10.1109/ICNN.1993.298623). For each weight, with g_prev the gradient stored from the previous step:
The sign-reversal branch is the one that is easy to get wrong. The paper takes NO step when the gradient reverses – it shrinks the step size and waits – and it zeroes the stored gradient so the following step is treated as a fresh sign rather than as a second reversal. Both halves matter: without the zeroing, a single oscillation would be counted twice and the step size would halve twice for one overshoot. This is the variant Igel and Huesken later labelled Rprop-minus (no weight backtracking), and it is what the paper’s published pseudocode and the fused Rprop kernel both implement.
Both eager paths are written branchlessly over whole vectors/tensors rather than as elementwise loops with data-dependent branches. The three cases are recovered from sign(g_prev * g) as three disjoint indicator vectors, which keeps the update on the vectorized engine while remaining exactly the three-case rule above.
Rprop requires full-batch gradients. The paper is explicit that this is a batch method, and the reason
is structural: the algorithm interprets a sign flip as “I overshot the minimum”, but on a mini-batch the sign
flips constantly from sampling noise alone, so every step size is driven down to Delta_min and training
stalls. Optimize therefore evaluates the gradient over the entire training set each iteration
and this optimizer exposes no batch size. On the tape path the caller controls batching, so the same
requirement applies to the caller.
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 RpropOptimizer<double, Tensor<double>, Tensor<double>>(model))
.ConfigureDataLoader(DataLoaders.FromTensors(trainX, trainY))
.BuildAsync();
Console.WriteLine("Trained with RpropOptimizer.");
Constructors
| Constructor | Summary |
|---|---|
RpropOptimizer(IFullModel<T, TInput, TOutput> model, RpropOptimizerOptions<T, TInput, TOutput> options) |
Initializes a new instance of the RpropOptimizer class. |
Methods
| Method | Summary |
|---|---|
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 Rprop algorithm. |
Vector<T> GetStepSizes() |
Gets the current per-weight step sizes, or null before the first update. |
Vector<T> ReverseUpdate(Vector<T> updatedParameters, Vector<T> appliedGradients) |
Reverses an Rprop update to recover the parameters from before the step. |
Vector<T> UpdateParameters(Vector<T> parameters, Vector<T> gradient) |
Updates a vector of parameters using the Rprop algorithm. |