Skip to content

declearn.optimizer.regularizers.RidgeRegularizer

Bases: Regularizer

L2 (Ridge) loss-regularization plug-in.

This regularizer implements the following term:

loss += alpha * l2_norm(weights)

To do so, it applies the following correction to gradients:

grads += alpha * 2 * weights
Source code in declearn/optimizer/regularizers/_base.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class RidgeRegularizer(Regularizer):
    """L2 (Ridge) loss-regularization plug-in.

    This regularizer implements the following term:

        loss += alpha * l2_norm(weights)

    To do so, it applies the following correction to gradients:

        grads += alpha * 2 * weights
    """

    name: ClassVar[str] = "ridge"

    def run(
        self,
        gradients: Vector,
        weights: Vector,
    ) -> Vector:
        correct = 2 * self.alpha * weights
        return gradients + correct