function Acc = svmCVOneVsRest( trn_samps, tst_samps, trn_zs, tst_zs,... tf, numFolds, svmflags, type, D, Dnull, gamma) %SVMCVONEVSREST Cross-validated binary "1 vs. Rest" classifcation using SVM light % % Arguments: % trn_samps randperms used to index into pool % tst_samps randperms used to index into pool % trn_zs corresponding (training) labels % tst_zs corresponding (testing) labels % tf term frequency matrix (cols are docs) % numFolds Number of cross validations % svmflags Flags passed to SVM light; if ED2 and A are specified, % "-t 4 -u /path/to/K.eg.mtx" is appended to svmflags % type either: 'stndrbf' 'stndlin' 'tranrbf' 'tranlin' % % Potentially Ignored Arguments (based on 'type'): % D either E{||a-b||^2} or E{a'*b} % Dnull vector E{||a||^2} or Ignored % gamma scalar as in exp(-gamma * D) or Ignored % % Returns: % Acc Number of correctly classified testing points at each fold % % Authors: % Joshua V. Dillon; Feb. 21, 2007; jvdillon AAT purdue DDOT edu if nargin<8 error('must supply at least eight arguments (see help)'); end % jvd: setup kernel if strcmp(type,'stndrbf') % standard radial basis function: exp(-gamma ||a-b||^2) Kcache = []; svmflags = [svmflags ' -t 2 -g ' num2str(gamma) ' ']; elseif strcmp(type,'stndlin') % standard linear: a'*b Kcache = []; svmflags = [svmflags ' -t 0 ']; elseif strcmp(type,'tranrbf') % translated radial basis function: exp(-gamma E{||a-b||^2}) if nargin<11 error('translated rbf: must supply E{||a-b||^2}, E{||a||^2}, and gamma'); end K_file = mktemp(['/noback/lowbow/translation/tmp/K.XXXXXX.mtx']); svmflags = [svmflags ' -t 4 -u "' K_file '" ']; if size(Dnull,2)==1 Kcache = exp(-gamma * [[0 Dnull'];[Dnull D]]); elseif size(Dnull,1)==1 Kcache = exp(-gamma * [[0 Dnull];[Dnull' D]]); else error('Dnull must be a vector'); end elseif strcmp(type,'tranlin') % translated linear: E{a'*b} if nargin<9 error('translated linear: must supply E{a''*b}'); end K_file = mktemp(['/noback/lowbow/translation/tmp/K.XXXXXX.mtx']); svmflags = [svmflags ' -t 4 -u "' K_file '" ']; Dnull = sparse(size(D,1),1); Kcache = [[0 Dnull'];[Dnull D]]; else error('unrecognized kernel type'); end Acc = zeros(1,numFolds); % cross validate for ii=1:numFolds % convenience variables; these will index into the normal K, hence we need % to be cautious when working with the Kcache, as it contains the % additional nulldoc row,col trn_samp = trn_samps(:,ii); tst_samp = tst_samps(:,ii); trn_z = trn_zs(:,ii); tst_z = tst_zs(:,ii); % Kcache is same kernel for training and testing (tst_samp labels are offset) if ~isempty(Kcache) % always include the nulldoc, thus we use the first row and shift % everything accordingly I = [1;trn_samp+1;tst_samp+1]; % can set precision here, default is '%.16f' jvdmatrixWrite( K_file, Kcache(I,I) ); end % labels start at 1 because 0 row and col are reserved for nulldoc Acc(ii) = svmWrapper ( ... tf(:,trn_samp), trn_z, 1:length(trn_samp),... tf(:,tst_samp), tst_z, (1:length(tst_samp))+length(trn_samp),... svmflags, 0 ); end if exist('K_file','var') & exist(K_file,'file') system(['rm ' K_file]); end