人脸识别-Loss-2016:Large Margin Softmax Loss【Margin:角度分类边界之间的空白角度区域】【增大Margin来提高分类精度】【缺陷:无法处理W_i≠W_j的情况】

尽管传统的softmax在卷积网络作为最常用的监督学习组件,但是他不能促进判别性强的特征的学习,在这篇论文里面首先提出一种基于Margin的L-Softmax损失函数,可以明确地促使学习到的特征具有类内的紧凑性和类间的可分离性。

此外L-Softmax不仅可以调节所需的Margin,还可以避免过拟合的发生。这个损失函数可以通过经典的随机梯度下降算法优化。

Large-Margin Softmax Loss(L-Softmax Loss)是为了提高类内特征的紧凑性,拉大类间特征的间隔(intra-class compactness and inter-class separability)而提出来的。

总的来说,L-softmax会缩小每个类别的可行角度(Feasible angle)范围,在这些类别之间产生角度Magin。

Feasible angle of the i-th class:refers to the possible angle between x x x and W i W_i Wi that is learned by CNNs.

一、Softmax Loss(Softmax + Cross-Entropy Loss)

Softmax Loss(Softmax + Cross-Entropy Loss)是CNNs中最为常见的代价组合,效果不错,但是没有对特征有区别的学习。

L = − ∑ j = 1 T y j l o g ( S j ) L = -\sum_{j=1}^{T}y_{j} log (S_{j}) L=j=1Tyjlog(Sj)

  • 首先 L L L 是损失。
  • S j S_j Sj 是Softmax的输出向量 S S S 的第 j j j 个值,前面已经介绍过了,表示的是这个样本属于第 j j j 个类别的概率。
  • y j y_j yj 前面有个求和符号, j j j 的范围也是 1 1 1 到类别数 T T T,因此 y y y 是一个 1 × T 1×T 1×T 的向量,里面的 T T T 个值,而且只有 1 1 1 个值是 1 1 1,其他 T − 1 T-1 T1 个值都是 0 0 0。那么哪个位置的值是 1 1 1 呢?答案是真实标签对应的位置的那个值是 1 1 1,其他都是 0 0 0

Softmax Loss组件为三个部分的组合,包括:

  • 交叉熵损失、
  • Softmax函数、
  • 网络结构最后的一层全连接层。

如下图:
在这里插入图片描述
在这样的定义下,许多流行的CNN网络可以被视为卷积特征学习组件和Softmax组件。最后一层全连接层可以表示为 f = W x f = Wx f=Wx W c W_{c} Wc W W W 矩阵(权重参数)中第 c c c 行, W c x = ∣ ∣ W c ∣ ∣ 2 ∣ ∣ x ∣ ∣ 2 c o s ( θ c ) W_{c}x=\left| \left| W_{c} \right| \right|_{2}\left| \left| x \right| \right|_{2}cos(\theta_{c}) Wcx=Wc2x2cos(θc) c c c 是表示某一类的索引, W c W_{c} Wc 可视为类别 c c c 的线性分类器。在这样的情况下,标签预测很大程度上取决于与每个类别角度的相似性

Large-Margin Softmax Loss的目的是根据角度相似性将softmax损失归纳为更一般的大幅度softmax(L-Softmax)损失,从而导致学习的特征之间可能存在更大的角度可分离性。

二、Large-Margin Softmax Loss(L-softmax Loss)

首先上图,看下L-softmax损失与softmax训练出的特征有什么不一样:

在这里插入图片描述

  • 第一个都是传统的softmax,
  • 后面3个是不同参数的L-softmax,

可以看到 L-Softmax 具有更紧凑的类内距离,更大的类间距离。怎么做到的呢?

1、原始的 Softmax Loss:

L = 1 N ∑ i L i = − 1 N ∑ i log ⁡ ( e w y i T x i ∑ j e w j T x i ) L = \cfrac{1}{N} \sum_{i} L_i = -\cfrac{1}{N} \sum_i \log \left (\cfrac{e^{\mathbf{w}_{y_i}^T \mathbf{x}_i}}{\sum_{j} e^{\mathbf{w}_{j}^T \mathbf{x}_i}} \right) L=N1iLi=N1ilog(jewjTxiewyiTxi)

