annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_supervised.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 sM = som_supervised(sData,varargin)
Daniel@0 2
Daniel@0 3 %SOM_SUPERVISED SOM training which utilizes class information.
Daniel@0 4 %
Daniel@0 5 % sM = som_supervised(sData, [ArgID, value,...]))
Daniel@0 6 %
Daniel@0 7 % Input and output arguments ([]'s are optional)
Daniel@0 8 % sData (struct) data struct, the class information is
Daniel@0 9 % taken from the first column of .labels field
Daniel@0 10 % [argID, (string) See below. These are given as
Daniel@0 11 % value] (varies) 'argID', value -pairs.
Daniel@0 12 %
Daniel@0 13 % sMap (struct) map struct
Daniel@0 14 %
Daniel@0 15 % Here are the argument IDs and corresponding values:
Daniel@0 16 % 'munits' (scalar) the preferred number of map units
Daniel@0 17 % 'msize' (vector) map grid size
Daniel@0 18 % 'mask' (vector) BMU search mask, size dim x 1
Daniel@0 19 % 'name' (string) map name
Daniel@0 20 % 'comp_names' (string array / cellstr) component names, size dim x 1
Daniel@0 21 % 'tracking' (scalar) how much to report, default = 1
Daniel@0 22 % The following values are unambiguous and can therefore
Daniel@0 23 % be given without the preceeding argument ID:
Daniel@0 24 % 'algorithm' (string) training algorithm: 'seq' or 'batch'
Daniel@0 25 % 'mapsize' (string) do you want a 'small', 'normal' or 'big' map
Daniel@0 26 % Any explicit settings of munits or msize override this.
Daniel@0 27 % 'topol' (struct) topology struct
Daniel@0 28 % 'som_topol','sTopol' = 'topol'
Daniel@0 29 % 'lattice' (string) map lattice, 'hexa' or 'rect'
Daniel@0 30 % 'shape' (string) map shape, 'sheet', 'cyl' or 'toroid'
Daniel@0 31 % 'neigh' (string) neighborhood function, 'gaussian', 'cutgauss',
Daniel@0 32 % 'ep' or 'bubble'
Daniel@0 33 %
Daniel@0 34 % For more help, try 'type som_supervised', or check out online documentation.
Daniel@0 35 % See also SOM_MAKE, SOM_AUTOLABEL.
Daniel@0 36
Daniel@0 37 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 38 %
Daniel@0 39 % som_supervised
Daniel@0 40 %
Daniel@0 41 % PURPOSE
Daniel@0 42 %
Daniel@0 43 % Creates, initializes and trains a supervised SOM by taking the
Daniel@0 44 % class-identity into account.
Daniel@0 45 %
Daniel@0 46 % SYNTAX
Daniel@0 47 %
Daniel@0 48 % sMap = som_supervised(sData);
Daniel@0 49 % sMap = som_supervised(...,'argID',value,...)
Daniel@0 50 % sMap = som_make(...,value,...);
Daniel@0 51 %
Daniel@0 52 % DESCRIPTION
Daniel@0 53 %
Daniel@0 54 % Creates, initializes and trains a supervised SOM. It constructs the
Daniel@0 55 % training data by adding 1-of-N -coded matrix to the original data
Daniel@0 56 % based on the class information in the .labels field. The dimension
Daniel@0 57 % of vectors after the process is (the old dimension + number of
Daniel@0 58 % different classes). In each vector, one of the new components has
Daniel@0 59 % value '1' (this depends on the class of the vector), and others '0'.
Daniel@0 60 % Calls SOM_MAKE to construct the map. Then the class of each map unit
Daniel@0 61 % is determined by taking maximum over these added components, and a
Daniel@0 62 % label is give accordingly. Finally, the extra components (the
Daniel@0 63 % 1-of-N -coded ones) are removed.
Daniel@0 64 %
Daniel@0 65 % REFERENCES
Daniel@0 66 %
Daniel@0 67 % Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag,
Daniel@0 68 % Berlin, 1995, pp. 160-161.
Daniel@0 69 % Kohonen, T., Mäkivasara, K., Saramäki, T., "Phonetic Maps -
Daniel@0 70 % Insightful Representation of Phonological Features For
Daniel@0 71 % Speech Recognition", In proceedings of International
Daniel@0 72 % Conference on Pattern Recognition (ICPR), Montreal, Canada,
Daniel@0 73 % 1984, pp. 182-185.
Daniel@0 74 %
Daniel@0 75 % REQUIRED INPUT ARGUMENTS
Daniel@0 76 %
Daniel@0 77 % sData The data to use in the training.
Daniel@0 78 % (struct) A data struct. '.comp_names' as well as '.name'
Daniel@0 79 % is copied to the map. The class information is
Daniel@0 80 % taken from the first column of '.labels' field.
Daniel@0 81 %
Daniel@0 82 % OPTIONAL INPUT ARGUMENTS
Daniel@0 83 %
Daniel@0 84 % argID (string) Argument identifier string (see below).
Daniel@0 85 % value (varies) Value for the argument (see below).
Daniel@0 86 %
Daniel@0 87 % The optional arguments can be given as 'argID',value -pairs. If an
Daniel@0 88 % argument is given value multiple times, the last one is used.
Daniel@0 89 % Here are the argument IDs and corresponding values:
Daniel@0 90 % 'munits' (scalar) the preferred number of map units - this may
Daniel@0 91 % change a bit, depending on the properties of the data
Daniel@0 92 % 'msize' (vector) map grid size
Daniel@0 93 % 'mask' (vector) BMU search mask, size dim x 1
Daniel@0 94 % 'name' (string) map name
Daniel@0 95 % 'comp_names' (string array / cellstr) component names, size dim x 1
Daniel@0 96 % 'tracking' (scalar) how much to report, default = 1. This parameter
Daniel@0 97 % is also passed to the training functions.
Daniel@0 98 % The following values are unambiguous and can therefore
Daniel@0 99 % be given without the preceeding argument ID:
Daniel@0 100 % 'algorithm' (string) training algorithm: 'seq' or 'batch' (default)
Daniel@0 101 % 'mapsize' (string) do you want a 'small', 'normal' or 'big' map
Daniel@0 102 % Any explicit settings of munits or msize (or topol)
Daniel@0 103 % override this.
Daniel@0 104 % 'topol' (struct) topology struct
Daniel@0 105 % 'som_topol','sTopol' = 'topol'
Daniel@0 106 % 'lattice' (string) map lattice, 'hexa' or 'rect'
Daniel@0 107 % 'shape' (string) map shape, 'sheet', 'cyl' or 'toroid'
Daniel@0 108 % 'neigh' (string) neighborhood function, 'gaussian', 'cutgauss',
Daniel@0 109 % 'ep' or 'bubble'
Daniel@0 110 %
Daniel@0 111 % OUTPUT ARGUMENTS
Daniel@0 112 %
Daniel@0 113 % sMap (struct) SOM -map struct
Daniel@0 114 %
Daniel@0 115 % EXAMPLES
Daniel@0 116 %
Daniel@0 117 % To simply train a map with default parameters:
Daniel@0 118 %
Daniel@0 119 % sMap = som_supervised(sData);
Daniel@0 120 %
Daniel@0 121 % With the optional arguments, the initialization and training can be
Daniel@0 122 % influenced. To change map size, use 'msize', 'munits' or 'mapsize'
Daniel@0 123 % arguments:
Daniel@0 124 %
Daniel@0 125 % sMap = som_supervised(D,'mapsize','big'); or
Daniel@0 126 % sMap = som_supervised(D,'big');
Daniel@0 127 % sMap = som_supervised(D,'munits', 100);
Daniel@0 128 % sMap = som_supervised(D,'msize', [20 10]);
Daniel@0 129 %
Daniel@0 130 % Argument 'algorithm' can be used to switch between 'seq' and 'batch'
Daniel@0 131 % algorithms. 'batch' is the default, so to use 'seq' algorithm:
Daniel@0 132 %
Daniel@0 133 % sMap = som_supervised(D,'algorithm','seq'); or
Daniel@0 134 % sMap = som_supervised(D,'seq');
Daniel@0 135 %
Daniel@0 136 % The 'tracking' argument can be used to control the amout of reporting
Daniel@0 137 % during training. The argument is used in this function, and it is
Daniel@0 138 % passed to the training functions. To make the function work silently
Daniel@0 139 % set it to 0.
Daniel@0 140 %
Daniel@0 141 % sMap = som_supervised(D,'tracking',0);
Daniel@0 142 %
Daniel@0 143 % SEE ALSO
Daniel@0 144 %
Daniel@0 145 % som_make Create, initialize and train Self-Organizing map.
Daniel@0 146 % som_autolabel Label SOM/data set based on another SOM/data set.
Daniel@0 147
Daniel@0 148 % Contributed to SOM Toolbox vs2, Feb 2nd, 2000 by Juha Parhankangas
Daniel@0 149 % Copyright (c) by Juha Parhankangas
Daniel@0 150 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 151
Daniel@0 152 % Juha Parhankangas 050100
Daniel@0 153
Daniel@0 154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 155
Daniel@0 156 D0 = sData.data;
Daniel@0 157 [c,n,classlabels] = class2num(sData.labels(:,1));
Daniel@0 158
Daniel@0 159 %%%%%%%% Checking arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 160
Daniel@0 161 if ~isstruct(sData)
Daniel@0 162 error('Argument ''sData'' must be a ''som_data'' -struct.');
Daniel@0 163 else
Daniel@0 164 data_name = sData.name;
Daniel@0 165 comp_names = sData.comp_names;
Daniel@0 166 comp_norm = sData.comp_norm;
Daniel@0 167 end
Daniel@0 168
Daniel@0 169 [dlen,dim] = size(sData.data);
Daniel@0 170
Daniel@0 171 % defaults
Daniel@0 172
Daniel@0 173 mapsize = '';
Daniel@0 174 sM = som_map_struct(dim+n);
Daniel@0 175 sTopol = sM.topol;
Daniel@0 176 munits = prod(sTopol.msize); % should be zero
Daniel@0 177 mask = sM.mask;
Daniel@0 178 name = sM.name;
Daniel@0 179 neigh = sM.neigh;
Daniel@0 180 tracking = 1;
Daniel@0 181 algorithm = 'batch';
Daniel@0 182
Daniel@0 183 %%%% changes to defaults (checking varargin) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 184
Daniel@0 185 i=1;
Daniel@0 186 while i <= length(varargin)
Daniel@0 187 argok = 1;
Daniel@0 188 if ischar(varargin{i})
Daniel@0 189 switch varargin{i},
Daniel@0 190 % argument IDs
Daniel@0 191 case 'mask',
Daniel@0 192 i=i+1;
Daniel@0 193 mask = varargin{i};
Daniel@0 194 case 'munits',
Daniel@0 195 i=i+1;
Daniel@0 196 munits = varargin{i};
Daniel@0 197 case 'msize',
Daniel@0 198 i=i+1;
Daniel@0 199 sTopol.msize = varargin{i};
Daniel@0 200 munits = prod(sTopol.msize);
Daniel@0 201 case 'mapsize',
Daniel@0 202 i=i+1;
Daniel@0 203 mapsize = varargin{i};
Daniel@0 204 case 'name',
Daniel@0 205 i=i+1;
Daniel@0 206 name = varargin{i};
Daniel@0 207 case 'comp_names',
Daniel@0 208 i=i+1;
Daniel@0 209 comp_names = varargin{i};
Daniel@0 210 case 'lattice',
Daniel@0 211 i=i+1;
Daniel@0 212 sTopol.lattice = varargin{i};
Daniel@0 213 case 'shape',
Daniel@0 214 i=i+1;
Daniel@0 215 sTopol.shape = varargin{i};
Daniel@0 216 case {'topol','som_topol','sTopol'},
Daniel@0 217 i=i+1;
Daniel@0 218 sTopol = varargin{i};
Daniel@0 219 munits = prod(sTopol.msize);
Daniel@0 220 case 'neigh',
Daniel@0 221 i=i+1;
Daniel@0 222 neigh = varargin{i};
Daniel@0 223 case 'tracking',
Daniel@0 224 i=i+1;
Daniel@0 225 tracking = varargin{i};
Daniel@0 226 case 'algorithm',
Daniel@0 227 i=i+1;
Daniel@0 228 algorithm = varargin{i};
Daniel@0 229 % unambiguous values
Daniel@0 230 case {'hexa','rect'},
Daniel@0 231 sTopol.lattice = varargin{i};
Daniel@0 232 case {'sheet','cyl','toroid'},
Daniel@0 233 sTopol.shape = varargin{i};
Daniel@0 234 case {'gaussian','cutgauss','ep','bubble'},
Daniel@0 235 neigh = varargin{i};
Daniel@0 236 case {'seq','batch'},
Daniel@0 237 algorithm = varargin{i};
Daniel@0 238 case {'small','normal','big'},
Daniel@0 239 mapsize = varargin{i};
Daniel@0 240 otherwise argok=0;
Daniel@0 241 end
Daniel@0 242 elseif isstruct(varargin{i}) & isfield(varargin{i},'type'),
Daniel@0 243 switch varargin{i}(1).type,
Daniel@0 244 case 'som_topol',
Daniel@0 245 sTopol = varargin{i};
Daniel@0 246 otherwise argok=0;
Daniel@0 247 end
Daniel@0 248 else
Daniel@0 249 argok = 0;
Daniel@0 250 end
Daniel@0 251 if ~argok,
Daniel@0 252 disp(['(som_supervised) Ignoring invalid argument #' num2str(i+1)]);
Daniel@0 253 end
Daniel@0 254 i = i+1;
Daniel@0 255 end
Daniel@0 256
Daniel@0 257 %%%%%%%% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 258
Daniel@0 259
Daniel@0 260
Daniel@0 261 % constructing the training data by adding 1-of-N -coded matrix to the
Daniel@0 262 % original data.
Daniel@0 263
Daniel@0 264 [dlen,dim] = size(D0);
Daniel@0 265
Daniel@0 266 Dc = zeros(dlen,n);
Daniel@0 267
Daniel@0 268 for i=1:dlen
Daniel@0 269 if c(i)
Daniel@0 270 Dc(i,c(i)) = 1;
Daniel@0 271 end
Daniel@0 272 end
Daniel@0 273
Daniel@0 274 D = [D0, Dc];
Daniel@0 275
Daniel@0 276 % initialization and training
Daniel@0 277
Daniel@0 278 sD = som_data_struct(D,...
Daniel@0 279 'name',data_name);
Daniel@0 280
Daniel@0 281 sM = som_make(sD,...
Daniel@0 282 'mask',mask,...
Daniel@0 283 'munits',munits,...
Daniel@0 284 'name',data_name,...
Daniel@0 285 'tracking',tracking,...
Daniel@0 286 'algorithm',algorithm,...
Daniel@0 287 'mapsize',mapsize,...
Daniel@0 288 'topol',sTopol,...
Daniel@0 289 'neigh',neigh);
Daniel@0 290
Daniel@0 291 % add labels
Daniel@0 292
Daniel@0 293 for i=1:prod(sM.topol.msize),
Daniel@0 294 [dummy,class] = max(sM.codebook(i,dim+[1:n]));
Daniel@0 295 sM.labels{i} = classlabels{class};
Daniel@0 296 end
Daniel@0 297
Daniel@0 298 %sD.labels = sData.labels;
Daniel@0 299 %sM = som_autolabel(sM,sD,'vote');
Daniel@0 300
Daniel@0 301 % remove extra components and modify map -struct
Daniel@0 302
Daniel@0 303 sM.codebook = sM.codebook(:,1:dim);
Daniel@0 304 sM.mask = sM.mask(1:dim);
Daniel@0 305 sM.comp_names = sData.comp_names;
Daniel@0 306 sM.comp_norm = sData.comp_norm;
Daniel@0 307
Daniel@0 308 % remove extras from sM.trainhist
Daniel@0 309
Daniel@0 310 for i=1:length(sM.trainhist)
Daniel@0 311 if sM.trainhist(i).mask
Daniel@0 312 sM.trainhist(i).mask = sM.trainhist(i).mask(1:dim);
Daniel@0 313 end
Daniel@0 314 end
Daniel@0 315
Daniel@0 316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 317
Daniel@0 318 function [numbers, n, names] = class2num(class)
Daniel@0 319
Daniel@0 320 names = {};
Daniel@0 321 numbers = zeros(length(class),1);
Daniel@0 322
Daniel@0 323 for i=1:length(class)
Daniel@0 324 if ~isempty(class{i}) & ~any(strcmp(class{i},names))
Daniel@0 325 names=cat(1,names,class(i));
Daniel@0 326 end
Daniel@0 327 end
Daniel@0 328
Daniel@0 329 n=length(names);
Daniel@0 330
Daniel@0 331 tmp_numbers = (1:n)';
Daniel@0 332
Daniel@0 333 for i=1:length(class)
Daniel@0 334 if ~isempty(class{i})
Daniel@0 335 numbers(i,1) = find(strcmp(class{i},names));
Daniel@0 336 end
Daniel@0 337 end