Mercurial > hg > camir-aes2014
comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_lininit.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_lininit(D, varargin) | |
2 | |
3 %SOM_LININIT Initialize a Self-Organizing Map linearly. | |
4 % | |
5 % sMap = som_lininit(D, [[argID,] value, ...]) | |
6 % | |
7 % sMap = som_lininit(D); | |
8 % sMap = som_lininit(D,sMap); | |
9 % sMap = som_lininit(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 % sMap (struct) map struct | |
18 % | |
19 % Here are the valid argument IDs and corresponding values. The values | |
20 % which are unambiguous (marked with '*') can be given without the | |
21 % preceeding argID. | |
22 % 'munits' (scalar) number of map units | |
23 % 'msize' (vector) map size | |
24 % 'lattice' *(string) map lattice: 'hexa' or 'rect' | |
25 % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid' | |
26 % 'topol' *(struct) topology struct | |
27 % 'som_topol','sTopol' = 'topol' | |
28 % 'map' *(struct) map struct | |
29 % 'som_map','sMap' = 'map' | |
30 % | |
31 % For more help, try 'type som_lininit' or check out online documentation. | |
32 % See also SOM_MAP_STRUCT, SOM_RANDINIT, SOM_MAKE. | |
33 | |
34 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
35 % | |
36 % som_lininit | |
37 % | |
38 % PURPOSE | |
39 % | |
40 % Initializes a SOM linearly along its greatest eigenvectors. | |
41 % | |
42 % SYNTAX | |
43 % | |
44 % sMap = som_lininit(D); | |
45 % sMap = som_lininit(D,sMap); | |
46 % sMap = som_lininit(D,'munits',100,'hexa'); | |
47 % | |
48 % DESCRIPTION | |
49 % | |
50 % Initializes a SOM linearly. If necessary, a map struct is created | |
51 % first. The initialization is made by first calculating the eigenvalues | |
52 % and eigenvectors of the training data. Then, the map is initialized | |
53 % along the mdim greatest eigenvectors of the training data, where | |
54 % mdim is the dimension of the map grid. | |
55 % | |
56 % REFERENCES | |
57 % | |
58 % Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag, | |
59 % Berlin, 1995, pp. 106-107. | |
60 % | |
61 % REQUIRED INPUT ARGUMENTS | |
62 % | |
63 % D The training data. | |
64 % (struct) Data struct. If this is given, its '.comp_names' and | |
65 % '.comp_norm' fields are copied to the map struct. | |
66 % (matrix) data matrix, size dlen x dim | |
67 % | |
68 % OPTIONAL INPUT ARGUMENTS | |
69 % | |
70 % argID (string) Argument identifier string (see below). | |
71 % value (varies) Value for the argument (see below). | |
72 % | |
73 % The optional arguments can be given as 'argID',value -pairs. If an | |
74 % argument is given value multiple times, the last one is used. | |
75 % | |
76 % Here are the valid argument IDs and corresponding values. The values | |
77 % which are unambiguous (marked with '*') can be given without the | |
78 % preceeding argID. | |
79 % 'dlen' (scalar) length of the training data | |
80 % 'data' (matrix) the training data | |
81 % *(struct) the training data | |
82 % 'munits' (scalar) number of map units | |
83 % 'msize' (vector) map size | |
84 % 'lattice' *(string) map lattice: 'hexa' or 'rect' | |
85 % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid' | |
86 % 'topol' *(struct) topology struct | |
87 % 'som_topol','sTopol' = 'topol' | |
88 % 'map' *(struct) map struct | |
89 % 'som_map','sMap' = 'map' | |
90 % | |
91 % OUTPUT ARGUMENTS | |
92 % | |
93 % sMap (struct) The initialized map struct. | |
94 % | |
95 % EXAMPLES | |
96 % | |
97 % sMap = som_lininit(D); | |
98 % sMap = som_lininit(D,sMap); | |
99 % sMap = som_lininit(D,'msize',[10 10]); | |
100 % sMap = som_lininit(D,'munits',100,'rect'); | |
101 % | |
102 % SEE ALSO | |
103 % | |
104 % som_map_struct Create a map struct. | |
105 % som_randinit Initialize a map with random values. | |
106 % som_make Initialize and train self-organizing map. | |
107 | |
108 % Copyright (c) 1997-2000 by the SOM toolbox programming team. | |
109 % http://www.cis.hut.fi/projects/somtoolbox/ | |
110 | |
111 % Version 1.0beta ecco 100997 | |
112 % Version 2.0beta 101199 | |
113 | |
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
115 %% check arguments | |
116 | |
117 % data | |
118 if isstruct(D), | |
119 data_name = D.name; | |
120 comp_names = D.comp_names; | |
121 comp_norm = D.comp_norm; | |
122 D = D.data; | |
123 struct_mode = 1; | |
124 else | |
125 data_name = inputname(1); | |
126 struct_mode = 0; | |
127 end | |
128 [dlen dim] = size(D); | |
129 | |
130 % varargin | |
131 sMap = []; | |
132 sTopol = som_topol_struct; | |
133 sTopol.msize = 0; | |
134 munits = NaN; | |
135 i=1; | |
136 while i<=length(varargin), | |
137 argok = 1; | |
138 if ischar(varargin{i}), | |
139 switch varargin{i}, | |
140 case 'munits', i=i+1; munits = varargin{i}; sTopol.msize = 0; | |
141 case 'msize', i=i+1; sTopol.msize = varargin{i}; | |
142 munits = prod(sTopol.msize); | |
143 case 'lattice', i=i+1; sTopol.lattice = varargin{i}; | |
144 case 'shape', i=i+1; sTopol.shape = varargin{i}; | |
145 case {'som_topol','sTopol','topol'}, i=i+1; sTopol = varargin{i}; | |
146 case {'som_map','sMap','map'}, i=i+1; sMap = varargin{i}; sTopol = sMap.topol; | |
147 case {'hexa','rect'}, sTopol.lattice = varargin{i}; | |
148 case {'sheet','cyl','toroid'}, sTopol.shape = varargin{i}; | |
149 otherwise argok=0; | |
150 end | |
151 elseif isstruct(varargin{i}) & isfield(varargin{i},'type'), | |
152 switch varargin{i}.type, | |
153 case 'som_topol', | |
154 sTopol = varargin{i}; | |
155 case 'som_map', | |
156 sMap = varargin{i}; | |
157 sTopol = sMap.topol; | |
158 otherwise argok=0; | |
159 end | |
160 else | |
161 argok = 0; | |
162 end | |
163 if ~argok, | |
164 disp(['(som_topol_struct) Ignoring invalid argument #' num2str(i)]); | |
165 end | |
166 i = i+1; | |
167 end | |
168 | |
169 if length(sTopol.msize)==1, sTopol.msize = [sTopol.msize 1]; end | |
170 | |
171 if ~isempty(sMap), | |
172 [munits dim2] = size(sMap.codebook); | |
173 if dim2 ~= dim, error('Map and data must have the same dimension.'); end | |
174 end | |
175 | |
176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
177 %% create map | |
178 | |
179 % map struct | |
180 if ~isempty(sMap), | |
181 sMap = som_set(sMap,'topol',sTopol); | |
182 else | |
183 if ~prod(sTopol.msize), | |
184 if isnan(munits), | |
185 sTopol = som_topol_struct('data',D,sTopol); | |
186 else | |
187 sTopol = som_topol_struct('data',D,'munits',munits,sTopol); | |
188 end | |
189 end | |
190 sMap = som_map_struct(dim, sTopol); | |
191 end | |
192 | |
193 if struct_mode, | |
194 sMap = som_set(sMap,'comp_names',comp_names,'comp_norm',comp_norm); | |
195 end | |
196 | |
197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | |
198 %% initialization | |
199 | |
200 % train struct | |
201 sTrain = som_train_struct('algorithm','lininit'); | |
202 sTrain = som_set(sTrain,'data_name',data_name); | |
203 | |
204 msize = sMap.topol.msize; | |
205 mdim = length(msize); | |
206 munits = prod(msize); | |
207 | |
208 [dlen dim] = size(D); | |
209 if dlen<2, | |
210 %if dlen==1, sMap.codebook = (sMap.codebook - 0.5)*diag(D); end | |
211 error(['Linear map initialization requires at least two NaN-free' ... | |
212 ' samples.']); | |
213 return; | |
214 end | |
215 | |
216 % compute principle components | |
217 if dim > 1 & sum(msize > 1) > 1, | |
218 % calculate mdim largest eigenvalues and their corresponding | |
219 % eigenvectors | |
220 | |
221 % autocorrelation matrix | |
222 A = zeros(dim); | |
223 me = zeros(1,dim); | |
224 for i=1:dim, | |
225 me(i) = mean(D(isfinite(D(:,i)),i)); | |
226 D(:,i) = D(:,i) - me(i); | |
227 end | |
228 for i=1:dim, | |
229 for j=i:dim, | |
230 c = D(:,i).*D(:,j); c = c(isfinite(c)); | |
231 A(i,j) = sum(c)/length(c); A(j,i) = A(i,j); | |
232 end | |
233 end | |
234 | |
235 % take mdim first eigenvectors with the greatest eigenvalues | |
236 [V,S] = eig(A); | |
237 eigval = diag(S); | |
238 [y,ind] = sort(eigval); | |
239 eigval = eigval(flipud(ind)); | |
240 V = V(:,flipud(ind)); | |
241 V = V(:,1:mdim); | |
242 eigval = eigval(1:mdim); | |
243 | |
244 % normalize eigenvectors to unit length and multiply them by | |
245 % corresponding (square-root-of-)eigenvalues | |
246 for i=1:mdim, V(:,i) = (V(:,i) / norm(V(:,i))) * sqrt(eigval(i)); end | |
247 | |
248 else | |
249 | |
250 me = zeros(1,dim); | |
251 V = zeros(1,dim); | |
252 for i=1:dim, | |
253 inds = find(~isnan(D(:,i))); | |
254 me(i) = mean(D(inds,i),1); | |
255 V(i) = std(D(inds,i),1); | |
256 end | |
257 | |
258 end | |
259 | |
260 % initialize codebook vectors | |
261 if dim>1, | |
262 sMap.codebook = me(ones(munits,1),:); | |
263 Coords = som_unit_coords(msize,'rect','sheet'); | |
264 cox = Coords(:,1); Coords(:,1) = Coords(:,2); Coords(:,2) = cox; | |
265 for i=1:mdim, | |
266 ma = max(Coords(:,i)); mi = min(Coords(:,i)); | |
267 if ma>mi, Coords(:,i) = (Coords(:,i)-mi)/(ma-mi); else Coords(:,i) = 0.5; end | |
268 end | |
269 Coords = (Coords-0.5)*2; | |
270 for n = 1:munits, | |
271 for d = 1:mdim, | |
272 sMap.codebook(n,:) = sMap.codebook(n,:)+Coords(n,d)*V(:, d)'; | |
273 end | |
274 end | |
275 else | |
276 sMap.codebook = [0:(munits-1)]'/(munits-1)*(max(D)-min(D))+min(D); | |
277 end | |
278 | |
279 % training struct | |
280 sTrain = som_set(sTrain,'time',datestr(now,0)); | |
281 sMap.trainhist = sTrain; | |
282 | |
283 return; | |
284 | |
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |