📄 kernelsk.m
字号:
function [Alpha,bias,sol,t,flps,margin,up,lo,trn_err]=... kernelsk(X,I,epsilon,ker,arg,tmax,C,up,lo)% KERNELSK kernel Schlesinger-Kozinec's algorithm, SVM (L2). % [Alpha,bias]=kernelsk(X,I,epsilon,ker,arg,tmax,C)%% [Alpha,bias,sol,t,flps,margin,up,lo,trm_err]=...% kernelsk(X,I,epsilon,ker,arg,tmax,C)%% This algorithm solves the SVM problem with the quadratic % penalizition of classification violations using the % kernel Schlesinger-Kozinec's algorithm.% % Inputs:% X [NxL] training patterns, N is dimension and L number of patterns.% I [1xL] labels, 1 for 1st class and 2 for 2nd class.% epsilon [1x1] precision of found solution. The margin of found % hyperplane is less than the optimal margin at most by epsilon. % ker [string] kernel, see 'help kernel'.% arg [...] argument of given kernel, see 'help kernel'.% tmax [int] maximal number of iterations.% C [real] trade-off between margin and training error.% % Outputs:% Alpha [1xL] Weights (Lagrangians) of patterns.% bias [real] bias (threshold) of found decision rule.% sol [int] 1 solution is found% 0 algorithm stoped (t == tmax) before converged.% -1 hyperplane with margin greater then epsilon % does not exist.% t [int] number of iterations.% margin [real] margin between classes.% flps [int] number of used floating point operations.
% up [1,t] evolution of the upper bound on the optimal margin.% lo [1,t] evolution of the lower bound on the optimal margin.% trn_err [real] training error (empirical risk).%% See also KERNELSKF, SVM.%% Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac% (c) Czech Technical University Prague, http://cmp.felk.cvut.cz% Written Vojtech Franc (diploma thesis) 02.11.1999, 13.4.2000% Modifications% 23-October-2001, V.Franc% 19-Septemberr-2001, V.Franc, comments changed.% 18-August-2001, V.Franc, m^* - m <= epsilon instead of <= epsilon/2
% added uppper and lower bound on % 17-August-2001, V.Franc, renamed to KERNELSK.% 13-July-2001, V.Franc, comments% 12-July-2001, V.Franc, C, bias and normal vect. normalized.% 11-July-2001, V.Franc, Rosta Horcik proved that the computation % of threshold is OK.% 10-July-2001, V.Franc, derived from kekozinec2flops(0);% set default values of the input argimentsif nargin < 7, C = inf;end% maximal number of iteraionsif nargin < 6, tmax = inf;end% indexes of pattens in the 1st and 2nd classxinx1 = find(I == 1);xinx2 = find(I == 2);X1=X(:,xinx1); % patters from 1st classX2=X(:,xinx2); % patters from 1st classl1 = size(X1,2); % number os patternsl2 = size(X2,2);% compute kernel matricesK1 = kernel( X1, X1, ker, arg ); % [l1 x l1]K2 = kernel( X2, X2, ker, arg ); % [l2 x l2]K12 = kernel( X1, X2, ker, arg ); % [l1 x l2]% make problem lin-separable in high dimensional spaceif C ~= 0, CD1=eye(l1,l1)/(2*C); % additional diagonal matrix CD2=eye(l2,l2)/(2*C); K1=K1+CD1; K2=K2+CD2;end% convex coeficients defining normal of the decision hyperplane% (they correspond to the Lagrangian multiplyers).s1 = zeros(l1, 1);s2 = zeros(l2, 1);% initial solutions1(1)=1; % take the 1st pattern from the 1st classs2(1)=1; % take the 2nd pattern from the 2st classsol=0;t = 0;
up=[];
lo=[];
% main cyclewhile sol == 0 & tmax > t, t = t + 1; sol = 1; % -- compute auxciliary variables -- a = s1'*K1*s1; b = s2'*K2*s2; c = s1'*K12*s2; f = K2*s2; e = K1*s1;
d1 = e - K12*s2; d2 = f - (s1'*K12)';
[d1min,inx1] = min(d1); [d2min,inx2] = min(d2);
% projection x \in X_1 on (w_1 - w_2)
proj1 = (d1min + b -c )/sqrt(a-2*c+b);
% projection x \in X_2 on (w_2 - w_1)
proj2 = (d2min + a - c)/sqrt(a-2*c+b);
if sqrt( a -2*c +b) <= 0, % algorithm has converged to the zero vector --> classes overlap sol = -1; break; end % --- compute stop condition for the alpha1 (1st class) -----
% (proj1 < proj2) ~ the worst point will be used for update if (proj1 < proj2) & (proj1 <= (sqrt(a-2*c+b) - epsilon)), % -- Adaptation phase of vector alpha1 ---------------------------- k = (a - d1min - c)/(a + K1(inx1,inx1) - 2*e(inx1) ); k = min( 1, k ); s1 = s1 * (1-k); s1(inx1) = s1(inx1) + k; sol = 0;
else % --- compute stop condition for the alpha2 (2st class) ------
if proj2 <= (sqrt(a-2*c+b) - epsilon ), % -- Adaptation phase ---------------------------------- k = (b - d2min -c)/(b + K2(inx2,inx2) - 2*f(inx2) ); k = min( 1, k ); s2 = s2 * (1-k); s2(inx2) = s2(inx2) + k; sol = 0;
end
end
% store up=||w||/2 and current margin m(w1-w2,theta) = min( m1, m2)
m = min([proj1,proj2]) - 0.5*sqrt(a-2*c+b);
up = [up,sqrt(a-2*c+b)/2];
lo = [lo,m ];
end% does the found hyperplane separate the classes or not ?if sol == 1 & ((d2min + a - c) < 0 | (d1min + b -c ) < 0), sol = -2;end% --- determine threshold --------%theta = 0.5*( s1'*K1*s1 - s2'*K2*s2 );% sqared margin in transfromed spacemargin2 = s1'*K1*s1 - 2*s1'*K12*s2 + s2'*K2*s2;% threshold after normalizationtheta = ( s1'*K1*s1 - s2'*K2*s2 )/margin2;% solution (normal vect. in the transformed space) after normalizations1=2*s1/margin2;s2=2*s2/margin2;% --- compute margin and classify training patternsif C~=0, K1=K1-CD1; K2=K2-CD2;endmargin = 1/sqrt( s1'*K1*s1 - 2*s1'*K12*s2 + s2'*K2*s2 );dpred1 = (K1*s1 - K12*s2 )' - theta; dpred2 = (K12'*s1 -K2*s2 )' - theta; % classification error on the traning settrn_err = length( find([dpred1,-dpred2] < 0) )/(l1+l2);%----------------------% make SVM-like outputAlpha=zeros(1,l1+l2);Alpha(xinx1)=s1;Alpha(xinx2)=s2;bias = -theta;% overall number of used float point operationsflps=flops;return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -