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)

  

原文地址:https://www.cnblogs.com/dxscode/p/12019794.html

时间: 2024-08-24 10:27:38

hinge loss/支持向量损失的理解的相关文章

损失函数 hinge loss vs softmax loss

1. 损失函数 损失函数(Loss function)是用来估量你模型的预测值 f(x) 与真实值 Y 的不一致程度,它是一个非负实值函数,通常用 L(Y,f(x)) 来表示. 损失函数越小,模型的鲁棒性就越好. 损失函数是经验风险函数的核心部分,也是结构风险函数的重要组成部分.模型的风险结构包括了风险项和正则项,通常如下所示: 其中,前面的均值函数表示的是经验风险函数,L代表的是损失函数,后面的 Φ 是正则化项(regularizer)或者叫惩罚项(penalty term), 它可以是L1,

【机器学习基础】支持向量回归

引言 这一小节介绍一下支持向量回归,我们在之前介绍的核逻辑回归使用表示定理(Representer Theorem),将逻辑回归编程Kernel的形式,这一节我们沿着这个思路出发,看看如何将回归问题和Kernel的形式结合起来. Kernel Ridge Regression 上次介绍的表示定理告诉我们,如果我们要处理的是有L2的正则项的线性模型,其最优解是数据zn的线性组合.我们可以将这样的线性模型变成Kernel的形式. 既然我们知道这样带有L2-Regularizer的线性回归模型的最佳解

matlab利用hinge loss实现多分类SVM

介绍 hinge loss code 1 介绍 本文将介绍hinge loss E(w)以及其梯度?E(w).并利用批量梯度下降方法来优化hinge loss实现SVM多分类.利用hinge loss在手写字数据库上实验,能达到87.040%的正确识别率. 2. hinge loss 根据二分类的SVM目标函数,我们可以定义多分类的SVM目标函数: E(w1,-,wk)=∑kj=112||wj||2+C∑ni=1L((w1,-,wk),(xi,yi)). 其中T={(x1,y1),-,(xn,y

通过支持向量排名法来做行人鉴定

其中为合页函数 通过支持向量排名法来做行人鉴定

logistic regression svm hinge loss

二类分类器svm 的loss function 是 hinge loss:L(y)=max(0,1-t*y),t=+1 or -1,是标签属性. 对线性svm,y=w*x+b,其中w为权重,b为偏置项,在实际优化中,w,b是待优化的未知,通过优化损失函数,使得loss function最小,得到优化接w,b. 对于logistic regression 其loss function是,由于y=1/(1+e^(-t)),则L=sum(y(log(h))+(1-y)log(1-h))

支持向量分类方法

1. 普通的支持向量积分类方法 import numpy as np # 加载数据 def loadData(): DataMatrix = [] LabelMatrix = [] with open("testSet.txt") as fr: for line in fr.readlines(): datas = line.strip().split('\t') DataMatrix.append([float(datas[0]), float(datas[1])]) LabelMa

《机器学习技法》---支持向量回归

1 核型岭回归 首先,岭回归的形式如下: 在<核型逻辑回归>中我们介绍过一个定理,即以上这种形式的问题,求得的w都能表示为z的线性组合: 因此我们把w代入,问题就转化为求β的问题,同时引入核技巧: 求解这个问题,先求梯度: 令

支持向量到最优超平面的距离的推导

The issue at hand is to find the parameters wo and bo for the optimal hyperplane, given the training set {(xi,di)}. 原文地址:https://www.cnblogs.com/donggongdechen/p/9943603.html

sklearn使用高斯核SVM显示支持向量

import graphviz import mglearn from mpl_toolkits.mplot3d import Axes3D from sklearn.datasets import load_breast_cancer, make_blobs from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.s