comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_train_struct.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function sTrain = som_train_struct(varargin)
2
3 %SOM_TRAIN_STRUCT Default values for SOM training parameters.
4 %
5 % sT = som_train_struct([[argID,] value, ...])
6 %
7 % sTrain = som_train_struct('train',sM,sD);
8 % sTrain = som_train_struct('finetune','data',D);
9 % sTrain = som_train_struct('previous',sT0);
10 %
11 % Input and output arguments ([]'s are optional):
12 % [argID, (string) Several default values depend on other SOM parameters
13 % value] (varies) or on the proporties of a data set. See below for a
14 % a list of required and optional arguments for
15 % different parameters, and well as the list of valid
16 % argIDs and associated values. The values which are
17 % unambiguous can be given without the preceeding argID.
18 %
19 % sT (struct) The training struct.
20 %
21 % Training struct contains values for training and initialization
22 % parameters. These parameters depend on the number of training samples,
23 % phase of training, the training algorithm.
24 %
25 % Here are the valid argument IDs and corresponding values. The values which
26 % are unambiguous (marked with '*') can be given without the preceeding rgID.
27 % 'dim' (scalar) input space dimension
28 % 'dlen' (scalar) length of the training data
29 % 'data' (matrix / struct) the training data
30 % 'munits' (scalar) number of map units
31 % 'msize' (vector) map size
32 % 'previous' (struct) previous training struct can be given in
33 % conjunction with 'finetune' phase (see below)
34 % 'phase' *(string) training phase: 'init', 'train', 'rough' or 'finetune'
35 % 'algorithm' *(string) algorithm to use: 'lininit', 'randinit', 'batch' or 'seq'
36 % 'map' *(struct) If a map struct is given, the last training struct
37 % in '.trainhist' field is used as the previous training
38 % struct. The map size and input space dimension are
39 % extracted from the map struct.
40 % 'sTrain' *(struct) a train struct, the empty fields of which are
41 % filled with sensible values
42 %
43 % For more help, try 'type som_train_struct' or check out online documentation.
44 % See also SOM_SET, SOM_TOPOL_STRUCT, SOM_MAKE.
45
46 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
47 %
48 % som_train_struct
49 %
50 % PURPOSE
51 %
52 % Default values for SOM training parameters.
53 %
54 % SYNTAX
55 %
56 % sT = som_train_struct('argID',value,...);
57 % sT = som_train_struct(value,...);
58 %
59 % DESCRIPTION
60 %
61 % This function is used to give sensible values for SOM training
62 % parameters and returns a training struct. Often, the parameters
63 % depend on the properties of the map and the training data. These are
64 % given as optional arguments to the function. If a partially filled
65 % train struct is given, its empty fields (field value is [] or '' or
66 % NaN) are supplimented with default values.
67 %
68 % The training struct has a number of fields which depend on each other
69 % and the optional arguments in complex ways. The most important argument
70 % is 'phase' which can be either 'init', 'train', 'rough' or 'finetune'.
71 %
72 % 'init' Map initialization.
73 % 'train' Map training in a onepass operation, as opposed to the
74 % rough-finetune combination.
75 % 'rough' Rough organization of the map: large neighborhood, big
76 % initial value for learning coefficient. Short training.
77 % 'finetune' Finetuning the map after rough organization phase. Small
78 % neighborhood, learning coefficient is small already at
79 % the beginning. Long training.
80 %
81 % The fields of training struct set by this function are listed below.
82 %
83 % '.mask' Basically, a column vector of ones. But if a previous
84 % train or map struct is given, it is copied from there.
85 % '.neigh' Default value is 'gaussian' but if a previous train or map
86 % struct is given, it is copied from there.
87 % '.alpha_type' Default value is 'inv' but if a previous training struct
88 % is given, it is copied from there.
89 % '.alpha_ini' For 'train' and 'rough' phases, this is 0.5, for
90 % 'finetune' it is 0.05.
91 % '.radius_ini' Depends on the previous training operation and the
92 % maximum sidelength of the map ms = max(msize).
93 % if there isn't one, or it is 'randinit', rad_ini = max(1,ms/2)
94 % if it is 'lininit', rad_ini = max(1,ms/8)
95 % otherwise, rad_ini = rad_fin of the previous training
96 % '.radius_fin' Default value is 1, but if the training phase is
97 % 'rough', rad_fin = max(1,rad_ini/4).
98 % '.trainlen' For 'train' phase this is 20 x mpd epochs, for 'rough'
99 % phase 4 x mpd epochs and for 'finetune' 16 x mpd
100 % epochs, where mpd = munits/dlen. If mpd cannot be
101 % calculated, it is set to be = 0.5. In any case,
102 % trainlen is at least one epoch.
103 % '.algorithm' Default training algorithm is 'batch' and default
104 % initialization algorithm is 'lininit'.
105 %
106 % OPTIONAL INPUT ARGUMENTS
107 %
108 % argID (string) Argument identifier string (see below).
109 % value (varies) Value for the argument (see below).
110 %
111 % The optional arguments can be given as 'argID',value -pairs. If an
112 % argument is given value multiple times, the last one is used. The
113 % valid IDs and corresponding values are listed below. The values
114 % which are unambiguous (marked with '*') can be given without the
115 % preceeding argID.
116 %
117 % 'dim' (scalar) input space dimension
118 % 'dlen' (scalar) length of the training data
119 % 'data' (matrix / struct) the training data
120 % 'munits' (scalar) number of map units
121 % 'msize' (vector) map size
122 % 'previous' (struct) previous training struct can be given in
123 % conjunction with 'finetune' phase.
124 % 'phase' *(string) training phase: 'init', 'train', 'rough' or 'finetune'
125 % 'algorithm' *(string) algorithm to use: 'lininit', 'randinit',
126 % 'batch' or 'seq'
127 % 'map' *(struct) If a map struc is given, the last training struct
128 % in '.trainhist' field is used as the previous training
129 % struct. The map size and input space dimension are
130 % extracted from the map struct.
131 % 'sTrain' *(struct) a train struct, the empty fields of which are
132 % filled with sensible values
133 %
134 % OUTPUT ARGUMENTS
135 %
136 % sT (struct) The training struct.
137 %
138 % EXAMPLES
139 %
140 % The most important optional argument for the training parameters is
141 % 'phase'. The second most important are 'previous' and/or 'map'.
142 %
143 % To get default initialization parameters, use:
144 %
145 % sTrain = som_train_struct('phase','init');
146 % or
147 % sTrain = som_train_struct('init');
148 %
149 % To get default training parameters, use:
150 %
151 % sTrain = som_train_struct('phase','train','data',D,'map',sMap);
152 % or
153 % sTrain = som_train_struct('train','data',D,sMap);
154 % or
155 % sTrain = som_train_struct('train','dlen',dlen, ...
156 % 'msize',sMap.topol.msize,'dim',dim);
157 %
158 % If you want to first rough train and then finetune, do like this:
159 %
160 % sT1 = som_train_struct('rough','dlen',length(D),sMap); % rough training
161 % sT2 = som_train_struct('finetune','previous',sT1); % finetuning
162 %
163 % SEE ALSO
164 %
165 % som_make Initialize and train a map using default parameters.
166 % som_topol_struct Default map topology.
167 % som_randinint Random initialization algorithm.
168 % som_lininit Linear initialization algorithm.
169 % som_seqtrain Sequential training algorithm.
170 % som_batchtrain Batch training algorithm.
171
172 % Copyright (c) 1999-2000 by the SOM toolbox programming team.
173 % http://www.cis.hut.fi/projects/somtoolbox/
174
175 % Version 2.0beta juuso 101199 090200 210301
176
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
178 %% check arguments
179
180 % initial default structs
181 sTrain = som_set('som_train');
182
183 % initialize optional parameters
184 dlen = NaN;
185 msize = 0;
186 munits = NaN;
187 sTprev = [];
188 dim = NaN;
189 phase = '';
190
191 % varargin
192 i=1;
193 while i<=length(varargin),
194 argok = 1;
195 if ischar(varargin{i}),
196 switch varargin{i},
197 case 'dim', i=i+1; dim = varargin{i};
198 case 'dlen', i=i+1; dlen = varargin{i};
199 case 'msize', i=i+1; msize = varargin{i};
200 case 'munits', i=i+1; munits = varargin{i}; msize = 0;
201 case 'phase', i=i+1; phase = varargin{i};
202 case 'algorithm', i=i+1; sTrain.algorithm = varargin{i};
203 case 'mask', i=i+1; sTrain.mask = varargin{i};
204 case {'previous','map'},
205 i=i+1;
206 if strcmp(varargin{i}.type,'som_map'),
207 if length(varargin{i}.trainhist),
208 sTprev = varargin{i}.trainhist(end);
209 msize = varargin{i}.topol.msize;
210 end
211 elseif strcmp(varargin{i}.type,'som_train'),
212 sTprev = varargin{i};
213 end
214 case 'data',
215 i=i+1;
216 if isstruct(varargin{i}), [dlen dim] = size(varargin{i}.data);
217 else [dlen dim] = size(varargin{i});
218 end
219 case {'init','train','rough','finetune'}, phase = varargin{i};
220 case {'lininit','randinit','seq','batch'}, sTrain.algorithm = varargin{i};
221 otherwise argok=0;
222 end
223 elseif isstruct(varargin{i}) & isfield(varargin{i},'type'),
224 switch varargin{i}.type,
225 case 'som_train',
226 sT = varargin{i};
227 if ~isempty(sT.algorithm), sTrain.algorithm = sT.algorithm; end
228 if ~isempty(sT.neigh), sTrain.neigh = sT.neigh; end
229 if ~isempty(sT.mask), sTrain.mask = sT.mask; end
230 if ~isnan(sT.radius_ini), sTrain.radius_ini = sT.radius_ini; end
231 if ~isnan(sT.radius_fin), sTrain.radius_fin = sT.radius_fin; end
232 if ~isnan(sT.alpha_ini), sTrain.alpha_ini = sT.alpha_ini; end
233 if ~isempty(sT.alpha_type), sTrain.alpha_type = sT.alpha_type; end
234 if ~isnan(sT.trainlen), sTrain.trainlen = sT.trainlen; end
235 if ~isempty(sT.data_name), sTrain.data_name = sT.data_name; end
236 if ~isempty(sT.time), sTrain.time = sT.time; end
237 case 'som_map',
238 if strcmp(varargin{i}.type,'som_map'),
239 if length(varargin{i}.trainhist),
240 sTprev = varargin{i}.trainhist(end);
241 msize = varargin{i}.topol.msize;
242 end
243 if ~isempty(varargin{i}.neigh) & isempty(sTrain.neigh),
244 sTrain.neigh = varargin{i}.neigh;
245 end
246 if ~isempty(varargin{i}.mask) & isempty(sTrain.mask),
247 sTrain.mask = varargin{i}.mask;
248 end
249 elseif strcmp(varargin{i}.type,'som_train'),
250 sTprev = varargin{i};
251 end
252 case 'som_topol', msize = varargin{i}.msize;
253 case 'som_data', [dlen dim] = size(varargin{i}.data);
254 otherwise argok=0;
255 end
256 else
257 argok = 0;
258 end
259 if ~argok,
260 disp(['(som_train_struct) Ignoring invalid argument #' num2str(i)]);
261 end
262 i = i+1;
263 end
264
265 % dim
266 if ~isempty(sTprev) & isnan(dim), dim = length(sTprev.mask); end
267
268 % mask
269 if isempty(sTrain.mask) & ~isnan(dim), sTrain.mask = ones(dim,1); end
270
271 % msize, munits
272 if ~msize | isempty(msize),
273 if isnan(munits), msize = [10 10];
274 else s = round(sqrt(munits)); msize = [s round(munits/s)];
275 end
276 end
277 munits = prod(msize);
278
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 %% action
281
282 % previous training
283 prevalg = '';
284 if ~isempty(sTprev),
285 if any(findstr(sTprev.algorithm,'init')), prevalg = 'init';
286 else prevalg = sTprev.algorithm;
287 end
288 end
289
290 % first determine phase
291 if isempty(phase),
292 switch sTrain.algorithm,
293 case {'lininit','randinit'}, phase = 'init';
294 case {'batch','seq',''},
295 if isempty(sTprev), phase = 'rough';
296 elseif strcmp(prevalg,'init'), phase = 'rough';
297 else phase = 'finetune';
298 end
299 otherwise, phase = 'train';
300 end
301 end
302
303 % then determine algorithm
304 if isempty(sTrain.algorithm),
305 if strcmp(phase,'init'), sTrain.algorithm = 'lininit';
306 elseif any(strcmp(prevalg,{'init',''})), sTrain.algorithm = 'batch';
307 else sTrain.algorithm = sTprev.algorithm;
308 end
309 end
310
311 % mask
312 if isempty(sTrain.mask),
313 if ~isempty(sTprev), sTrain.mask = sTprev.mask;
314 elseif ~isnan(dim), sTrain.mask = ones(dim,1);
315 end
316 end
317
318 % neighborhood function
319 if isempty(sTrain.neigh),
320 if ~isempty(sTprev) & ~isempty(sTprev.neigh), sTrain.neigh = sTprev.neigh;
321 else sTrain.neigh = 'gaussian';
322 end
323 end
324
325 if strcmp(phase,'init'),
326 sTrain.alpha_ini = NaN;
327 sTrain.alpha_type = '';
328 sTrain.radius_ini = NaN;
329 sTrain.radius_fin = NaN;
330 sTrain.trainlen = NaN;
331 sTrain.neigh = '';
332 else
333 mode = [phase, '-', sTrain.algorithm];
334
335 % learning rate
336 if isnan(sTrain.alpha_ini),
337 if strcmp(sTrain.algorithm,'batch'), sTrain.alpha_ini = NaN;
338 else
339 switch phase,
340 case {'train','rough'}, sTrain.alpha_ini = 0.5;
341 case 'finetune', sTrain.alpha_ini = 0.05;
342 end
343 end
344 end
345 if isempty(sTrain.alpha_type),
346 if ~isempty(sTprev) & ~isempty(sTprev.alpha_type) ...
347 & ~strcmp(sTrain.algorithm,'batch'),
348 sTrain.alpha_type = sTprev.alpha_type;
349 elseif strcmp(sTrain.algorithm,'seq'),
350 sTrain.alpha_type = 'inv';
351 end
352 end
353
354 % radius
355 ms = max(msize);
356 if isnan(sTrain.radius_ini),
357 if isempty(sTprev) | strcmp(sTprev.algorithm,'randinit'),
358 sTrain.radius_ini = max(1,ceil(ms/4));
359 elseif strcmp(sTprev.algorithm,'lininit') | isnan(sTprev.radius_fin),
360 sTrain.radius_ini = max(1,ceil(ms/8));
361 else
362 sTrain.radius_ini = sTprev.radius_fin;
363 end
364 end
365 if isnan(sTrain.radius_fin),
366 if strcmp(phase,'rough'),
367 sTrain.radius_fin = max(1,sTrain.radius_ini/4);
368 else
369 sTrain.radius_fin = 1;
370 end
371 end
372
373 % trainlen
374 if isnan(sTrain.trainlen),
375 mpd = munits/dlen;
376 if isnan(mpd), mpd = 0.5; end
377 switch phase,
378 case 'train', sTrain.trainlen = ceil(50*mpd);
379 case 'rough', sTrain.trainlen = ceil(10*mpd);
380 case 'finetune', sTrain.trainlen = ceil(40*mpd);
381 end
382 sTrain.trainlen = max(1,sTrain.trainlen);
383 end
384
385 end
386
387 return;
388
389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%