?? bsvm2.m~
字號:
function [model]=msvm2( data, options )% MSVM2 Multiclass SVM with 2-soft margin trained by single-class optimizer.%% Synopsis:% model = mssvm( data, options) %% Description:% This function implements transformation of multi-class SVM problem% to the single-class SVM which is consequently solved by a seleceted% single-class SVM optimizers (e.g., Kozinec, SMO, KOZSMO).% % The transformation is based on the Kersler's construction [DHS01] which % can be simply expresed in terms of kernel functions [Franc02a]. % The resulting multi-class SVM has the same architecture as the one % yieled by one-against-all decomposition. In constrast to the one-against-all % decomposition all rules (discriminant functions) are trained simultanously.%% See 'help oaasvmclass' for info about classification.%% Inputs:% data.X [dim x num_data] Training patterns.% data.y [1 x num_data] Labels of training patterns. The integers from% 1 to max_class are assumed as the labels.% options.ker [string] Kernel identifier. See 'help kernel'.% options.arg [...] Arguments of selected kernel.% options.C [positiv real] Regularization constant.% options.solver [string] KOZINEC, SMO, KOZSMO% options.tmax [int]% options.tolabs [real] % options.tolrel [real]%% Output:% model.rule [cell num_class x 1] Description of SVM discriminat functions.% model.num_classes [int] Number of classes.% model.num_rules [int] Number of classification rules == num_classes.% model.kercnt [int] Total number of used kernel evaluations.% model.cputime [int] Used CPU time.% model.options [struct] Copy of used options.% model.nsv [int] Number of unique support vectors.% model.sv.X [dim x nsv] Support vectors stored as columns.% model.sv.y [1 x nsv] Labels of Support vectors (not used for % classification).% model.classifier [string] Name of classifier.%% Example:% data=load('threeclusters1');% model=msvm2(data,{'ker','linear','C',10})% ytrn = oaasvmclass( data.X, model);% trnerr=cerror(data.y,ytrn)% figure; ppatterns(data); pmsvm(model);%% See also % OAASVMCLASS, OAOSVM, OAOSVMCLASS, PMSVM.%% About: Statistical Pattern Recognition Toolbox% (C) 1999-2003, Written by Vojtech Franc and Vaclav Hlavac% <a href="http://www.cvut.cz">Czech Technical University Prague</a>% <a href="http://www.feld.cvut.cz">Faculty of Electrical Engineering</a>% <a href="http://cmp.felk.cvut.cz">Center for Machine Perception</a>% Modifications:% 23-jan-2003, VFtic;model.classifier = 'oaasvmclass';% process input argumensdata=c2s(data);if nargin < 2, options=[]; else options=c2s(options); endif ~isfield(options,'ker'), options.ker='linear'; endif ~isfield(options,'arg'), options.arg=1; endif ~isfield(options,'C'), options.C=inf; endif ~isfield(options,'tmax'), options.tmax=inf; endif ~isfield(options,'tolabs'), options.tolabs=0; endif ~isfield(options,'tolrel'), options.tolrel=0.001; endif ~isfield(options,'solver'), options.solver='kozinec'; end%---------------------------------[dim,num_data]=size(data.X);num_classes=max(data.y);%---------------------------------model.name = 'One-Against-All SVM classifier';[ker,arg]=kerarg(options.ker,options.arg);switch upper(options.solver) case 'KOZINEC', solver = 0; case 'SMO', solver = 1; case 'KOZSMO', solver = 2; case 'NPA', solver = 3; otherwise error('Unknown solver.');end[Alpha,b,exitflag,kercnt,trnerr,t,UB,LB,History] = msvm2ccode(data.X,data.y,... ker,arg,options.C,solver,options.tmax,options.tolabs, options.tolrel);
model.num_classes = num_classes;model.num_rules = num_classes;model.rule = cell(model.num_rules,1);
model.exitflag = exitflag;
model.trnerr = trnerr;model.kercnt = kercnt;
model.t = t;
model.UB = UB;
model.LB = LB;
model.LB_History = History(1,:);model.UB_History = History(2,:);for i=1:num_classes, model.rule{i}.b = b(i); inx=find(Alpha(i,:)~=0); model.rule{i}.Alpha = Alpha(i,inx); model.rule{i}.sv.inx = inx; lab=data.y(inx); lab2=lab; lab2(find(lab==i))=1;lab2(find(lab~=i))=2; model.rule{i}.Alpha = model.rule{i}.Alpha(:).*label12topm(lab2(:)); model.rule{i}.options = options;endmodel.options = options;model.sv = data;model.nsv = length(find(sum(Alpha))); inx_sv = find(sum(abs(Alpha))); % takes only support vectors (maintain only one copy od support % vectors) and change indicesmodel.nsv = length(inx_sv);inx_tmp = zeros(1,num_data);inx_tmp(inx_sv) = [1:model.nsv];model.sv.X = data.X(:,inx_sv);model.sv.y = data.y(:,inx_sv);for i=1:model.num_rules, model.rule{i}.sv.inx = inx_tmp(model.rule{i}.sv.inx);endmodel.cputime = toc;return;% EOF
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -