smo算法伪代码详细解释

首先表明参数的解析公式:

a2=a2+y2(E1-E2)/eta

eta=x1*x1+x2*x2-2*x1*x2

伪代码:

procedure takeStep (i1,i2)//利用解析式对参数进行更新,我比较推荐按李航统计学习的内容进行更新。

target = desired output vector
point = training point matrix
procedure takeStep(i1, i2)
if (i1 == i2)//如果选取俩个一样的索引返回0表示更新失败 
return 0
alpha1 = Lagrange multiplier for i1
y1 = target[i1]
E1 = SVM output on point[i1] - y1 (check in error cache)//构建解析式所需变量
s = y1 * y2
Compute L, H via equations (*) and (**)
if (L == H)
return 0
k11 = kernel(point[i1], point[i1])//为求eta作准备
k12 = kernel(point[i1], point[i2])
k22 = kernel(point[i2], point[i2])
eta = k11 + k22 - 2 * k12
if(eta > 0)//对a2进行截断
a2 = alpha2 + y2 * (E1 - E2)/eta
if(a2 < L)
a2 = L
else if(a2 > H)
a2 = H
else
Lobj = objective function at a2=L
Hobj = objective function at a2=H
if(Lobj < Hobj - eps)
a2 = L
else if(Lobj > Hobj + eps)
a2 = H
else
a2 = alpha2
if(|a2 - alpha2| < eps * (a2 + alpha2 + eps))//如果这次变化较小返回0,表示在误差可接收范围内。
return 0
a1 = alpha1 + s * (alpha2 - a2)
Update threshold to reflect change in Lagrange multipliers
Update weight vector to reflect change in a1 & a2, if SVM is linear
Update error cache using new Lagrange multipliers
Store a1 in the alpha array
Store a2 in the alpha array
return 1
endprocedure
procedure examineExample(i2)//找到俩个参数,如果优化这俩个参数成功返回1,否则返回0。
y2 = target[i2]
alpha2 = Lagrange multiplier for i2
E2 = SVM output on point[i2] - y2 (check in error cache)
r2 = E2 * y2
if((r2 < -tol && alpha2 < C) || (r2 > tol && alpha2 > 0))
if (number of non-zero & non-C alpha > 1)
i1 = result of second choice heuristic (section 2.2)
if takeStep(i1, i2)
return 1//如果选择一个让i2变化最大的参数,更新成功返回1。
loop over all non-zero and non-C alpha, starting at a random point
i1 = identity of current alpha
if takeStep(i1, i2)
return 1//在C>参数>0的情况下循环随机读取。
loop over all possible i1, starting at a random point
i1 = loop variable
if takeStep(i1, i2)//随机选取任意参数进行更新,成功返回1,否则是0.
return 1
return 0//更新失败。
endprocedure
main routine:
numChanged = 0
examineAll = 1
while(numchanged > 0 | examineAll)//直到检验完所有例子都满足KKT条件,就退出循环
numChanged = 0;
if examineAll
loop I over all training examples
numchanged += examineExample(I)
else
loop I over examples where alpha is not 0 & not C
numchanged += examineExample(I)
if examineAll == 1
examineAll = 0
else if numchanged == 0
examineAll = 1

上一篇:数据结构--栈的应用(表达式求值 nyoj 35)


下一篇:LeetCode 1292. 元素和小于等于阈值的正方形的最大边长--前缀和