annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_randinit.m @ 0:cc4b1211e677 tip

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