forked from ilarinieminen/SOM-Toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
som_randinit.m
215 lines (197 loc) · 6.34 KB
/
som_randinit.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
function sMap = som_randinit(D, varargin)
%SOM_RANDINIT Initialize a Self-Organizing Map with random values.
%
% sMap = som_randinit(D, [[argID,] value, ...])
%
% sMap = som_randinit(D);
% sMap = som_randinit(D,sMap);
% sMap = som_randinit(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_randinit' or check out online documentation.
% See also SOM_MAP_STRUCT, SOM_LININIT, SOM_MAKE.
%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% som_randinit
%
% PURPOSE
%
% Initializes a SOM with random values.
%
% SYNTAX
%
% sMap = som_randinit(D)
% sMap = som_randinit(D,sMap);
% sMap = som_randinit(D,'munits',100,'hexa');
%
% DESCRIPTION
%
% Initializes a SOM with random values. If necessary, a map struct
% is created first. For each component (xi), the values are uniformly
% distributed in the range of [min(xi) max(xi)].
%
% 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_randinit(D);
% sMap = som_randinit(D,sMap);
% sMap = som_randinit(D,sTopol);
% sMap = som_randinit(D,'msize',[10 10]);
% sMap = som_randinit(D,'munits',100,'hexa');
%
% SEE ALSO
%
% som_map_struct Create a map struct.
% som_lininit Initialize a map using linear initialization algorithm.
% 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 juuso 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 ~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','randinit');
sTrain = som_set(sTrain,'data_name',data_name);
munits = prod(sMap.topol.msize);
sMap.codebook = rand([munits dim]);
% set interval of each component to correct value
for i = 1:dim,
inds = find(~isnan(D(:,i)) & ~isinf(D(:,i)));
if isempty(inds), mi = 0; ma = 1;
else ma = max(D(inds,i)); mi = min(D(inds,i));
end
sMap.codebook(:,i) = (ma - mi) * sMap.codebook(:,i) + mi;
end
% training struct
sTrain = som_set(sTrain,'time',datestr(now,0));
sMap.trainhist = sTrain;
return;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%