zoukankan      html  css  js  c++  java
  • 线性模型与损失函数(分类问题)

    # 线性模型与损失函数(分类问题)

    1.线性模型的构成

      如下图所示,这是一个基础的线性模型示意图。

    线性模型图

      线性模型是机器学习中应用最广泛的模型,指通过样本特性的线性关系及组合来进行预测的模型,我们用下式来表示线性模型函数(判别函数)一般的表现形式:

    [egin{gathered} fleft( {mathop xlimits^ o ;mathop omega limits^ o ,b} ight) = {omega _1}{x_1} + {omega _2}{x_2} + cdots + {omega _D}{x_D} + b \ = mathop {{omega ^T}}limits^ o mathop xlimits^ o + b \ end{gathered}]

      其中,(mathop xlimits^ o = {left[ {{x_1}, cdots ,{x_D}} ight]^T})是D维的样本,(mathop omega limits^ o = {left[ {{omega _1}, cdots ,{omega _D}} ight]^T})(D)维的权重向量,(b)是偏置项,由于偏置项对整体来说不是主要影响因素,所以我们将整体线性模型函数表示为({fleft( {mathop xlimits^ o ;mathop omega limits^ o } ight)})

      而在分类问题中,我们的输出的预计目标是离散的标签(定义为(y),即有(y = fleft( {mathop xlimits^ o ;mathop omega limits^ o } ight))),而实际模型函数({fleft( {mathop xlimits^ o ;mathop omega limits^ o } ight)})的输出是实数,所以我们无法直接使用模型函数的输出进行预测,所以需要引入一个非线性的决策函数(等价于激活函数的作用)(gleft( ullet ight))来预测模型真实输出,即(y = gleft( {fleft( {mathop xlimits^ o ;mathop omega limits^ o } ight)} ight))

    2.线性判别函数、决策(等价与激活的作用)函数与决策边界

    2.1二分类

      在二分类模型中,(y)(我们预测所对比的标签)通常只有两类值,即负值和正值,我们用(left{ {-1,+1} ight})来表示(也可以用(left{ {1,0} ight})),而且线性判别函数我们只需要一个就能分出两个类别,即在特征空间中我们将所有满足(fleft( {x;omega } ight) = 0)的点构成一个决策边界,以此实现分成两类的目的。

      在特征空间中,决策平面与权重向量(omega)正交。而且在特征空间的每个点距决策平面的有向距离用下式表示:

    [gamma = frac{{fleft( {mathop xlimits^ o ;mathop omega limits^ o } ight)}}{{left| {mathop omega limits^ o } ight|}} ]

      其中(gamma)表示点({mathop xlimits^ o })({mathop omegalimits^ o })方向上的投影,为了更加直观的看出这些效果,我们可以看到下图:

    决策边界图

      图中横纵坐标表示的是样本的特征向量(mathop xlimits^ o = left[ {{x_1},{x_2}} ight]),权重向量(mathop omega limits^ o = left[ {{omega _1},{omega _2}} ight]),假设总共为N个样本的训练集(包含数据和标签)(D = left{ {left( {mathop {{x^{left( n ight)}}}limits^ o ,{y^{left( n ight)}}} ight)} ight}_{n = 1}^N),其中({y^{left( n ight)}} in left{ { + 1, - 1} ight}),我们的线性模型要学到参数(mathop {{omega ^ * }}limits^ o),使得(mathop {{omega ^ * }}limits^ o)要满足({y^{left( n ight)}}fleft( {mathop {{x^{left( n ight)}}}limits^ o ;mathop {{omega ^ * }}limits^ o } ight) > 0,forall n in left[ {1,N} ight]),即线性判别函数为正的时候对应标签为正,同理,判别函数为负对应标签也为负。

      那么问题来了,这个参数(mathop omega limits^ o)我们的模型是怎么学习到的呢?

      这个问题的答案也就是各种分类问题不一样的答案,即我们是通过损失函数让线性预测模型学习参数(mathop omega limits^ o),损失函数的不同也是区分各种分类模型最明显的点。

      二分类的损失函数是0-1损失函数,即:

    [{L_{01}}left( {y,fleft( {mathop xlimits^ o ;mathop omega limits^ o } ight)} ight) = Ileft( {yfleft( {mathop xlimits^ o ;mathop omega limits^ o } ight) > 0} ight) ]

      其中,(Ileft( ullet ight))是指示函数,但是0-1损失函数的数学性质不好,其关于(mathop omega limits^ o)的导数为0,从而让其无法继续优化。

    2.2多分类

      多分类和二分类的区别主要有两个,第一,多分类的决策边界有多个(即,线性判别函数有多个);第二,各种分类问题的损失函数不同(即,模型学习参数(mathop omega limits^ o)的方式不同)。但是多分类与二分类又是紧密联系的,因为多分类问题可以拆分为多个二分类问题。

      多分类问题类别为(left{ {1,2, cdots ,C} ight}),常用的有以下三类方式:

     (1)“一对其余”方式:把多分类问题转换为C个“一对其余”的二分类问题。这种方式共需要C个判别函数,其中第c个判别函数({f_c})是将类别c的样本和不属于该类别的样本分开。

     (2)“一对一”方式:把多分类问题转换为(Cleft( {C - 1} ight)/2)个“一对一”的二分类问题。这种方式共需要(Cleft( {C - 1} ight)/2)个判别函数,其中第几个判别函数就是将第几个样本进行与其他样本的拆分。

     (3)“argmax”方式:这是一种改进的“一对其余”方式,共需要C个判别函数:

    [{f_c}left( {mathop xlimits^ o ,mathop {{omega _c}}limits^ o } ight) = mathop {omega _c^T}limits^ o mathop xlimits^ o + {b_c},c in left{ {1, cdots ,C} ight} ]

      对于样本({mathop xlimits^ o }),如果存在一个类别c,相对于所有的其他类别(mathop climits^ sim)({f_c}left( {mathop xlimits^ o ;mathop {{omega _c}}limits^ o } ight) > {f_{mathop climits^ sim }}left( {mathop xlimits^ o ;mathop {{omega _{mathop climits^ sim }}}limits^ o } ight)),那么(mathop xlimits^ o)属于类别c。该种方式的预测函数定义为:

    [y = mathop {arg max }limits_{c = 1}^C {f_c}left( {mathop xlimits^ o ;mathop {{omega _c}}limits^ o } ight) ]

      如下图所示,其中“argmax”对比于其它两种方式具有明显的优势,因为它可以准确地判别任何一个样本所属的类别,而不会出现无法判别的问题。

    决策边界图

    3.Logistic回归(二分类方式)

      说到Logistic回归我们首先来看一下Logistic函数的定义,Logistic函数是一种常用的S型函数,其定义式为:

    [log isticleft( x ight) = frac{L}{{1 + exp left( { - Kleft( {x - {x_0}} ight)} ight)}} ]

      其中,(exp left( ullet ight))表示自然对数,({{x_0}})是中心点,(L)是最大值,(K)是曲线的倾斜度,为了更加直观的看看logistic函数的性质,我们看看它的效果图:

    logistic函数图

      有上图我们可以看出,当x趋向于(- infty)时,(log isticleft( x ight))趋向于0;当x趋向于(+ infty)时,(log isticleft( x ight))趋向于L。并且当参数为第一行即蓝色实线时,(log isticleft( x ight))函数被称为标准(log isticleft( x ight))函数,记作(sigma left( x ight))。且(sigma left( x ight) = frac{1}{{1 + exp left( { - x} ight)}})

      Logistic回归也就是对数回归,是一种常用的二分类问题的线性模型,为了解决连续的线性函数不适合进行分类的问题,我们定义对数回归为:

    [pleft( {y = 1|mathop xlimits^ o } ight) = gleft( {fleft( {mathop xlimits^ o ;mathop omega limits^ o } ight)} ight) ]

      其中,(gleft( ullet ight))称为激活函数,作用是将线性函数的值域从实数区间压缩到(left( {0,1} ight))之间,可以用来表示概率。

      在Logistic回归中,我们使用Logistic函数来作为激活函数,标签y=1的后验概率为:

    [egin{gathered} pleft( {y = 1|mathop xlimits^ o } ight) = sigma left( {mathop {{omega ^T}}limits^ o mathop xlimits^ o } ight) \ = frac{1}{{1 + exp left( { - mathop {{omega ^T}}limits^ o mathop xlimits^ o } ight)}} \ end{gathered}]

      其中,(mathop xlimits^ o = {left[ {{x_1}, cdots ,{x_D},1} ight]^T})表示样本的D+1维增广特征向量(mathop omega limits^ o = {left[ {{omega _1}, cdots ,{omega _D},b} ight]^T})表示D+1维的增广权重向量。

      既然得到了y=1的后验概率,那么在二分类中y=0的后验概率很容易就可以得到:

    [egin{gathered} pleft( {y = 0|mathop xlimits^ o } ight) = 1 - pleft( {y = 1|mathop xlimits^ o } ight) hfill \ = frac{{exp left( { - mathop {{omega ^T}}limits^ o mathop xlimits^ o } ight)}}{{1 + exp left( { - mathop {{omega ^T}}limits^ o mathop xlimits^ o } ight)}} hfill \ end{gathered}]

      而({mathop {{omega ^T}}limits^ o mathop xlimits^ o })可以表示为:

    [mathop {{omega ^T}}limits^ o mathop xlimits^ o = log frac{{pleft( {y = 1|mathop xlimits^ o } ight)}}{{pleft( {y = 0|mathop xlimits^ o } ight)}} ]

      其中,(frac{{pleft( {y = 1|mathop xlimits^ o } ight)}}{{pleft( {y = 0|mathop xlimits^ o } ight)}})为样本正反例后验概率的比值,称为几率。

      下图给出了线性回归和Logistic回归的区别:

    线性回归与logistic函数区别图

    3.1该种方式的参数学习

      Logistic回归采用交叉熵作为损失函数,并使用梯度下降算法来进行参数的优化。

      给定N个训练样本(left{ {mathop {{x^{left( n ight)}}}limits^ o ,{y^{left( n ight)}}} ight}_{n = 1}^N),用对数回归模型对每个样本进行预测,输出其标签为1的后验概率,记为({{hat y}^{left( n ight)}})

    [{{hat y}^{left( n ight)}} = sigma left( {mathop {{omega ^T}}limits^ o {{mathop xlimits^ o }^{left( n ight)}}} ight),1 leqslant n leqslant N ]

      由于({y^{left( n ight)}} in left{ {0,1} ight}),样本(left( {mathop {{x^{left( n ight)}}}limits^ o ,{y^{left( n ight)}}} ight))的真实条件概率可以表示为:

    [egin{gathered} {p_r}left( {{y^{left( n ight)}} = 1|mathop {{x^{left( n ight)}}}limits^ o } ight) = {y^{left( n ight)}} hfill \ {p_r}left( {{y^{left( n ight)}} = 0|mathop {{x^{left( n ight)}}}limits^ o } ight) = 1 - {y^{left( n ight)}} hfill \ end{gathered}]

      使用交叉熵损失函数,但是为了简单起见,忽略了正则化项,其风险函数为:

    [egin{gathered} Re left( {mathop omega limits^ o } ight) = - frac{1}{N}sumlimits_{n = 1}^N {left( {{p_r}left( {{y^{left( n ight)}} = 1|{{mathop xlimits^ o }^{left( n ight)}}} ight)log {{hat y}^{left( n ight)}} + {p_r}left( {{y^{left( n ight)}} = 0|{{mathop xlimits^ o }^{left( n ight)}}} ight)log left( {1 - {{hat y}^{left( n ight)}}} ight)} ight)} hfill \ = - frac{1}{N}sumlimits_{n = 1}^N {left( {{y^{left( n ight)}}log {{hat y}^{left( n ight)}} + left( {1 - {y^{left( n ight)}}} ight)log left( {1 - {{hat y}^{left( n ight)}}} ight)} ight)} hfill \ end{gathered}]

      由风险函数(Re left( {mathop omega limits^ o } ight))对参数({mathop omega limits^ o })的偏导数为:

    [egin{gathered} frac{{partial Re left( {mathop omega limits^ o } ight)}}{{partial mathop omega limits^ o }} = - frac{1}{N}sumlimits_{n = 1}^N {left( {{y^{left( n ight)}}frac{{{{hat y}^{left( n ight)}}left( {1 - {{hat y}^{left( n ight)}}} ight)}}{{{{hat y}^{left( n ight)}}}}{{mathop xlimits^ o }^{left( n ight)}} - left( {1 - {y^{left( n ight)}}} ight)frac{{{{hat y}^{left( n ight)}}left( {1 - {{hat y}^{left( n ight)}}} ight)}}{{left( {1 - {{hat y}^{left( n ight)}}} ight)}}{{mathop xlimits^ o }^{left( n ight)}}} ight)} hfill \ = - frac{1}{N}sumlimits_{n = 1}^N {{{mathop xlimits^ o }^{left( n ight)}}left( {{y^{left( n ight)}} - {{hat y}^{left( n ight)}}} ight)} hfill \ end{gathered}]

      采用梯度下降算法,Logistic回归的训练过程为:初始化(mathop {{omega _0}}limits^ o leftarrow 0),然后通过下式来迭代更新参数:

    [mathop {{omega _{t + 1}}}limits^ o leftarrow mathop {{omega _t}}limits^ o + alpha frac{1}{N}sumlimits_{n = 1}^N {{{mathop xlimits^ o }^{left( n ight)}}left( {{y^{left( n ight)}} - hat y_{{omega _t}}^{left( n ight)}} ight)} ]

      其中,(alpha)是学习率,({hat y_{{omega _t}}^{left( n ight)}})是当参数为(mathop {{omega _t}}limits^ o)时,Logistic回归模型的输出。

    4.Softmax回归(多分类的对数回归方式)

      Softmax回归,是Logistic回归在多分类问题上的推广。

      对于多分类问题,类别标签(y in left{ {1,2, cdots ,C} ight})可以有(C)个取值,给定一个样本({mathop xlimits^ o }),那么Softmax回归预测的属于类别c的条件概率为:

    [egin{gathered} pleft( {y = c|mathop xlimits^ o } ight) = softmax left( {mathop {omega _c^T}limits^ o mathop xlimits^ o } ight) hfill \ = frac{{exp left( {mathop {omega _c^T}limits^ o mathop xlimits^ o } ight)}}{{sum olimits_{c' = 1}^C {exp left( {mathop {omega _{c'}^T}limits^ o mathop xlimits^ o } ight)} }} hfill \ end{gathered}]

      其中,(mathop {{omega _c}}limits^ o)是第c类的权重向量。

      softmax回归的决策函数可以表示为:

    [egin{gathered} hat y = mathop {arg max }limits_{c = 1}^C pleft( {y = c|mathop xlimits^ o } ight) hfill \ = mathop {arg max }limits_{c = 1}^C left( {mathop {omega _c^T}limits^ o mathop xlimits^ o } ight) hfill \ end{gathered}]

      与Logistic回归的关系表现为,当类别数C=2时,Softmax回归的决策函数为:

    [egin{gathered} hat y = mathop {arg max }limits_{y in left{ {0,1} ight}} left( {mathop {{omega _y}^T}limits^ o mathop xlimits^ o } ight) hfill \ = Ileft( {mathop {{omega _1}^T}limits^ o mathop xlimits^ o - mathop {{omega _0}^T}limits^ o mathop xlimits^ o > 0} ight) hfill \ = Ileft( {{{left( {mathop {{omega _1}}limits^ o - mathop {{omega _0}}limits^ o } ight)}^T}mathop xlimits^ o > 0} ight) hfill \ end{gathered}]

      其中,(Ileft( ullet ight))是指示函数,所以根据二分类的线性模型我们可以得到权重向量的表达式为(mathop omega limits^ o = mathop {{omega _1}}limits^ o - mathop {{omega _0}}limits^ o)

    4.1参数学习

      首先,我们都是通过损失函数使得我们的模型进行参数矩阵(W)学习的,而在softmax回归模型中我们使用的是交叉熵损失函数,所以我们可以得到其风险函数为:

    [egin{gathered} Re left( W ight) = - frac{1}{N}sumlimits_{n = 1}^N {sumlimits_{c = 1}^C {{y_c}^{left( n ight)}log {{hat y}_c}^{left( n ight)}} } hfill \ = - frac{1}{N}sumlimits_{n = 1}^N {left( {{{left( {{y^{left( n ight)}}} ight)}^T}log {{hat y}^{left( n ight)}}} ight)} hfill \ end{gathered}]

      其中,({{hat y}^{left( n ight)}} = softmax left( {{W^T}mathop {{x^{left( n ight)}}}limits^ o } ight))为样本({mathop {{x^{left( n ight)}}}limits^ o })在每个类别的后验概率。

      风险函数(Re left( W ight))关于W的梯度为:

    [frac{{partial Re left( W ight)}}{{partial W}} = - frac{1}{N}{sumlimits_{n = 1}^N {mathop {{x^{left( n ight)}}}limits^ o left( {{y^{left( n ight)}} - {{hat y}^{left( n ight)}}} ight)} ^T} ]

      如果根据上式我们通过非向量形式转变,可以得到下列式子:

    [frac{{partial {L^{left( n ight)}}left( W ight)}}{{partial W}} = - mathop {{x^{left( n ight)}}}limits^ o {left( {{y^{left( n ight)}} - {{hat y}^{left( n ight)}}} ight)^T} ]

      采用梯度下降法,softmax回归的训练过程为:初始化({W_0} leftarrow 0),然后通过下式进行更新迭代:

    [mathop {{W_{t + 1}}}limits^ o leftarrow mathop {{W_t}}limits^ o + alpha left( {frac{1}{N}{{sumlimits_{n = 1}^N {{{mathop xlimits^ o }^{left( n ight)}}left( {{y^{left( n ight)}} - hat y_{{W_t}}^{left( n ight)}} ight)} }^T}} ight) ]

      其中(alpha)是学习率,({hat y_{{W_t}}^{left( n ight)}})是当参数为(mathop {{W_t}}limits^ o)时,Softmax回归模型的输出。

  • 相关阅读:
    JDBC的异常
    JDBC的事务
    JDBC的数据类型
    JDBC的结果集
    JDBC操作MySQL出现:This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, ...的问题解决
    JDBC的Statement对象
    JDBC连接数据库
    JDBC驱动类型
    JDBC实例代码
    java与javax的区别分析(转)
  • 原文地址:https://www.cnblogs.com/minyuan/p/14541585.html
Copyright © 2011-2022 走看看