序列最小优化Sequential Minimal Optimization

项目地址:https://github.com/Daya-Jin/ML_for_learner/blob/master/svm/SMO.ipynb
原博客:https://daya-jin.github.io/2019/03/24/SequentialMinimalOptimization/

算法概述

在之前讲解SVM博客中,分析了SVM模型的理论基础与优化目标,并且讨论了SVM在达到最优解时的一些性质。但是前文中并没有提及SVM目标函数的优化方法,本文的目的就是讨论二次优化算法SMO用于SVM的学习。因为SMO算法涉及到的很多数学知识已超出本文范畴,某些地方只给出直接结论。

首先回顾SVM的优化目标为:

min ⁡ λ   L ( λ ) = 1 2 ∑ i = 1 m ∑ j = 1 m λ i λ j y i y j x i x j T − ∑ i = 1 m λ i s . t .   0 ≤ λ i ≤ C ,   ∑ λ i y i = 0 \begin{aligned} \min\limits_{\lambda}\ L(\lambda)&=\frac{1}{2}\sum_{i=1}^{m}\sum_{j=1}^{m}\lambda_{i}\lambda_{j}y^{i}y^{j}x^{i}{x^{j}}^{T}-\sum_{i=1}^{m}\lambda_{i} \\ s.t. \ & 0\le\lambda_{i}\le{C}, \ \sum\limits\lambda_{i}y^{i}=0 \end{aligned} λmin L(λ)s.t. =21i=1mj=1mλiλjyiyjxixjTi=1mλi0λiC, λiyi=0

为了将核函数加入进来,将目标函数中两训练样本的内积替换成核函数的形式:

min ⁡ λ   L ( λ ) = 1 2 ∑ i = 1 m ∑ j = 1 m λ i λ j y i y j κ i j − ∑ i = 1 m λ i s . t .   0 ≤ λ i ≤ C ,   ∑ λ i y i = 0 \begin{aligned} \min\limits_{\lambda}\ L(\lambda)&=\frac{1}{2}\sum_{i=1}^{m}\sum_{j=1}^{m}\lambda_{i}\lambda_{j}y^{i}y^{j}\kappa_{ij}-\sum_{i=1}^{m}\lambda_{i} \\ s.t. \ & 0\le\lambda_{i}\le{C}, \ \sum\limits\lambda_{i}y^{i}=0 \end{aligned} λmin L(λ)s.t. =21i=1mj=1mλiλjyiyjκiji=1mλi0λiC, λiyi=0

SMO算法的核心思想是:每次只选取一对参数进行优化。假设在上述目标中,我们只令 λ a \lambda_{a} λa λ b \lambda_{b} λb为参数,其他 λ \lambda λ为常数,那么优化问题可以写成:

min ⁡ λ a , λ b 1 2 λ a 2 y ( a ) 2 κ a a + 1 2 λ b 2 y ( b ) 2 κ b b + 1 2 λ a y a ∑ i ≠ a λ i y i κ a i + 1 2 λ b y b ∑ i ≠ b λ i y i κ b i − λ a − λ b − ∑ i ≠ a , b λ i s . t .   0 ≤ λ a , b ≤ C ,   λ a y a + λ b y b = − ∑ i ≠ a , b λ i y i \begin{aligned} \min\limits_{\lambda_{a},\lambda_{b}} & \frac{1}{2}\lambda_{a}^{2}{y^{(a)}}^{2}\kappa_{aa}+\frac{1}{2}\lambda_{b}^{2}{y^{(b)}}^{2}\kappa_{bb}+\frac{1}{2}\lambda_{a}y^{a}\sum\limits_{i{\ne}a}\lambda_{i}y^{i}\kappa_{ai}+\frac{1}{2}\lambda_{b}y^{b}\sum\limits_{i{\ne}b}\lambda_{i}y^{i}\kappa_{bi}-\lambda_{a}-\lambda_{b}-\sum\limits_{i{\ne}a,b}\lambda_{i} \\ s.t. \ & 0\le\lambda_{a,b}\le{C}, \ \lambda_{a}y^{a}+\lambda_{b}y^{b}=-\sum\limits_{i{\ne}a,b}\lambda_{i}y^{i} \\ \end{aligned} λa,λbmins.t. 21λa2y(a)2κaa+21λb2y(b)2κbb+21λayai̸=aλiyiκai+21λbybi̸=bλiyiκbiλaλbi̸=a,bλi0λa,bC, λaya+λbyb=i̸=a,bλiyi

