-
Notifications
You must be signed in to change notification settings - Fork 3
/
l2_regularizer.py
37 lines (24 loc) · 957 Bytes
/
l2_regularizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
""" The L2 Regularization Class """
import numpy as np
from .regularizer import Regularizer
class L2Regularizer(Regularizer):
""" The L2 Regularization class to reduce overfitting.
Attributes:
lambd (float, optional): The hyper-parameter lambda for the L1 or L2 regularization.
"""
def forward(self, weights):
""" The forward stage for the L2 regularization.
Args:
weights (array): The weights of our layer.
Returns:
array: Returns the regularized weights of our layer to compute cost.
"""
return (np.sum(np.square(weights))) * (self.lambd / 2)
def backward(self, weights):
""" The backward stage for the L2 regularization.
Args:
weights (array): The weights of our layer.
Returns:
array: Returns the regularized weights of our layer to update parameters.
"""
return self.lambd * weights