-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsvmlopt.m
More file actions
292 lines (281 loc) · 10.5 KB
/
Copy pathsvmlopt.m
File metadata and controls
292 lines (281 loc) · 10.5 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
function options = svmlopt(varargin)
% SVMLOPT - Generate/alter options structure for SVM light
%
% OPTIONS = SVMLOPT('PARAM1',VALUE1,'PARAM2',VALUE2,...) creates an
% optimization options structure OPTIONS in which the named parameters
% have the specified values. Any unspecified parameters are set to []
% (parameters with value [] indicate to use the default value for that
% parameter when OPTIONS is passed to SVM light. It is sufficient to type
% only the leading characters that uniquely identify the parameter. Case
% is ignored for parameter names.
% OPTIONS = SVMLOPT(OLDOPTS,'PARAM1',VALUE1,...) creates a copy of OLDOPTS
% with the named parameters altered with the specified values.
% OPTIONS = SVMLOPT(OLDOPTS,NEWOPTS) combines an existing options structure
% OLDOPTS with a new options structure NEWOPTS. Any parameters in NEWOPTS
% with non-empty values overwrite the corresponding old parameters in
% OLDOPTS.
% OPTIONS = SVMLOPT (with no input arguments) creates an options structure
% OPTIONS where all the fields are set to [].
%
% The correspondence of the SVM light options to the field in the
% OPTIONS structure is as follows:
% Field SVM light Range, description
% 'Verbosity' -v {0 .. 3}, default value 1
% Verbosity level
% 'Regression' -z {0, 1}, default value 0
% Switch between regression [1] and
% classification [0]
% 'C' -c (0, Inf), default value (avg. x*x)^-1
% Trade-off between error and margin
% 'TubeWidth' -w (0, Inf), default value 0.1
% Epsilon width of tube for regression
% 'CostFactor' -j (0, Inf), default value 1
% Cost-Factor by which training errors on
% positive examples outweight errors on
% negative examples
% 'Biased' -b {0, 1}, default value 1
% Use biased hyperplane x*w+b0 [1] instead of
% unbiased x*w0 [0]
% 'RemoveIncons' -i {0, 1}, default value 0
% Remove inconsistent training examples and
% retrain
% 'ComputeLOO' -x {0, 1}, default value 0
% Compute leave-one-out estimates [1]
% 'XialphaRho' -o )0, 2), default value 1.0
% Value of rho for XiAlpha-estimator and for
% pruning leave-one-out computation
% 'XialphaDepth' -k {0..100}, default value 0
% Search depth for extended XiAlpha-estimator
% 'TransPosFrac' -p (0..1), default value ratio of
% positive and negative examples in the
% training data. Fraction of unlabeled
% examples to be classified into the positive
% class
% 'Kernel' -t {0..4}, default value 1
% Type of kernel function:
% 0: linear
% 1: polynomial (s a*b+c)^d
% 2: radial basis function exp(-gamma ||a-b||^2)
% 3: sigmoid tanh(s a*b + c)
% 4: user defined kernel from kernel.h
% 'KernelParam' -d, -g, -s, -r, -u
% Depending on the kernel, this vector
% contains [d] for polynomial kernel, [gamma]
% for RBF, [s, c] for tanh kernel, string for
% user-defined kernel
% 'MaximumQP' -q {2..}, default value 10
% Maximum size of QP-subproblems
% 'NewVariables' -n {2..}, default value is the value chosen
% for 'MaximumQP'. Number of new variables
% entering the working set in each
% iteration. Use smaller values to prevent
% zig-zagging
% 'CacheSize' -m (5..Inf), default value 40.
% Size of cache for kernel evaluations in MB
% 'EpsTermin' -e (0..Inf), default value 0.001
% Allow that error for termination criterion
% [y [w*x+b] - 1] < eps
% 'ShrinkIter' -h {5..Inf}, default value 100.
% Number of iterations a variable needs to be
% optimal before considered for shrinking
% 'ShrinkCheck' -f {0, 1}, default value 1
% Do final optimality check for variables
% removed by shrinking. Although this test is
% usually positive, there is no guarantee
% that the optimum was found if the test is
% omitted.
% 'TransLabelFile' -l String. File to write predicted labels of
% unlabeled examples into after transductive
% learning.
% 'AlphaFile' -a String. Write all alphas to this file after
% learning (in the same order as in the
% training set).
%
% See also SVML,SVM_LEARN,SVM_CLASSIFY
%
%
% Copyright (c) by Anton Schwaighofer (2001)
% $Revision: 1.7 $ $Date: 2002/05/30 10:33:28 $
% mailto:anton.schwaighofer@gmx.net
%
% This program is released unter the GNU General Public License.
%
options = struct('ExecPath', [], ...
'Verbosity', [], ...
'Regression', [], ...
'C', [], ...
'TubeWidth', [], ...
'CostFactor', [], ...
'Biased', [], ...
'RemoveIncons', [], ...
'ComputeLOO', [], ...
'XialphaRho', [], ...
'XialphaDepth', [], ...
'TransPosFrac', [], ...
'Kernel', [], ...
'KernelParam', [], ...
'MaximumQP', [], ...
'NewVariables', [], ...
'CacheSize', [], ...
'EpsTermin', [], ...
'ShrinkIter', [], ...
'ShrinkCheck', [], ...
'TransLabelFile', [], ...
'AlphaFile', []);
numberargs = nargin;
Names = fieldnames(options);
[m,n] = size(Names);
names = lower(Names);
i = 1;
while i <= numberargs
arg = varargin{i};
if isstr(arg)
break;
end
if ~isempty(arg)
if ~isa(arg,'struct')
error(sprintf('Expected argument %d to be a string parameter name or an options structure.', i));
end
for j = 1:m
if any(strcmp(fieldnames(arg),Names{j,:}))
val = getfield(arg, Names{j,:});
else
val = [];
end
if ~isempty(val)
[valid, errmsg] = checkfield(Names{j,:},val);
if valid
options = setfield(options, Names{j,:},val);
else
error(errmsg);
end
end
end
end
i = i + 1;
end
% A finite state machine to parse name-value pairs.
if rem(numberargs-i+1,2) ~= 0
error('Arguments must occur in name-value pairs.');
end
expectval = 0;
while i <= numberargs
arg = varargin{i};
if ~expectval
if ~isstr(arg)
error(sprintf('Expected argument %d to be a string parameter name.', i));
end
lowArg = lower(arg);
j = strmatch(lowArg,names);
if isempty(j)
error(sprintf('Unrecognized parameter name ''%s''.', arg));
elseif length(j) > 1
% Check for any exact matches (in case any names are subsets of others)
k = strmatch(lowArg,names,'exact');
if length(k) == 1
j = k;
else
msg = sprintf('Ambiguous parameter name ''%s'' ', arg);
msg = [msg '(' Names{j(1),:}];
for k = j(2:length(j))'
msg = [msg ', ' Names{k,:}];
end
msg = sprintf('%s).', msg);
error(msg);
end
end
expectval = 1;
else
[valid, errmsg] = checkfield(Names{j,:}, arg);
if valid
options = setfield(options, Names{j,:}, arg);
else
error(errmsg);
end
expectval = 0;
end
i = i + 1;
end
function [valid, errmsg] = checkfield(field,value)
% CHECKFIELD Check validity of structure field contents.
% [VALID, MSG] = CHECKFIELD('field',V) checks the contents of the specified
% value V to be valid for the field 'field'.
%
valid = 1;
errmsg = '';
if isempty(value)
return
end
isFloat = length(value==1) & isa(value, 'double');
isPositive = isFloat & (value>=0);
isString = isa(value, 'char');
range = [];
requireInt = 0;
switch field
case {'Biased', 'ComputeLOO', 'ShrinkCheck'}
if isFloat,
if all(value~=[0 1]);
valid = 0;
errmsg = sprintf('Invalid value for %s parameter: Must be either 0 or 1', field);
end
end
case 'XialphaRho'
if ~isFloat | (value<=0) | (value>2),
valid = 0;
errmsg = sprintf('Invalid value for %s parameter: Must be scalar in the range (0, 2]', field);
end
case {'ExecPath', 'TransLabelFile', 'AlphaFile'}
if ~isString,
valid = 0;
errmsg = sprintf('Invalid value for %s parameter: Must be a string', field);
end
case 'RemoveIncons'
requireInt = 1;
range = [0 3];
% does not comply to the SVMlight help text, but is mentioned in the code
case 'Verbosity'
requireInt = 1;
range = [0 3];
case 'Regression'
requireInt = 1;
range = [0 1];
case {'C', 'CostFactor', 'EpsTermin', 'TubeWidth'}
range = [0 Inf];
case 'Kernel'
requireInt = 1;
range = [0 4];
case 'KernelParam'
valid = 1;
case 'XialphaDepth'
requireInt = 1;
range = [0 100];
case 'TransPosFrac'
range = [0 1];
case 'MaximumQP'
requireInt = 1;
range = [2, Inf];
case 'NewVariables'
requireInt = 1;
range = [2, Inf];
case 'CacheSize'
requireInt = 1;
range = [5, Inf];
case 'ShrinkIter'
requireInt = 1;
range = [5, Inf];
otherwise
valid = 0;
error('Unknown field name for Options structure.')
end
if ~isempty(range),
if (value<range(1)) | (value>range(2)),
valid = 0;
errmsg = sprintf('Invalid value for %s parameter: Must be scalar in the range [%g..%g]', ...
field, range(1), range(2));
end
end
if requireInt & ((value-round(value))~=0),
valid = 0;
errmsg = sprintf('Invalid value for %s parameter: Must be integer', ...
field);
end