可以看到:

  • 样本 i i i 的类别 j j j 的分数由 w j T x i \mathbf{w}_j^T\mathbf{x}_i wjTxi ​决定,这是一个内乘操作,所以可以说类别 j j j 的分数由向量 w j \mathbf{w_j} wj ​和 x i \mathbf{x}_i xi ​的相似度决定,二者的相似度越大,分数越高,样本 i i i 越有可能属于类别 j j j
  • 内乘操作可以写成向量模和夹角余弦的乘积形式,如下(其中 θ j \theta_j θj ​表示 w j \mathbf{w_j} wj ​和 x i \mathbf{x}_i xi ​的夹角):
    w j T x i = ∥ w j ∥ ∥ x i ∥ cos ⁡ ( θ j ) \mathbf{w}_j^T \mathbf{x}_i = \lVert \mathbf{w}_j \rVert \lVert \mathbf{x}_i \rVert \cos(\theta_j) wjTxi=wjxicos(θj)
  • 损失值可以改写成
    L i = − log ⁡ ( e ∥ w y i ∥ ∥ x i ∥ cos ⁡ ( θ y i ) ∑ j e ∥ w j ∥ ∥ x i ∥ cos ⁡ ( θ j ) ) L_i = - \log \left (\cfrac{e^{\lVert \mathbf{w}_{y_i} \rVert \lVert \mathbf{x}_i \rVert \cos(\theta_{y_i})}}{\sum_{j} e^{\lVert \mathbf{w}_j \rVert \lVert \mathbf{x}_i \rVert \cos(\theta_j)}} \right) Li=log(jewjxicos(θj)ewyixicos(θyi))

2、Large-Margin Softmax

2.1 二分类例子

先举一个二分类例子来描述问题。

假设样本特征 x x x,它属于类别 1 1 1,那么,为了将 x x x 分类正确,原始的Softmax损失下降会促使 w 1 T x > w 2 T x \mathbf{w}_1^T \mathbf{x} \gt \mathbf{w}_2^T \mathbf{x} w1Tx>w2Tx ,可以写成如下形式:

∥ w 1 ∥ ∥ x ∥ cos ⁡ ( θ 1 ) > ∥ w 2 ∥ ∥ x ∥ cos ⁡ ( θ 2 ) \lVert \mathbf{w}_1 \rVert \lVert \mathbf{x} \rVert \cos (\theta_1) \gt \lVert \mathbf{w}_2 \rVert \lVert \mathbf{x} \rVert \cos (\theta_2) w1xcos(θ1)>w2xcos(θ2)

由于一定存在 ∥ w 1 ∥ ∥ x ∥ cos ⁡ ( θ 1 ) ≥ ∥ w 1 ∥ ∥ x ∥ cos ⁡ ( m θ 1 ) , 0 ≤ θ 1 ≤ π m ,其中 m 是正数。 \lVert \mathbf{w}_1 \rVert \lVert \mathbf{x} \rVert \cos (\theta_1) \ge \lVert \mathbf{w}_1 \rVert \lVert \mathbf{x} \rVert \cos (m \theta_1) , 0\leq \theta_{1} \leq\cfrac{\pi}{m},其中 m 是正数。 w1xcos(θ1)w1xcos(mθ1)0θ1mπ,其中m是正数。

为了使上面的不等式左边远远大于右边(促使分类更加严格),我们只需要加个约束:

∥ w 1 ∥ ∥ x ∥ cos ⁡ ( m θ 1 ) > ∥ w 2 ∥ ∥ x ∥ cos ⁡ ( θ 2 ) \lVert \mathbf{w}_1 \rVert \lVert \mathbf{x} \rVert \cos (m \theta_1) \gt \lVert \mathbf{w}_2 \rVert \lVert \mathbf{x} \rVert \cos (\theta_2) w1xcos(mθ1)>w2xcos(θ2)

这样产生一个决策Margin, m m m 是正整数,可以控制Margin的大小, m m m 越大,Margin越大,学习特征的难度越大,分类边界越严格。

即:不等式

