comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_randinit.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 sMap = som_randinit(D, varargin)
2
3 %SOM_RANDINIT Initialize a Self-Organizing Map with random values.
4 %
5 % sMap = som_randinit(D, [[argID,] value, ...])
6 %
7 % sMap = som_randinit(D);
8 % sMap = som_randinit(D,sMap);
9 % sMap = som_randinit(D,'munits',100,'hexa');
10 %
11 % Input and output arguments ([]'s are optional):
12 % D The training data.
13 % (struct) data struct
14 % (matrix) data matrix, size dlen x dim
15 % [argID, (string) Parameters affecting the map topology are given
16 % value] (varies) as argument ID - argument value pairs, listed below.
17 %
18 % sMap (struct) map struct
19 %
20 % Here are the valid argument IDs and corresponding values. The values
21 % which are unambiguous (marked with '*') can be given without the
22 % preceeding argID.
23 % 'munits' (scalar) number of map units
24 % 'msize' (vector) map size
25 % 'lattice' *(string) map lattice: 'hexa' or 'rect'
26 % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid'
27 % 'topol' *(struct) topology struct
28 % 'som_topol','sTopol' = 'topol'
29 % 'map' *(struct) map struct
30 % 'som_map','sMap' = 'map'
31 %
32 % For more help, try 'type som_randinit' or check out online documentation.
33 % See also SOM_MAP_STRUCT, SOM_LININIT, SOM_MAKE.
34
35 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %
37 % som_randinit
38 %
39 % PURPOSE
40 %
41 % Initializes a SOM with random values.
42 %
43 % SYNTAX
44 %
45 % sMap = som_randinit(D)
46 % sMap = som_randinit(D,sMap);
47 % sMap = som_randinit(D,'munits',100,'hexa');
48 %
49 % DESCRIPTION
50 %
51 % Initializes a SOM with random values. If necessary, a map struct
52 % is created first. For each component (xi), the values are uniformly
53 % distributed in the range of [min(xi) max(xi)].
54 %
55 % REQUIRED INPUT ARGUMENTS
56 %
57 % D The training data.
58 % (struct) Data struct. If this is given, its '.comp_names' and
59 % '.comp_norm' fields are copied to the map struct.
60 % (matrix) data matrix, size dlen x dim
61 %
62 % OPTIONAL INPUT ARGUMENTS
63 %
64 % argID (string) Argument identifier string (see below).
65 % value (varies) Value for the argument (see below).
66 %
67 % The optional arguments can be given as 'argID',value -pairs. If an
68 % argument is given value multiple times, the last one is used.
69 %
70 % Here are the valid argument IDs and corresponding values. The values
71 % which are unambiguous (marked with '*') can be given without the
72 % preceeding argID.
73 % 'dlen' (scalar) length of the training data
74 % 'data' (matrix) the training data
75 % *(struct) the training data
76 % 'munits' (scalar) number of map units
77 % 'msize' (vector) map size
78 % 'lattice' *(string) map lattice: 'hexa' or 'rect'
79 % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid'
80 % 'topol' *(struct) topology struct
81 % 'som_topol','sTopol' = 'topol'
82 % 'map' *(struct) map struct
83 % 'som_map','sMap' = 'map'
84 %
85 % OUTPUT ARGUMENTS
86 %
87 % sMap (struct) The initialized map struct.
88 %
89 % EXAMPLES
90 %
91 % sMap = som_randinit(D);
92 % sMap = som_randinit(D,sMap);
93 % sMap = som_randinit(D,sTopol);
94 % sMap = som_randinit(D,'msize',[10 10]);
95 % sMap = som_randinit(D,'munits',100,'hexa');
96 %
97 % SEE ALSO
98 %
99 % som_map_struct Create a map struct.
100 % som_lininit Initialize a map using linear initialization algorithm.
101 % som_make Initialize and train self-organizing map.
102
103 % Copyright (c) 1997-2000 by the SOM toolbox programming team.
104 % http://www.cis.hut.fi/projects/somtoolbox/
105
106 % Version 1.0beta ecco 100997
107 % Version 2.0beta juuso 101199
108
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 %% check arguments
111
112 % data
113 if isstruct(D),
114 data_name = D.name;
115 comp_names = D.comp_names;
116 comp_norm = D.comp_norm;
117 D = D.data;
118 struct_mode = 1;
119 else
120 data_name = inputname(1);
121 struct_mode = 0;
122 end
123 [dlen dim] = size(D);
124
125 % varargin
126 sMap = [];
127 sTopol = som_topol_struct;
128 sTopol.msize = 0;
129 munits = NaN;
130 i=1;
131 while i<=length(varargin),
132 argok = 1;
133 if ischar(varargin{i}),
134 switch varargin{i},
135 case 'munits', i=i+1; munits = varargin{i}; sTopol.msize = 0;
136 case 'msize', i=i+1; sTopol.msize = varargin{i};
137 munits = prod(sTopol.msize);
138 case 'lattice', i=i+1; sTopol.lattice = varargin{i};
139 case 'shape', i=i+1; sTopol.shape = varargin{i};
140 case {'som_topol','sTopol','topol'}, i=i+1; sTopol = varargin{i};
141 case {'som_map','sMap','map'}, i=i+1; sMap = varargin{i}; sTopol = sMap.topol;
142 case {'hexa','rect'}, sTopol.lattice = varargin{i};
143 case {'sheet','cyl','toroid'}, sTopol.shape = varargin{i};
144 otherwise argok=0;
145 end
146 elseif isstruct(varargin{i}) & isfield(varargin{i},'type'),
147 switch varargin{i}.type,
148 case 'som_topol',
149 sTopol = varargin{i};
150 case 'som_map',
151 sMap = varargin{i};
152 sTopol = sMap.topol;
153 otherwise argok=0;
154 end
155 else
156 argok = 0;
157 end
158 if ~argok,
159 disp(['(som_topol_struct) Ignoring invalid argument #' num2str(i)]);
160 end
161 i = i+1;
162 end
163
164 if ~isempty(sMap),
165 [munits dim2] = size(sMap.codebook);
166 if dim2 ~= dim, error('Map and data must have the same dimension.'); end
167 end
168
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 %% create map
171
172 % map struct
173 if ~isempty(sMap),
174 sMap = som_set(sMap,'topol',sTopol);
175 else
176 if ~prod(sTopol.msize),
177 if isnan(munits),
178 sTopol = som_topol_struct('data',D,sTopol);
179 else
180 sTopol = som_topol_struct('data',D,'munits',munits,sTopol);
181 end
182 end
183 sMap = som_map_struct(dim, sTopol);
184 end
185
186 if struct_mode,
187 sMap = som_set(sMap,'comp_names',comp_names,'comp_norm',comp_norm);
188 end
189
190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 %% initialization
192
193 % train struct
194 sTrain = som_train_struct('algorithm','randinit');
195 sTrain = som_set(sTrain,'data_name',data_name);
196
197 munits = prod(sMap.topol.msize);
198 sMap.codebook = rand([munits dim]);
199
200 % set interval of each component to correct value
201 for i = 1:dim,
202 inds = find(~isnan(D(:,i)) & ~isinf(D(:,i)));
203 if isempty(inds), mi = 0; ma = 1;
204 else ma = max(D(inds,i)); mi = min(D(inds,i));
205 end
206 sMap.codebook(:,i) = (ma - mi) * sMap.codebook(:,i) + mi;
207 end
208
209 % training struct
210 sTrain = som_set(sTrain,'time',datestr(now,0));
211 sMap.trainhist = sTrain;
212
213 return;
214
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%