美文网首页
loss函数之MarginRankingLoss

loss函数之MarginRankingLoss

作者: ltochange | 来源:发表于2021-07-17 23:01 被阅读0次

MarginRankingLoss

排序损失函数

对于包含N个样本的batch数据 D(x1,x2,y), x1, x2是给定的待排序的两个输入,y代表真实的标签,属于\{1,-1\}。当y=1是,x1应该排在x2之前,y=-1是,x1应该排在x2之后。第n个样本对应的loss计算如下:

l_{n}=\max (0,-y *(x 1-x 2)+\operatorname{margin})

x1, x2排序正确且-y *(x 1-x 2)>\operatorname{margin}, 则loss为0;其他情况下是loss为-y *(x 1-x 2)+\operatorname{margin}

class MarginRankingLoss(_Loss):
    __constants__ = ['margin', 'reduction']
    def __init__(self, margin=0., size_average=None, reduce=None, reduction='mean'):
        super(MarginRankingLoss, self).__init__(size_average, reduce, reduction)
        self.margin = margin
    def forward(self, input1, input2, target):
        return F.margin_ranking_loss(input1, input2, target, margin=self.margin, reduction=self.reduction)

pytorch中通过torch.nn.MarginRankingLoss类实现,也可以直接调用F.margin_ranking_loss 函数,代码中的size_averagereduce已经弃用。reduction有三种取值mean, sum, none,对应不同的返回\ell(x, y)。 默认为mean,对应于上述loss的计算

L=\left\{l_{1}, \ldots, l_{N}\right\}

\ell(x, y)=\left\{\begin{array}{ll}\operatorname L, & \text { if reduction }=\text { 'none' } \\ \frac1{N}\sum_{i=1}^{N} l_{i}, & \text { if reduction }=\text { 'mean' } \\ \sum_{i=1}^{N} l_{i} & \text { if reduction }=\text { 'sum' }\end{array} \right.

margin默认取值0

例子:

import torch
import torch.nn.functional as F
import torch.nn as nn
import math


def validate_MarginRankingLoss(input1, input2, target, margin):
    val = 0
    for x1, x2, y in zip(input1, input2, target):
        loss_val = max(0, -y * (x1 - x2) + margin)
        val += loss_val
    return val / input1.nelement()


torch.manual_seed(10)
margin = 0
loss = nn.MarginRankingLoss()
input1 = torch.randn([3], requires_grad=True)
input2 = torch.randn([3], requires_grad=True)
target = torch.tensor([1, -1, -1])
print(target)
output = loss(input1, input2, target)
print(output.item())

output = validate_MarginRankingLoss(input1, input2, target, margin)
print(output.item())

loss = nn.MarginRankingLoss(reduction="none")
output = loss(input1, input2, target)
print(output)

输出:

tensor([ 1, -1, -1])
0.015400052070617676
0.015400052070617676
tensor([0.0000, 0.0000, 0.0462], grad_fn=<ClampMinBackward>)

相关文章

  • loss函数之MarginRankingLoss

    MarginRankingLoss 排序损失函数 对于包含个样本的batch数据 , , 是给定的待排序的两个输入...

  • Center Loss

    损失函数改进之Center Loss

  • loss函数之triplet loss

    不同于交叉熵损失仅仅考虑样本与类别标签之间误差,triplet loss关注样本与其他样本之间距离。来自论文Lea...

  • loss函数之BCELoss

    BCELoss 二分类交叉熵损失 单标签二分类 一个输入样本对应于一个分类输出,例如,情感分类中的正向和负向 对于...

  • loss函数之KLDivLoss

    KL散度 KL散度,又叫相对熵,用于衡量两个分布(离散分布和连续分布)之间的距离。 设 、 是离散随机变量的两个概...

  • loss函数之SoftMarginLoss

    SoftMarginLoss 用于二分类任务 对于包含个样本的batch数据 , 代表模型输出,代表真实的类别标签...

  • loss函数之SoftMarginLoss

    SoftMarginLoss 用于二分类任务 对于包含个样本的batch数据 , 代表模型输出,代表真实的类别标签...

  • 逻辑回归

    逻辑回归: 公式 loss函数 非凸函数,容易造成局部最优 Minimizing the loss corresp...

  • region_layer

    一言蔽之,这个层就是用来算loss的,作者定义的multi task loss 对外主要提供6个函数: layer...

  • [ML] Loss_Function

    1. Loss Function(损失函数) 损失函数可以看做误差部分(loss term)+正则化部分(regu...

网友评论

      本文标题:loss函数之MarginRankingLoss

      本文链接:https://www.haomeiwen.com/subject/tpygpltx.html