使用Matlab實現了二分類的SVM,優化技術使用的是Matlab自帶優化函數quadprog。
只為檢查所學,更為熟悉;不為炫耀。也沒有太多時間去使用更多的優化方法。
function model = svm0311(data,options)
%SVM0311 解決2分類的SVM方法,優化使用matlab優化工具箱quadprog函數實現
%by LiFeiteng email:[email protected]
%Reference: stptool
% Pattern Recognition and Machine Learning P333 7.32-7.37
% input aruments
%-------------------------------------------
tic
data=c2s(data);
[dim,num_data]=size(data.X);
if nargin < 2, options=[]; else options=c2s(options); end
if ~isfield(options,'ker'), options.ker = 'linear'; end
if ~isfield(options,'arg'), options.arg = 1; end
if ~isfield(options,'C'), options.C = inf; end
if ~isfield(options,'norm'), options.norm = 1; end
if ~isfield(options,'mu'), options.mu = 1e-12; end
if ~isfield(options,'eps'), options.eps = 1e-12; end
X = data.X;
t = data.y;
t(t==2) = -1;
% Set up QP task
%----------------------------
K = X'*X;
T = t'*t;% 注意t是橫向量
H = K.*T;
save('H0311.mat','H')
H = H + options.mu*eye(size(H));
f = -ones(num_data,1);
Aeq = t;
beq = 0;
lb = zeros(num_data,1);
ub = options.C*ones(num_data,1);
x0 = zeros(num_data,1);
qp_options = optimset('Display','off');
[Alpha,fval,exitflag] = quadprog(H, f,[],[], Aeq, beq, lb, ub, x0, qp_options);
inx_sv = find(Alpha>options.eps);
% compute bias
%--------------------------
% take boundary (f(x)=+/-1) support vectors 0 < Alpha < C
b = 0;
inx_bound = find( Alpha > options.eps & Alpha < (options.C - options.eps));
Nm = length(inx_bound);
for n = 1:Nm
tmp = 0;
for m = 1:length(inx_sv) %PRML7.37
tmp = tmp+Alpha(inx_sv(m))*t(inx_sv(m))*K(inx_bound(n),inx_sv(m));
end
b = b + t(inx_bound(n))-tmp;
end
b = b/Nm;
model.b = b;
%-----------------------------------------
w = zeros(dim,1);
for i = 1:num_data
w = w+ Alpha(i)*t(i)*X(:,i);%PRML 7.29
end
margin = 1/norm(w);
%-------------------------------------------
%此處與stprtool保持接口一致 用於畫圖展示等
model.Alpha = Alpha( inx_sv );
model.sv.X = data.X(:,inx_sv );
model.sv.y = data.y(inx_sv );
model.sv.inx = inx_sv;
model.nsv = length( inx_sv );
model.margin = margin;
model.exitflag = exitflag;
model.options = options;
model.kercnt = num_data*(num_data+1)/2;
model.trnerr = cerror(data.y,svmclass(data.X, model));
model.fun = 'svmclass';
model.W = model.sv.X*model.Alpha;
% used CPU time
model.cputime=toc;
return;