铰链损失梯度

机器算法验证 损失函数
2022-02-08 20:27:48

我正在尝试实现基本梯度下降,并使用铰链损失函数进行测试,即但是,我对铰链损失的梯度感到困惑。我的印象是lhinge=max(0,1y xw)

wlhinge={y xif y xw<10if y xw1

但这不是返回一个与大小相同的矩阵吗?我以为我们希望返回一个长度为的向量?显然,我在某个地方有些困惑。有人可以在这里指出正确的方向吗?xw

我已经包含了一些基本代码,以防我对任务的描述不清楚

#Run standard gradient descent
gradient_descent<-function(fw, dfw, n, lr=0.01)
{
    #Date to be used
    x<-t(matrix(c(1,3,6,1,4,2,1,5,4,1,6,1), nrow=3))
    y<-c(1,1,-1,-1)
    w<-matrix(0, nrow=ncol(x))

    print(sprintf("loss: %f,x.w: %s",sum(fw(w,x,y)),paste(x%*%w, collapse=',')))
    #update the weights 'n' times
    for (i in 1:n)
    {
      w<-w-lr*dfw(w,x,y)
      print(sprintf("loss: %f,x.w: %s",sum(fw(w,x,y)),paste(x%*%w,collapse=',')))
    }
}
#Hinge loss
hinge<-function(w,x,y) max(1-y%*%x%*%w, 0)
d_hinge<-function(w,x,y){ dw<-t(-y%*%x); dw[y%*%x%*%w>=1]<-0; dw}
gradient_descent(hinge, d_hinge, 100, lr=0.01)

更新:虽然下面的答案帮助我理解了这个问题,但这个算法的输出对于给定的数据仍然是不正确的。损失函数每次减少 0.25,但收敛速度过快,得到的权重不会导致良好的分类。目前输出看起来像

#y=1,1,-1,-1
"loss: 1.000000, x.w: 0,0,0,0"
"loss: 0.750000, x.w: 0.06,-0.1,-0.08,-0.21"
"loss: 0.500000, x.w: 0.12,-0.2,-0.16,-0.42"
"loss: 0.250000, x.w: 0.18,-0.3,-0.24,-0.63"
"loss: 0.000000, x.w: 0.24,-0.4,-0.32,-0.84"
"loss: 0.000000, x.w: 0.24,-0.4,-0.32,-0.84"
"loss: 0.000000, x.w: 0.24,-0.4,-0.32,-0.84"
...  
3个回答

为了得到梯度,我们将损失与个分量进行区分iw

将铰链损失重写其中wf(g(w))f(z)=max(0,1y z)g(w)=xw

使用链式法则我​​们得到

wif(g(w))=fzgwi

和 0 当变为时被评估二阶导数项变为所以最后你得到 g(w)=xwyxw<1xw>1xi

f(g(w))wi={y xiif y xw<10if y xw>1

由于涵盖的分量,您可以将上述内容视为一个向量,并将写为ixw(w1,w2,)

这已经晚了3年,但仍然可能与某人有关......

表示点的样本和相应标签的集合我们寻找一个能够最小化总铰链损失的超平面对总铰链损失求导。每个分量的梯度为: SxiRdyi{1,1}w

w=argmin wLShinge(w)=argmin wilhinge(w,xi,yi)=argmin wimax{0,1yiwx}
w
lhingew={0yiwx1yixyiwx<1

总和的梯度是梯度的总和。 Python 示例,使用 GD 查找铰链损失最佳分离超平面如下(它可能不是最有效的代码,但它有效)

LShingew=ilhingew

import numpy as np
import matplotlib.pyplot as plt

def hinge_loss(w,x,y):
    """ evaluates hinge loss and its gradient at w

    rows of x are data points
    y is a vector of labels
    """
    loss,grad = 0,0
    for (x_,y_) in zip(x,y):
        v = y_*np.dot(w,x_)
        loss += max(0,1-v)
        grad += 0 if v > 1 else -y_*x_
    return (loss,grad)

def grad_descent(x,y,w,step,thresh=0.001):
    grad = np.inf
    ws = np.zeros((2,0))
    ws = np.hstack((ws,w.reshape(2,1)))
    step_num = 1
    delta = np.inf
    loss0 = np.inf
    while np.abs(delta)>thresh:
        loss,grad = hinge_loss(w,x,y)
        delta = loss0-loss
        loss0 = loss
        grad_dir = grad/np.linalg.norm(grad)
        w = w-step*grad_dir/step_num
        ws = np.hstack((ws,w.reshape((2,1))))
        step_num += 1
    return np.sum(ws,1)/np.size(ws,1)

def test1():
    # sample data points
    x1 = np.array((0,1,3,4,1))
    x2 = np.array((1,2,0,1,1))
    x  = np.vstack((x1,x2)).T
    # sample labels
    y = np.array((1,1,-1,-1,-1))
    w = grad_descent(x,y,np.array((0,0)),0.1)
    loss, grad = hinge_loss(w,x,y)
    plot_test(x,y,w)

def plot_test(x,y,w):
    plt.figure()
    x1, x2 = x[:,0], x[:,1]
    x1_min, x1_max = np.min(x1)*.7, np.max(x1)*1.3
    x2_min, x2_max = np.min(x2)*.7, np.max(x2)*1.3
    gridpoints = 2000
    x1s = np.linspace(x1_min, x1_max, gridpoints)
    x2s = np.linspace(x2_min, x2_max, gridpoints)
    gridx1, gridx2 = np.meshgrid(x1s,x2s)
    grid_pts = np.c_[gridx1.ravel(), gridx2.ravel()]
    predictions = np.array([np.sign(np.dot(w,x_)) for x_ in grid_pts]).reshape((gridpoints,gridpoints))
    plt.contourf(gridx1, gridx2, predictions, cmap=plt.cm.Paired)
    plt.scatter(x[:, 0], x[:, 1], c=y, cmap=plt.cm.Paired)
    plt.title('total hinge loss: %g' % hinge_loss(w,x,y)[0])
    plt.show()

if __name__ == '__main__':
    np.set_printoptions(precision=3)
    test1()

我修复了你的代码。主要问题是您对铰链和 d_hinge 函数的定义。这些应该一次应用一个样品。相反,您的定义会在取最大值之前聚合所有样本。

#Run standard gradient descent
gradient_descent<-function(fw, dfw, n, lr=0.01)
{
    #Date to be used
    x<-t(matrix(c(1,3,6,1,4,2,1,5,4,1,6,1), nrow=3))
    y<-t(t(c(1,1,-1,-1)))
    w<-matrix(0, nrow=ncol(x))


    print(sprintf("loss: %f,x.w: %s",sum(mapply(function(xr,yr) fw(w,xr,yr), split(x,row(x)),split(y,row(y)))),paste(x%*%w, collapse=',')))
    #update the weights 'n' times
    for (i in 1:n)
    {
      w<-w-lr*dfw(w,x,y)
      print(sprintf("loss: %f,x.w: %s",sum(mapply(function(xr,yr) fw(w,xr,yr), split(x,row(x)),split(y,row(y)))),paste(x%*%w,collapse=',')))
    }
}

#Hinge loss
hinge<-function(w,xr,yr) max(1-yr*xr%*%w, 0)
d_hinge<-function(w,x,y){ dw<- apply(mapply(function(xr,yr) -yr * xr * (yr * xr %*% w < 1),split(x,row(x)),split(y,row(y))),1,sum); dw}
gradient_descent(hinge, d_hinge, 100, lr=0.01)

我需要 n=10000 才能收敛。

[1]“损失:0.090000,XW:1.0899999999995,08999999999905,0.1.1900000000000008,1.690000000000001,1”[1]“损失:0.100000,XW:1.3399999999995,1.19999999999,0.900000000000075,-1.42000000000011,-1.42000000000011”[1]“损失:0.230000,XW: 0.939999999999948,0.829999999999905,-1.32000000000007,-1.77000000000011" [1] “损失:0.370000,XW:1.64999999999995,1.2899999999999,-0.630000000000075,-1.25000000000011”[1] “损失:0.000000,XW:1.24999999999995,0.999999999999905,-1.05000000000008,-1.60000000000011” [1] “损失:0.240000,XW:1.49999999999995,1.2099999999999,-0.760000000000075,-1.33000000000011”[1] “损失:0.080000,XW:1.09999999999995,0.919999999999905,-1.18000000000007,-1.68000000000011”[1]“损失:0.110000,XW: 1.34999999999995,1.1299999999999,-0.890000000000075,-1.41000000000011"[1]“损耗:0.210000,XW:0.949999999999905,-1.3100000000000007,1.7600000000000011”[1]“损失:0.380000,XW:1.6599999999995,1.29999999999,0.0.620000000000074,-1.2400000000000011”[1]“损失:0.000000,XW: 1.25999999999995,1.009999999999,1 -1.0400000000000008,1.5900000000000011,-1.59000000000011“[1]”损失:0.000000,XW:1.2599999999995,1.00999999999,-1.0400000000000008,-1.59000000000011“