Class L2Regularizer
- Namespace
- NeuralNetworks.Regularization
- Assembly
- NeuralNetworks.dll
Applies L2 regularization by adding a penalty proportional to the squared magnitude of model parameters.
public class L2Regularizer : BaseRegularizer, IRegularizer
- Inheritance
-
L2Regularizer
- Implements
- Inherited Members
Remarks
Uses the L2 penalty form l2 * Σ(w^2), contributes 2 * l2 * w to each parameter gradient, and can optionally include the penalty term in the reported loss via the base regularizer configuration.
Constructors
L2Regularizer(float, bool)
Initializes a new instance of the L2Regularizer class with the specified L2 regularization factor.
public L2Regularizer(float l2, bool reportPenaltyInLoss = false)
Parameters
l2floatPositive L2 regularization factor.
reportPenaltyInLossboolIndicates whether to include the regularization penalty in the reported loss.
Exceptions
- ArgumentOutOfRangeException
Thrown when
l2is less than or equal to 0.
Methods
ApplyGradientCore(ReadOnlySpan<float>, Span<float>)
Adds the L2 regularization gradient contribution for each parameter element to the existing gradient values.
protected override void ApplyGradientCore(ReadOnlySpan<float> paramSpan, Span<float> paramGradientSpan)
Parameters
paramSpanReadOnlySpan<float>A read-only span containing the parameter values used to compute the L2 gradient term.
paramGradientSpanSpan<float>A span containing gradient values to update in place with the L2 contribution.
Remarks
For each index, adds 2 * _l2 * paramSpan[i] to paramGradientSpan[i].
CalculatePenaltyCore(ReadOnlySpan<float>)
Computes the L2 penalty as the scaled sum of squared parameter values.
protected override float CalculatePenaltyCore(ReadOnlySpan<float> paramSpan)
Parameters
paramSpanReadOnlySpan<float>A read-only span of parameter values to penalize.
Returns
- float
The L2 coefficient multiplied by the sum of squares of all values in
paramSpan.
ToString()
Returns a string that represents the current object.
public override string ToString()
Returns
- string
A string that represents the current object.