∥ w 1 ∥ ∥ x ∥ cos ⁡ ( θ 1 ) ≥ ∥ w 1 ∥ ∥ x ∥ cos ⁡ ( m θ 1 ) > ∥ w 2 ∥ ∥ x ∥ cos ⁡ ( θ 2 ) \begin{aligned} \lVert \mathbf{w}_1 \rVert \lVert \mathbf{x} \rVert \cos (\theta_1) \ge \lVert \mathbf{w}_1 \rVert \lVert \mathbf{x} \rVert \cos (m \theta_1) \gt \lVert \mathbf{w}_2 \rVert \lVert \mathbf{x} \rVert \cos (\theta_2) \end{aligned} w1xcos(θ1)w1xcos(mθ1)>w2xcos(θ2) 恒成立。

因此,新的分类标准比正确对样本 x x x 分类的要求更为严格,为类别 1 1 1 产生更严格的决策边界。

2.2 L-Softmax损失函数

遵照上面的规则,L-Softmax损失函数可以定义为:

L i = − log ⁡ ( e ∥ w y i ∥ ∥ x i ∥ ψ ( θ y i ) e ∥ w y i ∥ ∥ x i ∥ ψ ( θ y i ) + ∑ j e ∥ w j ≠ y i ∥ ∥ x i ∥ cos ⁡ ( θ j ) ) (4) L_i = - \log \left (\cfrac{e^{\lVert \mathbf{w}_{y_i} \rVert \lVert \mathbf{x}_i \rVert \psi (\theta_{y_i})}}{e^{\lVert \mathbf{w}_{y_i} \rVert \lVert \mathbf{x}_i \rVert \psi (\theta_{y_i})} + \sum_{j} e^{\lVert \mathbf{w}_{j \neq y_i} \rVert \lVert \mathbf{x}_i \rVert \cos(\theta_j)}} \right) \tag4 Li=log(ewyixiψ(θyi)+jewj=yixicos(θj)ewyixiψ(θyi))(4)

