hinge loss/支持向量损失的理解
Posted dxscode
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hinge loss/支持向量损失的理解相关的知识,希望对你有一定的参考价值。
https://blog.csdn.net/AI_focus/article/details/78339234
https://www.cnblogs.com/massquantity/p/8964029.html
pytprch HingeLoss 的实现:
import torch import torch.nn as nn import torch.utils.data as data import torchvision.transforms as TF import torchvision.utils as vutils import torch.nn.functional as F class HingeLoss(nn.Module): """ 铰链损失 SVM hinge loss L1 loss = sum(max(0,pred-true+1)) / batch_size 注意: 此处不包含正则化项, 需要另外计算出来 add https://blog.csdn.net/AI_focus/article/details/78339234 """ def __init__(self, n_classes, margin=1.): super(HingeLoss, self).__init__() self.margin = margin self.n_classes = n_classes def forward(self, y_pred, y_truth): # y_pred: [b,n_classes] = W^t.X # y_truth:[b,] batch_size = y_truth.size(0) mask = torch.eye(self.n_classes, self.n_classes, dtype=torch.bool)[y_truth].cuda() y_pred_true = torch.masked_select(y_pred, mask).unsqueeze(dim=-1).cuda() loss = torch.max(torch.zeros_like(y_pred).cuda(), y_pred - y_pred_true + self.margin) loss = loss.masked_fill(mask, 0) return torch.sum(loss) / batch_size if __name__ == ‘__main__‘: LossFun = HingeLoss(5) y_truth = torch.tensor([0, 1, 2]) y_pred = torch.randn([3, 5]) loss = LossFun(y_pred, y_truth) print(loss)
以上是关于hinge loss/支持向量损失的理解的主要内容,如果未能解决你的问题,请参考以下文章
机器学习中的损失函数 (着重比较:hinge loss vs softmax loss)
机器学习中的损失函数 (着重比较:hinge loss vs softmax loss)
Computer Vision | SVM loss function支持向量机损失函数在线性分类器中的应用