去除无关常量,简化后的优化目标可以写成:

min ⁡ λ a , λ b 1 2 λ a 2 κ a a + 1 2 λ b 2 κ b b + 1 2 λ a y a ∑ i ≠ a λ i y i κ a i + 1 2 λ b y b ∑ i ≠ b λ i y i κ b i − λ a − λ b s . t .   0 ≤ λ a , b ≤ C ,   λ a y a + λ b y b = − ∑ i ≠ a , b λ i y i \begin{aligned} \min\limits_{\lambda_{a},\lambda_{b}} & \frac{1}{2}\lambda_{a}^{2}\kappa_{aa}+\frac{1}{2}\lambda_{b}^{2}\kappa_{bb}+\frac{1}{2}\lambda_{a}y^{a}\sum\limits_{i{\ne}a}\lambda_{i}y^{i}\kappa_{ai}+\frac{1}{2}\lambda_{b}y^{b}\sum\limits_{i{\ne}b}\lambda_{i}y^{i}\kappa_{bi}-\lambda_{a}-\lambda_{b} \\ s.t. \ & 0\le\lambda_{a,b}\le{C}, \ \lambda_{a}y^{a}+\lambda_{b}y^{b}=-\sum\limits_{i{\ne}a,b}\lambda_{i}y^{i} \\ \end{aligned} λa,λbmins.t. 21λa2κaa+21λb2κbb+21λayai̸=aλiyiκai+21λbybi̸=bλiyiκbiλaλb0λa,bC, λaya+λbyb=i̸=a,bλiyi

在前文中提过SVM在优化后的一些性质,如对于分类正确的样本,其对应的 λ i \lambda_{i} λi是等于 0 0 0的,同样的,那么对于软间隔SVM,不难推出优化后的几个性质:

样本分类情况 对应的 λ \lambda λ
y i ( x i θ T + θ 0 ) ≥ 1 y^{i}(x^{i}\theta^{T}+\theta_{0}){\ge}1 yi(xiθT+θ0)1 λ i = 0 \lambda_{i}=0 λi=0
y i ( x i θ T + θ 0 ) ≤ 1 y^{i}(x^{i}\theta^{T}+\theta_{0}){\le}1 yi(xiθT+θ0)1 λ i = C \lambda_{i}=C λi=C
y i ( x i θ T + θ 0 ) = 1 y^{i}(x^{i}\theta^{T}+\theta_{0})=1 yi(xiθT+θ0)=1 0 < λ i < C 0<\lambda_{i}<C 0<λi<C

优化策略

SMO每次只选取一对 λ \lambda λ视为参数,假设先选定 λ a \lambda_{a} λa,那么 λ b \lambda_{b} λb的优化公式为:

λ b : = λ b − y b ( ( y ^ a − y a ) − ( y ^ b − y b ) ) 2 κ a b − κ a a − κ b b = λ b − y b ( E a − E b ) η \begin{aligned} \lambda_{b}:&=\lambda_{b}-\frac{y^{b}((\hat{y}^{a}-y^{a})-(\hat{y}^{b}-y^{b}))}{2\kappa_{ab}-\kappa_{aa}-\kappa_{bb}} \\ &=\lambda_{b}-\frac{y^{b}(E_{a}-E_{b})}{\eta} \\ \end{aligned} λb:=λb2κabκaaκbbyb((y^aya)(y^byb))=λbηyb(EaEb)

