forked from ilarinieminen/SOM-Toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
som_lininit.m
285 lines (258 loc) · 8.12 KB
/
som_lininit.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
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
function sMap = som_lininit(D, varargin)
%SOM_LININIT Initialize a Self-Organizing Map linearly.
%
% sMap = som_lininit(D, [[argID,] value, ...])
%
% sMap = som_lininit(D);
% sMap = som_lininit(D,sMap);
% sMap = som_lininit(D,'munits',100,'hexa');
%
% Input and output arguments ([]'s are optional):
% D The training data.
% (struct) data struct
% (matrix) data matrix, size dlen x dim
% [argID, (string) Parameters affecting the map topology are given
% value] (varies) as argument ID - argument value pairs, listed below.
% sMap (struct) map struct
%
% Here are the valid argument IDs and corresponding values. The values
% which are unambiguous (marked with '*') can be given without the
% preceeding argID.
% 'munits' (scalar) number of map units
% 'msize' (vector) map size
% 'lattice' *(string) map lattice: 'hexa' or 'rect'
% 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid'
% 'topol' *(struct) topology struct
% 'som_topol','sTopol' = 'topol'
% 'map' *(struct) map struct
% 'som_map','sMap' = 'map'
%
% For more help, try 'type som_lininit' or check out online documentation.
% See also SOM_MAP_STRUCT, SOM_RANDINIT, SOM_MAKE.
%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% som_lininit
%
% PURPOSE
%
% Initializes a SOM linearly along its greatest eigenvectors.
%
% SYNTAX
%
% sMap = som_lininit(D);
% sMap = som_lininit(D,sMap);
% sMap = som_lininit(D,'munits',100,'hexa');
%
% DESCRIPTION
%
% Initializes a SOM linearly. If necessary, a map struct is created
% first. The initialization is made by first calculating the eigenvalues
% and eigenvectors of the training data. Then, the map is initialized
% along the mdim greatest eigenvectors of the training data, where
% mdim is the dimension of the map grid.
%
% REFERENCES
%
% Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag,
% Berlin, 1995, pp. 106-107.
%
% REQUIRED INPUT ARGUMENTS
%
% D The training data.
% (struct) Data struct. If this is given, its '.comp_names' and
% '.comp_norm' fields are copied to the map struct.
% (matrix) data matrix, size dlen x dim
%
% OPTIONAL INPUT ARGUMENTS
%
% argID (string) Argument identifier string (see below).
% value (varies) Value for the argument (see below).
%
% The optional arguments can be given as 'argID',value -pairs. If an
% argument is given value multiple times, the last one is used.
%
% Here are the valid argument IDs and corresponding values. The values
% which are unambiguous (marked with '*') can be given without the
% preceeding argID.
% 'dlen' (scalar) length of the training data
% 'data' (matrix) the training data
% *(struct) the training data
% 'munits' (scalar) number of map units
% 'msize' (vector) map size
% 'lattice' *(string) map lattice: 'hexa' or 'rect'
% 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid'
% 'topol' *(struct) topology struct
% 'som_topol','sTopol' = 'topol'
% 'map' *(struct) map struct
% 'som_map','sMap' = 'map'
%
% OUTPUT ARGUMENTS
%
% sMap (struct) The initialized map struct.
%
% EXAMPLES
%
% sMap = som_lininit(D);
% sMap = som_lininit(D,sMap);
% sMap = som_lininit(D,'msize',[10 10]);
% sMap = som_lininit(D,'munits',100,'rect');
%
% SEE ALSO
%
% som_map_struct Create a map struct.
% som_randinit Initialize a map with random values.
% som_make Initialize and train self-organizing map.
% Copyright (c) 1997-2000 by the SOM toolbox programming team.
% http://www.cis.hut.fi/projects/somtoolbox/
% Version 1.0beta ecco 100997
% Version 2.0beta 101199
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% check arguments
% data
if isstruct(D),
data_name = D.name;
comp_names = D.comp_names;
comp_norm = D.comp_norm;
D = D.data;
struct_mode = 1;
else
data_name = inputname(1);
struct_mode = 0;
end
[dlen dim] = size(D);
% varargin
sMap = [];
sTopol = som_topol_struct;
sTopol.msize = 0;
munits = NaN;
i=1;
while i<=length(varargin),
argok = 1;
if ischar(varargin{i}),
switch varargin{i},
case 'munits', i=i+1; munits = varargin{i}; sTopol.msize = 0;
case 'msize', i=i+1; sTopol.msize = varargin{i};
munits = prod(sTopol.msize);
case 'lattice', i=i+1; sTopol.lattice = varargin{i};
case 'shape', i=i+1; sTopol.shape = varargin{i};
case {'som_topol','sTopol','topol'}, i=i+1; sTopol = varargin{i};
case {'som_map','sMap','map'}, i=i+1; sMap = varargin{i}; sTopol = sMap.topol;
case {'hexa','rect'}, sTopol.lattice = varargin{i};
case {'sheet','cyl','toroid'}, sTopol.shape = varargin{i};
otherwise argok=0;
end
elseif isstruct(varargin{i}) && isfield(varargin{i},'type'),
switch varargin{i}.type,
case 'som_topol',
sTopol = varargin{i};
case 'som_map',
sMap = varargin{i};
sTopol = sMap.topol;
otherwise argok=0;
end
else
argok = 0;
end
if ~argok,
disp(['(som_topol_struct) Ignoring invalid argument #' num2str(i)]);
end
i = i+1;
end
if length(sTopol.msize)==1, sTopol.msize = [sTopol.msize 1]; end
if ~isempty(sMap),
[munits dim2] = size(sMap.codebook);
if dim2 ~= dim, error('Map and data must have the same dimension.'); end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% create map
% map struct
if ~isempty(sMap),
sMap = som_set(sMap,'topol',sTopol);
else
if ~prod(sTopol.msize),
if isnan(munits),
sTopol = som_topol_struct('data',D,sTopol);
else
sTopol = som_topol_struct('data',D,'munits',munits,sTopol);
end
end
sMap = som_map_struct(dim, sTopol);
end
if struct_mode,
sMap = som_set(sMap,'comp_names',comp_names,'comp_norm',comp_norm);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% initialization
% train struct
sTrain = som_train_struct('algorithm','lininit');
sTrain = som_set(sTrain,'data_name',data_name);
msize = sMap.topol.msize;
mdim = length(msize);
munits = prod(msize);
[dlen dim] = size(D);
if dlen<2,
%if dlen==1, sMap.codebook = (sMap.codebook - 0.5)*diag(D); end
error(['Linear map initialization requires at least two NaN-free' ...
' samples.']);
return;
end
% compute principle components
if dim > 1 && sum(msize > 1) > 1,
% calculate mdim largest eigenvalues and their corresponding
% eigenvectors
% autocorrelation matrix
A = zeros(dim);
me = zeros(1,dim);
for i=1:dim,
me(i) = mean(D(isfinite(D(:,i)),i));
D(:,i) = D(:,i) - me(i);
end
for i=1:dim,
for j=i:dim,
c = D(:,i).*D(:,j); c = c(isfinite(c));
A(i,j) = sum(c)/length(c); A(j,i) = A(i,j);
end
end
% take mdim first eigenvectors with the greatest eigenvalues
[V,S] = eig(A);
eigval = diag(S);
[y,ind] = sort(eigval);
eigval = eigval(flipud(ind));
V = V(:,flipud(ind));
V = V(:,1:mdim);
eigval = eigval(1:mdim);
% normalize eigenvectors to unit length and multiply them by
% corresponding (square-root-of-)eigenvalues
for i=1:mdim, V(:,i) = (V(:,i) / norm(V(:,i))) * sqrt(eigval(i)); end
else
me = zeros(1,dim);
V = zeros(1,dim);
for i=1:dim,
inds = find(~isnan(D(:,i)));
me(i) = mean(D(inds,i),1);
V(i) = std(D(inds,i),1);
end
end
% initialize codebook vectors
if dim>1,
sMap.codebook = me(ones(munits,1),:);
Coords = som_unit_coords(msize,'rect','sheet');
cox = Coords(:,1); Coords(:,1) = Coords(:,2); Coords(:,2) = cox;
for i=1:mdim,
ma = max(Coords(:,i)); mi = min(Coords(:,i));
if ma>mi, Coords(:,i) = (Coords(:,i)-mi)/(ma-mi); else Coords(:,i) = 0.5; end
end
Coords = (Coords-0.5)*2;
for n = 1:munits,
for d = 1:mdim,
sMap.codebook(n,:) = sMap.codebook(n,:)+Coords(n,d)*V(:, d)';
end
end
else
sMap.codebook = [0:(munits-1)]'/(munits-1)*(max(D)-min(D))+min(D);
end
% training struct
sTrain = som_set(sTrain,'time',datestr(now,0));
sMap.trainhist = sTrain;
return;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%