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