def __init__(self, loss, regularization=None, learning_rate=0.01, reg_param=0.05): self.learning_rate = learning_rate # Select regularizer if regularization == 'l1': regularizer = L1Regularization(reg_param) #? elif regularization == 'l2': regularizer = L2Regularization(reg_param) elif regularization is None: regularizer = None else: raise ValueError( 'Regularizer {} is not defined'.format(regularization)) # Select loss function if loss == 'hinge': self.loss = HingeLoss(regularizer) elif loss == 'squared': self.loss = SquaredLoss(regularizer) else: raise ValueError('Loss function {} is not defined'.format(loss)) self.model = None
def test_l1_regularization_forward(): """ Test the forward pass of the L1Regularization class. """ from your_code import L1Regularization X = np.array([[-1, 2, 1], [-3, 4, 1]]) regularizer = L1Regularization(reg_param=0.5) _true = np.array([1.5, 3.5]) _est = np.array([regularizer.forward(x) for x in X]) assert np.allclose(_true, _est)