我们现在的问题是要优化目标函数,同时求出参数向量 α \alpha α
P = m i n ⏟ α 1 2 ∑ i = 1 , j = 1 m α i α j y i y j K ( x i , x j ) − ∑ i = 1 m α i s . t . ∑ i = 1 m α i y i = 0 0 ≤ α i ≤ C P=\underbrace{ min }{\alpha} \frac{1}{2}\sum\limits{i=1,j=1}^{m}\alpha_i\alpha_jy_iy_jK(x_i,x_j) - \sum\limits_{i=1}^{m}\alpha_i\\ s.t. \; \sum\limits_{i=1}^{m}\alpha_iy_i = 0\\ 0 \leq \alpha_i \leq C P=α min21i=1,j=1∑mαiαjyiyjK(xi,xj)−i=1∑mαis.t.i=1∑mαiyi=00≤αi≤C
因为现在的问题是原来的对偶问题化简而来的所以 α \alpha α还应该满足对偶互补条件 α i ( y i ( w T x i + b ) − 1 + ξ i ∗ ) = 0 \alpha_{i}(y_i(w^Tx_i + b) - 1 + \xi_i^{*}) = 0 αi(yi(wTxi+b)−1+ξi∗)=0
根据 α \alpha α的取值范围和对偶互补条件我们可以分析出 α \alpha α的情况:
α i = 0 ⇒ y i ( w ∙ ϕ ( x i ) + b ) ≥ 1 0 < α i < C ⇒ y i ( w ∙ ϕ ( x i ) + b ) = 1 α i = C ⇒ y i ( w ∙ ϕ ( x i ) + b ) ≤ 1 \alpha_{i} = 0 \Rightarrow y_i(w \bullet \phi(x_i) + b) \geq 1\\ 0 <\alpha_{i}< C \Rightarrow y_i(w \bullet \phi(x_i) + b) = 1\\ \alpha_{i}= C \Rightarrow y_i(w \bullet \phi(x_i) + b) \leq 1 αi=0⇒yi(w∙ϕ(xi)+b)≥10<αi<C⇒yi(w∙ϕ(xi)+b)=1αi=C⇒yi(w∙ϕ(xi)+b)≤1
为了方面表示我们做出一些字符替换:
g ( x ) = w ∙ ϕ ( x ) + b = ∑ j = 1 m α j y j K ( x , x j ) + b K i j = ϕ ( x i ) ∙ ϕ ( x j ) E i = g ( x i ) − y i = ∑ j = 1 m α j y j K ( x i , x j ) + b − y i g(x) = w\bullet \phi(x) + b =\sum\limits_{j=1}^{m}\alpha_jy_jK(x, x_j)+ b \\ K_{ij} = \phi(x_i) \bullet \phi(x_j) \\ E_i = g(x_i)-y_i = \sum\limits_{j=1}^{m}\alpha_jy_jK(x_i, x_j)+ b - y_i g(x)=w∙ϕ(x)+b=j=1∑mαjyjK(x,xj)+bKij=ϕ(xi)∙ϕ(xj)Ei=g(xi)−yi=j=1∑mαjyjK(xi,xj)+b−yi
2.3.2.1 思路
我们的目标是求出m个 α i \alpha_i αi的值,如果同时去优化很难去得出结果,所以SMO选择和坐标下降算法类似的思路,选择两个参数先进行优化,将其他参数暂时看为常数,这样就能将问题简化为两变量的优化问题。我们假设除了 α 1 , α 2 \alpha_1,\alpha_2 α1,α2外的都是常数我们可以得到约束条件:
α 1 y 1 + α 2 y 2 + ∑ i = 3 m α i y i = 0 α 1 y 1 + α 2 y 2 = − ∑ i = 3 m α i y i S e t ς = − ∑ i = 3 m α i y i ∴ α 1 y 1 + α 2 y 2 = ς \alpha_1y_1+ \alpha_2y_2+\sum_{i=3}^m \alpha_iy_i =0\\ \alpha_1y_1+ \alpha_2y_2=-\sum_{i=3}^m \alpha_iy_i \\ Set \quad \varsigma=-\sum_{i=3}^m \alpha_iy_i \\ \therefore \alpha_1y_1+ \alpha_2y_2=\varsigma α1y1+α2y2+i=3∑mαiyi=0α1y1+α2y2=−i=3∑mαiyiSetς=−i=3∑mαiyi∴α1y1+α2y2=ς
同时我们从原问题P中将有 α 1 , α 2 \alpha_1,\alpha_2 α1,α2的项提取出来:
P = m i n ⏟ α 1 2 ∑ i = 1 , j = 1 m α i α j y i y j K i j − ∑ i = 1 m α i = m i n ⏟ α 1 2 [ α 1 2 K 11 + α 2 2 K 22 + 2 α 1 α 2 y 1 y 2 K 12 + y 1 α 1 ∑ i = 3 m y i α i K i 1 + y 2 α 2 ∑ i = 3 m y i α i K i 2 ] − α 1 − α 2 − ∑ i = 3 m α i = m i n ⏟ α 1 , α 1 1 2 K 11 α 1 2 + 1 2 K 22 α 2 2 + y 1 y 2 K 12 α 1 α 2 − ( α 1 + α 2 ) + y 1 α 1 ∑ i = 3 m y i α i K i 1 + y 2 α 2 ∑ i = 3 m y i α i K i 2 P=\underbrace{ min }{\alpha} \frac{1}{2}\sum\limits{i=1,j=1}^{m}\alpha_i\alpha_jy_iy_jK_{ij} - \sum\limits_{i=1}^{m}\alpha_i\\ =\underbrace{ min }{\alpha} \frac{1}{2}[\alpha_1^2K{11}+\alpha_2^2K_{22}+2\alpha_1\alpha_2y_1y_2K_{12}+y_1\alpha_1\sum\limits_{i=3}^{m}y_i\alpha_iK_{i1} + y_2\alpha_2\sum\limits_{i=3}^{m}y_i\alpha_iK_{i2}] -\alpha_1-\alpha_2-\sum_{i=3}^m \alpha_i\\ =\;\underbrace{ min }{\alpha_1, \alpha_1} \frac{1}{2}K{11}\alpha_1^2 + \frac{1}{2}K_{22}\alpha_2^2 +y_1y_2K_{12}\alpha_1 \alpha_2 -(\alpha_1 + \alpha_2) +y_1\alpha_1\sum\limits_{i=3}^{m}y_i\alpha_iK_{i1} + y_2\alpha_2\sum\limits_{i=3}^{m}y_i\alpha_iK_{i2} P=α min21i=1,j=1∑mαiαjyiyjKij−i=1∑mαi=α min21[α12K11+α22K22+2α1α2y1y2K12+y1α1i=3∑myiαiKi1+y2α2i=3∑myiαiKi2]−α1−α2−i=3∑mαi=α1,α1 min21K11α12+21K22α22+y1y2K12α1α2−(α1+α2)+y1α1i=3∑myiαiKi1+y2α2i=3∑myiαiKi2
接下来我们就是要求解这个二元优化问题:
首先我们要讨论 α 1 , α 2 \alpha_1,\alpha_2 α1,α2的范围, α 1 和 α 2 \alpha_1和\alpha_2 α1和α2是二元线性关系$ \alpha_1y_1+ \alpha_2y_2=\varsigma ,并且 ,并且 ,并且\alpha$的取值范围为 0 ≤ α i ≤ C 0 \leq \alpha_i \leq C 0≤αi≤C,同时y只取{+1和-1},其实我们可以将线性关系分为四种情况去讨论:
I F : y 1 = = y 2 α 1 + α 2 = k k > 0 o r k < 0 I F : y 1 ! = y 2 α 1 − α 2 = k k > 0 o r k < 0 IF :y_1==y_2\\ \alpha_1 +\alpha_2=k \quad k>0 \quad or \quad k <0 \\IF :y_1!=y_2 \\ \alpha_1 -\alpha_2=k \quad k>0 \quad or \quad k <0 IF:y1==y2α1+α2=kk>0ork<0IF:y1!=y2α1−α2=kk>0ork<0
放到坐标系中如图所示:
由于 α 1 , α 2 α_1,α_2 α1,α2的关系被限制在盒子里的一条线段上,所以两变量的优化问题实际上仅仅是一个变量的优化问题,我们可以直接视为是对变量 α 2 \alpha_2 α2的优化。
为了求出 α 2 \alpha_2 α2的值,我们应该先求出 α 2 \alpha_2 α2的定义域,我们假设 α 2 \alpha_2 α2的左边界为L右边界为H。由于 α 1 , α 2 α_1,α_2 α1,α2的取值其实是线上的动点问题,所以我们先研究 α 2 n e w \alpha_2^{new} α2new边界的取值。
对于左图来说L和H的取值范围是:
L = m a x ( 0 , α 2 o l d − α 1 o l d ) H = m i n ( C , C + α 2 o l d − α 1 o l d ) L = max(0, \alpha_2^{old}-\alpha_1^{old}) \;\;\;H = min(C, C+\alpha_2^{old}-\alpha_1^{old}) L=max(0,α2old−α1old)H=min(C,C+α2old−α1old)
推导:
∵ α 1 = α 2 + k 又 ∵ 0 ≤ α 1 ≤ C ∴ 0 ≤ α 2 + k ≤ C ∵ α 2 + k ≥ 0 且 α 2 ≥ 0 ∴ α 2 ≥ m a x { − k , 0 } = m a x { α 2 o l d − α 1 o l d , 0 } ∵ α 2 ≤ C − k 且 α 2 ≤ C ∴ α 2 ≤ m i n { C , C − K } ∴ L = m a x ( 0 , α 2 o l d − α 1 o l d ) H = m i n ( C , C + α 2 o l d − α 1 o l d ) \because \alpha_1 = \alpha_2 +k\\ 又\because 0\leq \alpha_1 \leq C \\\therefore 0\leq \alpha_2 +k \leq C \\ \because \alpha_2 +k \geq 0 且 \alpha_2 \geq 0 \\ \therefore \alpha_2 \geq max\{ -k,0\}=max\{ \alpha_2^{old} - \alpha_1^{old},0\} \\ \because \alpha_2 \leq C-k且 \alpha_2 \leq C \\ \therefore \alpha_2 \leq min\{C,C-K\} \\ \therefore L = max(0, \alpha_2^{old}-\alpha_1^{old}) \;\;\;H = min(C, C+\alpha_2^{old}-\alpha_1^{old}) ∵α1=α2+k又∵0≤α1≤C∴0≤α2+k≤C∵α2+k≥0且α2≥0∴α2≥max{−k,0}=max{α2old−α1old,0}∵α2≤C−k且α2≤C∴α2≤min{C,C−K}∴L=max(0,α2old−α1old)H=min(C,C+α2old−α1old)
对于右图来说,推导同理:
L = m a x ( 0 , α 2 o l d + α 1 o l d − C ) H = m i n ( C , α 2 o l d + α 1 o l d ) L = max(0, \alpha_2^{old}+\alpha_1^{old}-C) \;\;\; H = min(C, \alpha_2^{old}+\alpha_1^{old}) L=max(0,α2old+α1old−C)H=min(C,α2old+α1old)
我们得到最终 α 2 \alpha_2 α2的取值函数:
α 2 n e w = { H α 2 n e w , u n c > H α 2 n e w , u n c L ≤ α 2 n e w , u n c ≤ H L α 2 n e w , u n c < L \alpha_2^{new}= \begin{cases} H& { \alpha_2^{new,unc} > H}\\ \alpha_2^{new,unc}& {L \leq \alpha_2^{new,unc} \leq H}\\ L& {\alpha_2^{new,unc} < L} \end{cases} α2new=⎩ ⎨ ⎧Hα2new,uncLα2new,unc>HL≤α2new,unc≤Hα2new,unc<L
进一步我们优化函数为:
W ( α 1 , α 2 ) = 1 2 K 11 α 1 2 + 1 2 K 22 α 2 2 + y 1 y 2 K 12 α 1 α 2 − ( α 1 + α 2 ) + y 1 α 1 v 1 + y 2 α 2 v 2 W(\alpha_1,\alpha_2) = \frac{1}{2}K_{11}\alpha_1^2 + \frac{1}{2}K_{22}\alpha_2^2 +y_1y_2K_{12}\alpha_1 \alpha_2 -(\alpha_1 + \alpha_2) +y_1\alpha_1v_1 + y_2\alpha_2v_2 W(α1,α2)=21K11α12+21K22α22+y1y2K12α1α2−(α1+α2)+y1α1v1+y2α2v2
代入 α 1 = y 1 ( ς − α 2 y 2 ) \alpha_1 = y_1(\varsigma - \alpha_2y_2) α1=y1(ς−α2y2),消去 α 1 \alpha_1 α1
最终我们得到纯 α 2 \alpha_2 α2的函数:
W ( α 2 ) = 1 2 K 11 ( ς − α 2 y 2 ) 2 + 1 2 K 22 α 2 2 + y 2 K 12 ( ς − α 2 y 2 ) α 2 − ( ς − α 2 y 2 ) y 1 − α 2 + ( ς − α 2 y 2 ) v 1 + y 2 α 2 v 2 W(\alpha_2) = \frac{1}{2}K_{11}(\varsigma - \alpha_2y_2)^2 + \frac{1}{2}K_{22}\alpha_2^2 +y_2K_{12}(\varsigma - \alpha_2y_2) \alpha_2 - (\varsigma - \alpha_2y_2)y_1 - \alpha_2 +(\varsigma - \alpha_2y_2)v_1 + y_2\alpha_2v_2 W(α2)=21K11(ς−α2y2)2+21K22α22+y2K12(ς−α2y2)α2−(ς−α2y2)y1−α2+(ς−α2y2)v1+y2α2v2
对 α 2 \alpha_2 α2求导为0得到 α 2 n e w , u n c \alpha_2^{new,unc} α2new,unc:
α 2 n e w , u n c = α 2 o l d + y 2 ( E 1 − E 2 ) K 11 + K 22 − 2 K 12 ) \alpha_2^{new,unc} = \alpha_2^{old} + \frac{y_2(E_1-E_2)}{K_{11} +K_{22}-2K_{12})} α2new,unc=α2old+K11+K22−2K12)y2(E1−E2)
然后将 α 2 n e w , u n c \alpha_2^{new,unc} α2new,unc代入取值函数则可得到最终的 α 2 n e w \alpha_2^{new} α2new,同时根据 α 1 和 α 2 \alpha_1和\alpha_2 α1和α2的线性关系: α 1 y 1 + α 2 y 2 = ς \alpha_1y_1+ \alpha_2y_2=\varsigma α1y1+α2y2=ς可以求出 α 1 \alpha_1 α1
2.3.2.2 如何选择变量
一、第一个变量选择违反KKT条件最严重的点:
KKT条件为:
α i = 0 ⇒ y i g ( x i ) ≥ 1 0 < α i < C ⇒ y i g ( x i ) = 1 α i = C ⇒ y i g ( x i ) ≤ 1 \alpha_{i} = 0 \Rightarrow y_ig(x_i) \geq 1 \\0 < \alpha_{i} < C \Rightarrow y_ig(x_i) =1 \\\alpha_{i}= C \Rightarrow y_ig(x_i) \leq 1 αi=0⇒yig(xi)≥10<αi<C⇒yig(xi)=1αi=C⇒yig(xi)≤1
我们一般会选择违反 0 < α i ∗ < C ⇒ y i g ( x i ) = 1 0 < \alpha_{i}^{*} < C \Rightarrow y_ig(x_i) =1 0<αi∗<C⇒yig(xi)=1这个条件的点,这是因为:对于 α i = 0 \alpha_i =0 αi=0则意味着分类正确,这个点对于超平面的调整无效。对于 α i = C \alpha_{i}= C αi=C的点则代表点可能在错误间隔或者分类错误,对于这种情况其实无论怎么调整参数都无法优化超平面。
对于违反0 \< \\alpha_{i} \< C 的点,此时意味着 的点,此时意味着 的点,此时意味着\\alpha_i \>C \\quad OR\\quad \\alpha_i \<0,我们很清楚 0 ≤ α i ≤ C 0 \leq \alpha_{i} \leq C 0≤αi≤C,正常情况下不应该出现这种情况,进而说明KKT条件被违反,优化这些点能够更有效的让超平面向符合KKT条件的方向靠拢。
二、第二个变量选择 ∣ E 1 − E 2 ∣ \vert E_1-E_2\vert ∣E1−E2∣足够大的点:
E i = f ( x i ) − y i E_i =f(x_i)-y_i Ei=f(xi)−yi代表预测值和真实值之间的误差,我们先选择好 α 1 \alpha_1 α1计算出 E 1 E_1 E1,然后再在剩下的点中算出所有的 E i E_i Ei,对比这些 E 1 − E i E_1 -E_i E1−Ei,较大的误差差说明模型在这两个点附近的差异更大,调整 α \alpha α 可以让模型更有效的向正确的分类移动,进而加速收敛速度
E 1 − E i E_1-E_i E1−Ei 较大时,可以使得分类超平面向调整幅度大的方向走。
三、更新 E i E_i Ei和b
∵ y 1 − ∑ i = 1 m α i y i K i 1 − b 1 = 0 ∴ b 1 n e w = y 1 − ∑ i = 3 m α i y i K i 1 − α 1 n e w y 1 K 11 − α 2 n e w y 2 K 21 ∴ E 1 = g ( x 1 ) − y 1 = ∑ i = 3 m α i y i K i 1 + α 1 o l d y 1 K 11 + α 2 o l d y 2 K 21 + b o l d − y 1 联立两式 b 1 n e w = − E 1 − y 1 K 11 ( α 1 n e w − α 1 o l d ) − y 2 K 21 ( α 2 n e w − α 2 o l d ) + b o l d 同理 : b 2 n e w = − E 2 − y 1 K 12 ( α 1 n e w − α 1 o l d ) − y 2 K 22 ( α 2 n e w − α 2 o l d ) + b o l d ∴ b n e w = b 1 n e w + b 2 n e w 2 更新 : E i = ∑ S y j α j K ( x i , x j ) + b n e w − y i \because y_1 - \sum\limits_{i=1}^{m}\alpha_iy_iK_{i1} -b_1 = 0\\ \therefore b_1^{new} = y_1 - \sum\limits_{i=3}^{m}\alpha_iy_iK_{i1} - \alpha_{1}^{new}y_1K_{11} - \alpha_{2}^{new}y_2K_{21}\\ \therefore E_1 = g(x_1) - y_1 = \sum\limits_{i=3}^{m}\alpha_iy_iK_{i1} + \alpha_{1}^{old}y_1K_{11} + \alpha_{2}^{old}y_2K_{21} + b^{old} -y_1 \\ 联立两式\\ b_1^{new} = -E_1 -y_1K_{11}(\alpha_{1}^{new} - \alpha_{1}^{old}) -y_2K_{21}(\alpha_{2}^{new} - \alpha_{2}^{old}) + b^{old}\\ 同理:\\ b_2^{new} = -E_2 -y_1K_{12}(\alpha_{1}^{new} - \alpha_{1}^{old}) -y_2K_{22}(\alpha_{2}^{new} - \alpha_{2}^{old}) + b^{old} \\ \therefore b^{new} = \frac{b_1^{new} + b_2^{new}}{2}\\ 更新:E_i = \sum\limits_{S}y_j\alpha_jK(x_i,x_j) + b^{new} -y_i ∵y1−i=1∑mαiyiKi1−b1=0∴b1new=y1−i=3∑mαiyiKi1−α1newy1K11−α2newy2K21∴E1=g(x1)−y1=i=3∑mαiyiKi1+α1oldy1K11+α2oldy2K21+bold−y1联立两式b1new=−E1−y1K11(α1new−α1old)−y2K21(α2new−α2old)+bold同理:b2new=−E2−y1K12(α1new−α1old)−y2K22(α2new−α2old)+bold∴bnew=2b1new+b2new更新:Ei=S∑yjαjK(xi,xj)+bnew−yi
2.3.2.2 算法
输入是m个样本( $ x_ {1} $ , $ y_ {1} $ ),( $ x_ {2} $ , $ y_ {2} $ ), $ \cdots $ ,( $ x_ {m} $ , $ y_ {m} $ ),其中x为n维特征向量。y为二元输出,值为1,或者-1
输出是近似解 $ \alpha $
-
取初值 $ \alpha ^ {0} $ =0,k=0
-
选出 α 1 和 α 2 \alpha_1和\alpha_2 α1和α2
-
求出 α 2 n e w , u n c \alpha _ {2}^ {new,unc} α2new,unc = α 2 o l d =\alpha _ {2}^ {old} =α2old + y 2 ( E 1 − E 2 ) K 11 + K 22 − 2 K 12 \frac {y_ {2}(E_ {1}-E_ {2})}{K_ {11}+K_ {22}-2K_ {12}} K11+K22−2K12y2(E1−E2)
-
求出 α 2 n e w = { H α 2 n e w , u n c > H α 2 n e w , u n c L ≤ α 2 n e w , u n c ≤ H L α 2 n e w , u n c < L \alpha_2^{new}= \begin{cases} H& { \alpha_2^{new,unc} > H}\\ \alpha_2^{new,unc}& {L \leq \alpha_2^{new,unc} \leq H}\\ L& {\alpha_2^{new,unc} < L} \end{cases} α2new=⎩ ⎨ ⎧Hα2new,uncLα2new,unc>HL≤α2new,unc≤Hα2new,unc<L
-
利用 α 2 n e w \alpha _ {2}^ {new} α2new 和 α 1 n e w \alpha _ {1}^ {new} α1new 的关系求出 α 1 n e w \alpha _ {1}^ {new} α1new
-
计算 b k + 1 b^ {k+1} bk+1 和 E i E_ {i} Ei
-
检查是否满足如下的终止条件KKT:
∑ i = 1 m α i y i = 0 0 ≤ α i ≤ C , i = 1 , 2... m α i = 0 ⇒ y i g ( x i ) ≥ 1 0 < α i < C ⇒ y i g ( x i ) = 1 \sum\limits_{i=1}^{m}\alpha_iy_i = 0\\ 0 \leq \alpha_i \leq C, i =1,2...m\\ \alpha_{i} = 0 \Rightarrow y_ig(x_i) \geq 1 \\ 0 <\alpha_{i}< C \Rightarrow y_ig(x_i) = 1 i=1∑mαiyi=00≤αi≤C,i=1,2...mαi=0⇒yig(xi)≥10<αi<C⇒yig(xi)=1 -
如果满足则结束,返回 α \alpha α ,否则转到步骤2)。