然后再看优化问题中的约束条件 λ a y a + λ b y b = − ∑ i ≠ a , b λ i y i \lambda_{a}y^{a}+\lambda_{b}y^{b}=-\sum\limits_{i{\ne}a,b}\lambda_{i}y^{i} λaya+λbyb=i̸=a,bλiyi,由于只有 λ a \lambda_{a} λa λ b \lambda_{b} λb是参数,那么该优化条件还可以写成: λ a y a + λ b y b = ξ \lambda_{a}y^{a}+\lambda_{b}y^{b}=\xi λaya+λbyb=ξ。而 y a y^{a} ya y b y^{b} yb的可能取值为 { − 1 , + 1 } \{-1,+1\} {1,+1},由几何方法可以得到优化参数 λ \lambda λ的一个上下界:

  • y a ≠ y b y^{a}{\ne}y^{b} ya̸=yb L = max ⁡ ( 0 , λ b − λ a ) L=\max(0,\lambda_{b}-\lambda_{a}) L=max(0,λbλa) H = min ⁡ ( C , C + λ b − λ a ) H=\min(C,C+\lambda_{b}-\lambda_{a}) H=min(C,C+λbλa)
  • y a = y b y^{a}=y^{b} ya=yb L = max ⁡ ( 0 , λ a + λ b − C ) L=\max(0,\lambda_{a}+\lambda_{b}-C) L=max(0,λa+λbC) H = min ⁡ ( C , λ b + λ a ) H=\min(C,\lambda_{b}+\lambda_{a}) H=min(C,λb+λa)

所以,在优化之后,还需要检验 λ b \lambda_{b} λb是否还符合约束条件,若不满足,则需要做截断处理:

λ b = { H if  λ b > H λ b if  L < λ b < H L if  λ b < L \lambda_{b}= \begin{cases} H & \text{if $\lambda_{b}>H$} \\ \lambda_{b} & \text{if $L<\lambda_{b}<H$} \\ L & \text{if $\lambda_{b}<L$} \\ \end{cases} λb=HλbLif λb>Hif L<λb<Hif λb<L

λ a \lambda_{a} λa的优化公式为:

λ a : = λ a − y a y b Δ λ b \lambda_{a}:=\lambda_{a}-y^{a}y^{b}\Delta\lambda_{b} λa:=λayaybΔλb

其中 Δ λ b = λ b n e w − λ b o l d \Delta\lambda_{b}=\lambda_{b}^{new}-\lambda_{b}^{old} Δλb=λbnewλbold

针对任一一个 λ \lambda λ,若 λ \lambda λ在边界范围 ( 0 , C ) (0,C) (0,C)内,可以推出对应的 θ 0 , k \theta_{0,k} θ0,k

θ 0 , k = θ 0 − E k − y a Δ λ a κ a k − y b Δ λ b κ k b k = a , b \theta_{0,k}=\theta_{0}-E_{k}-y^{a}\Delta\lambda_{a}\kappa_{ak}-y^{b}\Delta\lambda_{b}\kappa_{kb} \qquad k={a,b} θ0,k=θ0EkyaΔλaκakybΔλbκkbk=a,b

那么将其写成一个条件函数,可得到 θ 0 \theta_{0} θ0的迭代优化公式:

θ 0 : = { θ 0 , a if  0 < λ a < C θ 0 , b if  0 < λ b < C ( θ 0 , a + θ 0 , b ) / 2 otherwise \theta_{0}:= \begin{cases} \theta_{0,a} & \text{if $0<\lambda_{a}<C$} \\ \theta_{0,b} & \text{if $0<\lambda_{b}<C$} \\ (\theta_{0,a}+\theta_{0,b})/2 & \text{otherwise} \\ \end{cases} θ0:=θ0,aθ0,b(θ0,a+θ0,b)/2if 0<λa<Cif 0<λb<Cotherwise

实现指导

你可能感兴趣的:(机器学习笔记,ML,for,leaner)