-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsvm_learn.m
155 lines (145 loc) · 4.38 KB
/
svm_learn.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
function status = svm_learn(options, examples, model)
% SVM_LEARN - Interface to SVM light, learning module
%
% STATUS = SVM_LEARN(OPTIONS, EXAMPLES, MODEL)
% Call the training program 'svm_learn' of the SVM light
% package.
% OPTIONS must be a structure generated by SVMLOPT.
% EXAMPLES is the name of the file containing the training examples
% (use SVMLWRITE to convert a Matlab matrix to the appropriate format).
% MODEL is the name of the file holding the trained Support Vector
% Machine.
% If 'svm_learn' is not on the path, OPTIONS must contain a field
% 'ExecPath' with the path of the executable.
% STATUS is the error code returned by SVM light (0 if everything went
% fine)
%
% See also SVMLOPT, SVMLWRITE, SVM_CLASSIFY, SVMLREAD
%
%
% Copyright (c) by Anton Schwaighofer (2001)
% $Revision: 1.6 $ $Date: 2002/08/09 20:24:03 $
% mailto:anton.schwaighofer@gmx.net
%
% This program is released unter the GNU General Public License.
%
error(nargchk(3, 3, nargin));
% check parameter consistency for kernels
if ~isempty(options.Kernel),
if (options.Kernel~=0) & isempty(options.KernelParam),
error(sprintf('The chosen Kernel = %i requires parameters, but none are given in KernelParam', ...
options.Kernel));
end
parlen = length(options.KernelParam);
isString = isa(options.KernelParam, 'char');
switch options.Kernel
case {1, 2}
if isString | (parlen~=1),
error(sprintf('The chosen Kernel = %i requires a scalar parameter', ...
options.Kernel));
end
case 3
if isString | (parlen~=2),
error(sprintf('The chosen Kernel = %i requires 2 scalar parameters', ...
options.Kernel));
end
case 4,
if ~isString,
error(sprintf('The chosen Kernel = %i requires a string parameter', ...
options.Kernel));
end
end
end
if ~isempty(options.NewVariables),
if isempty(options.MaximumQP),
maxval = 10;
else
maxval = options.MaximumQP;
end
if options.NewVariables>maxval,
error('Option ''NewVariables'' must be smaller than 10 resp. value of MaximumQP');
end
end
Names = fieldnames(options);
[m,n] = size(Names);
s = '';
for i = 1:m,
field = Names{i,:};
value = getfield(options, field);
switch field,
case 'Verbosity'
s = stroption(s, '-v %i', value);
case 'Regression'
if ~isempty(value),
if value==0,
s = [s ' -z c'];
else
s = [s ' -z r'];
end
end
case 'C'
s = stroption(s, '-c %.10g', value);
case 'TubeWidth'
s = stroption(s, '-w %.10g', value);
case 'CostFactor'
s = stroption(s, '-j %.10g', value);
case 'Biased'
s = stroption(s, '-b %i', value);
case 'RemoveIncons'
s = stroption(s, '-i %i', value);
case 'ComputeLOO'
s = stroption(s, '-x %i', value);
case 'XialphaRho'
s = stroption(s, '-o %.10g', value);
case 'XialphaDepth'
s = stroption(s, '-k %.10g', value);
case 'TransPosFrac'
s = stroption(s, '-p %.10g', value);
case 'Kernel'
s = stroption(s, '-t %i', value);
case 'KernelParam'
if ~isempty(value),
switch options.Kernel
case 0
case 1
s = stroption(s, '-d %.10g', value(1));
case 2
s = stroption(s, '-g %.10g', value(1));
case 3
s = stroption(s, '-s %.10g -r %.10g', value(1), value(2));
case 4
s = stroption(s, '-u "%s"', value);
end
end
case 'MaximumQP'
s = stroption(s, '-q %i', value);
case 'NewVariables'
s = stroption(s, '-n %i', value);
case 'CacheSize'
s = stroption(s, '-m %i', value);
case 'EpsTermin'
s = stroption(s, '-e %.10g', value);
case 'ShrinkIter'
s = stroption(s, '-h %i', value);
case 'ShrinkCheck'
s = stroption(s, '-f %i', value);
case 'TransLabelFile'
s = stroption(s, '-l %s', value);
case 'AlphaFile'
s = stroption(s, '-a %s', value);
end
end
evalstr = [fullfile(options.ExecPath, 'svm_learn') s ' ' ...
examples ' ' model];
fprintf('\nCalling SVMlight:\n%s\n\n', evalstr);
if isunix,
status = unix(evalstr);
else
status = dos(evalstr);
end
function s = stroption(s, formatstr, value, varargin)
% STROPTION - Add a new option to string
%
if ~isempty(value),
s = [s ' ' sprintf(formatstr, value, varargin{:})];
end