L2Prior

Adds L2 regularization to a Linear layer.

class L2Prior : AdditiveLinearPrior {
float _lambda;
float[] _lambdas;
float[][] W_prior;
size_t _ind_start;
void delegate() _accumulate_grad;
}

Examples

1 // basic L2 regularization: loss = (0.03 / 2) * || W ||^2
2 auto l1 = Linear(5).prior(L2Prior(0.03));
3 
4 // same, but centered around a non-zero matrix: loss = (0.03 / 2) * || W - W_p ||^2
5 auto l2 = Linear(5).prior(L2Prior(0.03, W_p));
6 
7 // L2 regularization with 1 lambda per feature (diagonal Hessian 0-centered prior).
8 // Example when input dim is 5:
9 auto l3 = Linear(10).prior(L2Prior([0.01f, 0.02f, 0.01f, 0.015f, 0.005f]));
10 
11 // L2 regularization with 1 lambda per feature, centered around a non-zero matrix.
12 // Example when input dim is 5:
13 auto l4 = Linear(10).prior(L2Prior([0.01f, 0.02f, 0.01f, 0.015f, 0.005f], W_p));

Meta