def test_hinge_loss_backward(): """ Tests the backward pass of the hinge loss function """ from your_code import HingeLoss X = np.array([[-1, 2, 1], [-3, 4, 1]]) w = np.array([1, 2, 3]) y = np.array([1, -1]) loss = HingeLoss(regularization=None) _true = np.array([-1.5, 2, 0.5]) _est = loss.backward(X, w, y) print(_est)
def test_hinge_loss_forward(): """ Tests the forward pass of the hinge loss function """ from your_code import HingeLoss X = np.array([[-1, 2, 1], [-3, 4, 1]]) w = np.array([1, 2, 3]) y = np.array([1, -1]) loss = HingeLoss(regularization=None) _true = 4.5 _est = loss.forward(X, w, y) assert np.allclose(_true, _est)
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