function Y = svmWrapper( trn_vects, trn_labels, trn_ids, ... tst_vects, tst_labels, tst_ids, ... flags, debugsvm ) %SVMWRAPPER SVM light wrapper % % Arguments: % trn_vects x % trn_labels x % trn_ids x % tst_vects x % tst_labels x % K_file x % flags x % debugsvm x % % Returns: % Y x % % Authors: % Joshua V. Dillon; Feb. 21, 2007; jvdillon AAT purdue DDOT edu % vectors never modified should automatically be passed by reference. % cf.: http://www.mathworks.com/support/solutions/data/1-15SO4.html?1-15SO4 % TODO: dimension checking here. Ensure K has a compare-to-self column and % row! % TODO: if we are doing a lot of CV, could just write the WHOLE kernel once, % and let SVM read the whole thing each time? if nargin==6 flags = ''; debugsvm = 0; elseif nargin==7 debugsvm = 0; elseif nargin==8 % do nothing else error('invalid args'); end svmdir = '/noback/lowbow/translation/svm/helper/'; % --- Phase 0: Setup ---------------------------------------------------------- % create new temporary files and set some variables tmpdir = '/noback/lowbow/translation/tmp/'; test_file = mktemp([tmpdir 'testXXXXXX.dat']); train_file = mktemp([tmpdir 'trainXXXXXX.dat']); model_file = mktemp([tmpdir 'modelXXXXXX.dat']); % designate a model file output_file = mktemp([tmpdir 'outputXXXXXX.dat']); % designate an output file floatfmt = '%.16g'; % --- Phase 1: Training ------------------------------------------------------- % write training file svmWriteDat(train_file, trn_vects, trn_labels, trn_ids, floatfmt); %cmdlrn = [' ./svm_learn -c 1.0 -z c -t 4 -u "' K_file '" ' train_file ' ' model_file ]; cmdlrn = [svmdir 'svm_learn -z c ' flags ' ' train_file ' ' model_file ]; [status result] = system(cmdlrn); if debugsvm, fprintf('%s\n%s\n',cmdlrn,result); end system(['rm ' train_file]); % --- Phase 2: Testing -------------------------------------------------------- % write testing file svmWriteDat(test_file, tst_vects, tst_labels, tst_ids, floatfmt); %cmdcls = ['./svm_classify -v 1 ' test_file ' ' model_file ' ' output_file]; cmdcls = [svmdir 'svm_classify -v 1 ' test_file ' ' model_file ' ' '/dev/null']; %disp(cmdcls); [status result] = system(cmdcls); if debugsvm, fprintf('%s\n%s\n',cmdcls,result); end % jvd: we could read the whole output file... %system(['rm ' model_file ' ' test_file]); %fd = fopen(output_file,'r'); %Y = textscan(fd,'%n'); %fclose(fd); %Y = [Y{:}]; %system(['rm ' output_file]); % jvd: ...or simply regex the svm output ptrn = 'Accuracy .+? \((\d+) correct, \d+ incorrect, \d+ total)'; match = regexp(result,ptrn,'tokens'); Y = cellfun(@str2num,match{1}); system(['rm ' model_file ' ' test_file ' ' output_file]); % --- Helper Functions -------------------------------------------------------- function svmWriteDat(filename,X,labels,ids,floatfmt) if nargin==4, floatfmt = '%.16g'; elseif nargin~=5, error('invalid arguments'); else, end fd = fopen(filename,'w'); for ii=1:size(X,2) [I J S] = find(X(:,ii)); fprintf(fd,'%d',labels(ii)); fprintf(fd,[' %d:' floatfmt],[I S]'); fprintf(fd,' #%d\n',ids(ii)); end fclose(fd);