class Loss(object):
def __init__(self):
self.grad_history = []
def forward(self, y_out, y_truth):
return NotImplementedError
def backward(self, y_out, y_truth, upstream_grad=1.):
return NotImplementedError
def __call__(self, y_out, y_truth):
loss = self.forward(y_out, y_truth)
grad = self.backward(y_out, y_truth)
return (loss, grad)
class L1(Loss):
def forward(self, y_out, y_truth):
"""
Performs the forward pass of the L1 loss function.
:param y_out: [N, ] array predicted value of your model.
y_truth: [N, ] array ground truth value of your training set.
:return: [N, ] array of L1 loss for each sample of your training set.
"""
result = None
result = np.abs(y_out - y_truth)
return result
def backward(self, y_out, y_truth):
"