Skip to content Skip to sidebar Skip to footer

Hinge Loss Function Gradient W.r.t. Input Prediction

For an assignment I have to implement both the Hinge loss and its partial derivative calculation functions. I got the Hinge loss function itself but I'm having hard time understand

Solution 1:

I've managed to solve this by using np.where() function. Here is the code:

def hinge_grad_input(target_pred, target_true):
    """Compute the partial derivative 
        of Hinge loss with respect to its input
    # Arguments
        target_pred: predictions - np.array of size `(n_objects,)`
        target_true: ground truth - np.array of size `(n_objects,)`
    # Output
        the partial derivative 
        of Hinge loss with respect to its input
        np.array of size `(n_objects,)`
    """
    grad_input = np.where(target_pred * target_true < 1, -target_true / target_pred.size, 0)

    return grad_input

Basically the gradient equals -y/N for all the cases where y*y < 1, otherwise 0.


Post a Comment for "Hinge Loss Function Gradient W.r.t. Input Prediction"