Class BaseRegularizer
- Namespace
- NeuralNetworks.Regularization
- Assembly
- NeuralNetworks.dll
Provides a base implementation of IRegularizer for applying regularization updates and optionally reporting a regularization penalty in the loss.
public abstract class BaseRegularizer : IRegularizer
- Inheritance
-
BaseRegularizer
- Implements
- Derived
- Inherited Members
Remarks
Derived types implement CalculatePenaltyCore(ReadOnlySpan<float>) and can override gradient and parameter update hooks to define regularization behavior.
Constructors
BaseRegularizer(bool)
Provides a base implementation of IRegularizer for applying regularization updates and optionally reporting a regularization penalty in the loss.
protected BaseRegularizer(bool reportPenaltyInLoss)
Parameters
reportPenaltyInLossboolIndicates whether the regularization penalty is included when reporting loss during training.
Remarks
Derived types implement CalculatePenaltyCore(ReadOnlySpan<float>) and can override gradient and parameter update hooks to define regularization behavior.
Properties
ReportPenaltyInLoss
Gets a value indicating whether a penalty is reported as a loss.
public bool ReportPenaltyInLoss { get; }
Property Value
Remarks
When it is true, the regularization penalty is intended to be included in the reported training loss; otherwise, regularization affects gradients without changing the reported loss value.
Methods
ApplyGradientCore(ReadOnlySpan<float>, Span<float>)
Applies a gradient update to the specified parameter span using the corresponding gradient span.
protected virtual void ApplyGradientCore(ReadOnlySpan<float> paramSpan, Span<float> paramGradientSpan)
Parameters
paramSpanReadOnlySpan<float>The span of parameter values to update.
paramGradientSpanSpan<float>The span of gradient values corresponding to
paramSpan.
ApplyGradient<T>(T, T)
Applies a gradient update using the specified parameter values and corresponding gradient values.
public void ApplyGradient<T>(T param, T paramGradient) where T : notnull
Parameters
paramTThe parameter values to use for the update.
paramGradientTThe gradient values corresponding to
param.
Type Parameters
TThe parameter container type that can be converted to spans of single-precision floating-point values.
Remarks
Both inputs are converted to spans and must have the same length.
CalculatePenaltyCore(ReadOnlySpan<float>)
protected virtual float CalculatePenaltyCore(ReadOnlySpan<float> paramSpan)
Parameters
paramSpanReadOnlySpan<float>
Returns
ReportPenalty<T>(T)
Computes and returns the penalty for the specified parameter when penalty reporting is enabled.
public float ReportPenalty<T>(T param) where T : notnull
Parameters
paramTThe parameter value to evaluate for penalty calculation.
Returns
- float
The computed penalty value when reporting is enabled; otherwise,
0f.
Type Parameters
TThe type of the parameter to evaluate. Must be a non-null type.
UpdateParamsCore(Span<float>)
Updates parameter values in the specified span.
protected virtual void UpdateParamsCore(Span<float> paramSpan)
Parameters
Remarks
Override in a derived type to apply parameter updates in place.
UpdateParams<T>(T)
Updates parameter values from the specified input by converting it to a Span<T> and passing it to the core update routine.
public void UpdateParams<T>(T param) where T : notnull
Parameters
paramTThe input containing parameter values to apply.
Type Parameters
TThe non-null input type that can be converted to a span of single-precision values.