Table of Contents

Class L1Regularizer

Namespace
NeuralNetworks.Regularization
Assembly
NeuralNetworks.dll

Applies L1 regularization by adding a penalty proportional to the sum of absolute parameter values.

public class L1Regularizer : BaseRegularizer, IRegularizer
Inheritance
L1Regularizer
Implements
Inherited Members

Remarks

Uses a positive regularization coefficient to encourage sparse model parameters. During optimization, it adds l1 * sign(parameter) to each parameter gradient and computes the regularization penalty as l1 multiplied by the L1 norm of the parameter vector.

Constructors

L1Regularizer(float, bool)

Initializes a new instance of the L1Regularizer class with the specified L1 regularization factor.

public L1Regularizer(float l1, bool reportPenaltyInLoss = false)

Parameters

l1 float

Positive L1 regularization factor.

reportPenaltyInLoss bool

Indicates whether to include the regularization penalty in the reported loss value.

Exceptions

ArgumentOutOfRangeException

Thrown when l1 is less than or equal to 0.

Methods

ApplyGradientCore(ReadOnlySpan<float>, Span<float>)

Adds the L1 regularization gradient contribution for each parameter to the gradient buffer.

protected override void ApplyGradientCore(ReadOnlySpan<float> paramSpan, Span<float> paramGradientSpan)

Parameters

paramSpan ReadOnlySpan<float>

A read-only span of parameter values used to compute the sign-based L1 contribution.

paramGradientSpan Span<float>

A span of gradient values to update in place with the L1 contribution.

CalculatePenaltyCore(ReadOnlySpan<float>)

Calculates the L1 penalty for the specified parameter values.

protected override float CalculatePenaltyCore(ReadOnlySpan<float> paramSpan)

Parameters

paramSpan ReadOnlySpan<float>

A read-only span containing the parameter values to evaluate.

Returns

float

The L1 coefficient multiplied by the sum of the absolute 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.