【问题标题】:Hinge loss function gradient w.r.t. input prediction铰链损失函数梯度 w.r.t.输入预测
【发布时间】:2019-04-14 02:34:52
【问题描述】:

对于作业,我必须同时实现铰链损失及其偏导数计算功能。我得到了铰链损失函数本身,但我很难理解如何计算它的偏导数 w.r.t。预测输入。我尝试了不同的方法,但都没有奏效。

任何帮助、提示、建议将不胜感激!

这是铰链损失函数本身的解析表达式:

这是我的铰链损失函数实现:

def hinge_forward(target_pred, target_true):
    """Compute the value of Hinge loss 
        for a given prediction and the ground truth
    # Arguments
        target_pred: predictions - np.array of size `(n_objects,)`
        target_true: ground truth - np.array of size `(n_objects,)`
    # Output
        the value of Hinge loss 
        for a given prediction and the ground truth
        scalar
    """
    output = np.sum((np.maximum(0, 1 - target_pred * target_true)) / target_pred.size)

    return output

现在我需要计算这个梯度:

这是我尝试的铰链损失梯度计算:

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,)`
    """
# ----------------
#     try 1
# ----------------
#     hinge_result = hinge_forward(target_pred, target_true)

#     if hinge_result == 0:
#         grad_input = 0
#     else:
#         hinge = np.maximum(0, 1 - target_pred * target_true)
#         grad_input = np.zeros_like(hinge)
#         grad_input[hinge > 0] = 1
#         grad_input = np.sum(np.where(hinge > 0))
# ----------------
#     try 2
# ----------------
#     hinge = np.maximum(0, 1 - target_pred * target_true)
#     grad_input = np.zeros_like(hinge)

#     grad_input[hinge > 0] = 1
# ----------------
#     try 3
# ----------------
    hinge_result = hinge_forward(target_pred, target_true)

    if hinge_result == 0:
        grad_input = 0
    else:
        loss = np.maximum(0, 1 - target_pred * target_true)
        grad_input = np.zeros_like(loss)
        grad_input[loss > 0] = 1
        grad_input = np.sum(grad_input) * target_pred

    return grad_input

【问题讨论】:

    标签: python machine-learning deep-learning loss-function


    【解决方案1】:

    我已经设法通过使用 np.where() 函数解决了这个问题。代码如下:

    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
    

    对于 y*y

    【讨论】:

      猜你喜欢
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2022-01-14
      • 2018-10-14
      • 2016-11-29
      • 2020-08-25
      • 2019-05-08
      相关资源
      最近更新 更多