Libsvm 添加L2损失函数

最近需要这个功能,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

min_{w,b} w^Tw/2 + C \sum max(0, 1- (y_i w^Tx_i+b))^2,

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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值