最近需要这个功能,google出来了,网址
https://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#f506
网页中搜索L2就可以找到,需要改动三处。
还有最新版本的
LIBLINEAR-大型线性分类的库https://www.csie.ntu.edu.tw/~cjlin/liblinear/、
建议是使用libsvm,对于某些大型问题使用LIBLINEAR
Q: I would like to solve L2-loss SVM (i.e., error term is quadratic). How should I modify the code ?
It is extremely easy. Taking c-svc for example, to solve
only two places of svm.cpp have to be changed.
我的程序做过改动,所以行数略有不同可能,就在附近
First(1466行), modify the following line of solve_c_svc from
s.Solve(l, SVC_Q(*prob,*param,y), minus_ones, y,
alpha, Cp, Cn, param->eps, si, param->shrinking);
to
s.Solve(l, SVC_Q(*prob,*param,y), minus_ones, y,
alpha, INF, INF, param->eps, si, param->shrinking);
Second(1322行), in the class of SVC_Q, declare C as a private variable:
double C;
In the constructor replace(1283行)
for(int i=0;i<prob.l;i++)
QD[i]= (Qfloat)(this->*kernel_function)(i,i);
with
this->C = param.C;
for(int i=0;i<prob.l;i++)
QD[i]= (Qfloat)(this->*kernel_function)(i,i)+0.5/C;
Then in the subroutine get_Q, after the for loop, add(1295行)
if(i >= start && i < len)
data[i] += 0.5/C;
For one-class svm, the modification is exactly the same. For SVR, you don't need an if statement like the above. Instead, you only need a simple assignment:
data[real_i] += 0.5/C;
For large linear L2-loss SVM, please use LIBLINEAR.