其中
ψ ( θ ) = { cos ⁡ ( m θ ) , 0 ≤ θ ≤ π m D ( θ ) , π m < θ ≤ π (5) \psi(\theta) = \begin{cases} \cos (m \theta), 0 \le \theta \le \cfrac{\pi}{m} \\ \mathcal{D} (\theta), \cfrac{\pi}{m} \lt \theta \le \pi \tag5 \end{cases} ψ(θ)= cos(mθ),0θmπD(θ),mπ<θπ(5)

  • 公式(5)要保证 D ( θ ) D(\theta) D(θ) 要单调递减,并且 D ( π m ) = c o s ( π ) D(\cfrac{\pi}{m}) = cos(\pi) D(mπ)=cos(π) ,为了保证随着 θ y i \theta_{yi} θyi 的增大,样本的损失也增大,需要让 ψ ( θ ) \psi(\theta) ψ(θ) [ 0 , π ] [0,\pi] [0,π] 单调递减,可以这么理解,如果要让损失函数在样本上递减,必须使得分母 ∣ ∣ W y i ∣ ∣ ∣ ∣ x i ∣ ∣ ψ ( θ y i ) \left| \left| W_{yi} \right| \right| \left| \left| x_{i} \right|\right|\psi(\theta_{yi}) Wyixiψ(θyi) 递增,因为 c o s ( θ y i ) ≥ c o s ( m θ y i ) > D ( θ y i ) cos(\theta_{yi})\geq cos(m\theta_{yi})>D(\theta_{yi}) cos(θyi)cos(mθyi)>D(θyi) ,随着 ψ ( θ y i ) \psi(\theta_{yi}) ψ(θyi) 的递增,可以保证 θ y i \theta_{yi} θyi 越来越小。
  • 总的来说,其实有点类似数学里面的不等式放缩,让原始的Softmax一定小于L-softmax损失,如果L-Softmax损失收敛到一个很小值,那么原始的Softmax损失会比原来更小,这样解决了原始的Softmax损失收敛出现瓶颈的问题

但是为了简化上述(5)的函数,可以改造函数 ψ ( θ ) \psi(\theta) ψ(θ) [ 0 , π ] [0,\pi] [0,π] 这段区间,因为 c o s ( θ ) cos(\theta) cos(θ) 是周期为 π \pi π 函数,可以这么如下分析:

  • 0 ≤ θ ≤ π m 0\leq \theta \leq\cfrac{\pi}{m} 0θmπ 时, ψ ( θ ) = c o s ( m θ ) \psi(\theta)=cos(m\theta) ψ(θ)=cos(mθ)
  • π m < θ ≤ 2 π m \cfrac{\pi}{m}<\theta\leq \cfrac{2\pi}{m} mπ<θm2π , ψ ( θ ) = − c o s ( m θ ) − 2 \psi(\theta)= -cos(m\theta)-2 ψ(θ)=cos(mθ)2 也就是 c o s ( m θ ) cos(m\theta) cos(mθ) 在区间 [ π m , 2 π m ] [\cfrac{\pi}{m}, \cfrac{2\pi}{m}] [mπ,m2π] 对称反转,在向下平移2个单位,与 [ 0 , π m ] [0, \cfrac{\pi}{m}] [0,mπ] 这段曲线的右端点相接。
  • 2 π m < θ ≤ 3 π m \cfrac{2\pi}{m}<\theta\leq \cfrac{3\pi}{m} m2π<θm3π ψ ( θ ) = c o s ( m θ ) − 4 \psi(\theta)= cos(m\theta)-4 ψ(θ)=cos(mθ)4 ,自己画图就可以分析出来;

最终得出论文中 ψ ( θ ) \psi(\theta) ψ(θ) 的一般形式:

ψ ( θ ) = ( − 1 ) k cos ⁡ ( m θ ) − 2 k , θ ∈ [ k π m , ( k + 1 ) π m ] (6) \psi(\theta) = (-1)^k \cos(m\theta) - 2k, \theta \in [\cfrac{k\pi}{m}, \cfrac{(k+1) \pi}{m}] \tag6 ψ(θ)=(1)kcos(mθ)2k,θ[m,m(k+1)π](6)

  • 其中 k + 1 ≤ m k+1\leq m k+1m ,所以 k ∈ [ 0 , m − 1 ] k∈[0,m−1] k[0,m1] 同时 k k k 是整数。下图是不同 m m m 对应的 ψ ( θ ) ψ(θ) ψ(θ)曲线
    在这里插入图片描述

在正向传播和反向传播中,我们要计算 c o s ( θ y i ) = W y i x i ∣ ∣ W y i ∣ ∣ ∣ ∣ x i ∣ ∣ cos(\theta_{yi})=\cfrac{W_{yi}x_{i}}{\left| \left| W_{yi} \right| \right|\left| \left| x_{i}\right| \right|} cos(θyi)=WyixiWyixi ,然后计算 c o s ( m θ y i ) cos(m\theta_{yi}) cos(mθyi)

cos ⁡ ( m θ i ) \cos (m\theta_i) cos(mθi)由下面公式计算
在这里插入图片描述
m = 1 m=1 m=1 时,L-Softmax Loss就是原来的Softmax Loss。

三、L-Softmax损失函数几何解释

1、 ∣ ∣ W 1 ∣ ∣ = ∣ ∣ W 2 ∣ ∣ \left| \left| W_{1} \right| \right|=\left| \left| W_{2} \right| \right| W1=W2 的情形

为了简单起见,只分析二分类情况,首先考虑当 ∣ ∣ W 1 ∣ ∣ = ∣ ∣ W 2 ∣ ∣ \left| \left| W_{1} \right| \right|=\left| \left| W_{2} \right| \right| W1=W2 的特殊情形。如图:

在这里插入图片描述

x x x 与向量 W 1 W_{1} W1 W 2 W_{2} W2 的角度决定了分类结果,

  • 原始的Softmax如果 θ 1 < θ 2 \theta_{1}<\theta_{2} θ1<θ2 ,则判定为类别 1 1 1,分类边界即为 W 1 W_{1} W1 W 2 W_{2} W2 的角平分线。
  • L-softmax则需要 m θ 1 < θ 2 m\theta_{1}<\theta_{2} mθ1<θ2 ,这导致出现两个分类决策边界,不同类的样本紧凑的聚集在一个很小的角度范围,两个决策边界有个间隔也就是Margin。
    • 对于第1类样本的分类边界
      m θ 1 = θ 2 , θ 1 + θ 2 = θ 12 ⇒ θ 1 = θ 12 1 + m m\theta_{1}=\theta_{2},\theta_{1}+\theta_{2}=\theta_{12}\Rightarrow \theta_{1}=\cfrac{\theta_{12}}{1+m} mθ1=θ2,θ1+θ2=θ12θ1=1+mθ12
    • 对于第2类样本的分类边界
      m θ 2 = θ 1 , θ 1 + θ 2 = θ 12 ⇒ θ 2 = θ 12 1 + m m\theta_{2}=\theta_{1},\theta_{1}+\theta_{2}=\theta_{12}\Rightarrow \theta_{2}=\cfrac{\theta_{12}}{1+m} mθ2=θ1,θ1+θ2=θ12θ2=1+mθ12
  • 角度 Margin:
    M a r g i n = θ 12 − 2 θ 12 m + 1 = m − 1 m + 1 θ 12 , θ 12 是 W 1 和 W 2 的夹角 Margin = \theta_{12}-2\cfrac{\theta_{12}}{m+1}=\cfrac{m-1}{m+1}\theta_{12},\quad \theta_{12} 是 W_{1}和 W_{2} 的夹角 Margin=θ122m+1θ12=m+1m1θ12,θ12W1W2的夹角

总的来说,L-softmax会缩小每个类别的可行角度(Feasible angle)范围,在这些类别之间产生角度Magin。

Feasible angle of the i-th class:refers to the possible angle between x x x and W i W_i Wi that is learned by CNNs.

2、 ∣ ∣ W 1 ∣ ∣ ≠ ∣ ∣ W 2 ∣ ∣ \left| \left| W_{1} \right| \right|\ne\left| \left| W_{2} \right| \right| W1=W2 的情况

对于 ∣ ∣ W 1 ∣ ∣ ≠ ∣ ∣ W 2 ∣ ∣ \left| \left| W_{1} \right| \right|\ne\left| \left| W_{2} \right| \right| W1=W2 的情况,类别 1 1 1 和类别 2 2 2 的可行角度范围发生变化, W j W_{j} Wj 越大,相应类的可行角度范围越大,L-Softmax损失对于不同类别也产生了不同的可行角度范围。这其实是个需要改进的缺陷,以后的论文会提出权重的归一化概念。

在这里插入图片描述

四、激活函数

论文中用的网络采用的是PReLU激活函数,收敛性比ReLU更好,具有BN归一化层,没有DropOut,用随机梯度下降优化就可以,但是当训练数据样本类别比较复杂的时候比如(CASIA-WebFace),L-softmax收敛变得比Softmax困难,需要做如下改变:

在这里插入图片描述

当训练刚开始设置 λ \lambda λ 比较大,近似于传统的Softmax,然后在迭代中不断慢慢减小 λ \lambda λ ,当后面训练阶段很小时候,近似于L-softmax。对于简单的数据集比如Mnist、cifar, λ \lambda λ 最小值可以取0,对于大型困难数据集, λ \lambda λ 最小值设置为5或者10。可以在训练的初始阶段采用传统的Softmax训练,最后微调阶段用L-softmax。

五、总结

  1. L-softmax有较为清晰的几何解释,由m控制着类别间的margin大小,m越大,类别间的margin越大,分类就越理想,学习也就会越困难;
  2. 定义了相对较难的学习目标,能够有效避免过拟合;
  3. 可以作为标准损失的替换,也可以与其他性能提升方法和模块一起使用,使用灵活。
  4. 有使用L-Softmax的同学反映训练难度比较大,需要反复调参。



参考资料:
Large-Margin Softmax Loss 论文笔记
Large-Margin Softmax Loss
Large-Margin Softmax Loss
深度学习–Large Marge Softmax Loss损失
Softmax loss 之 Large Margin Softmax(L-softmax)
Additive Margin Softmax Loss (AM-Softmax)


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部