Daniel@0: function sMap = som_make(D, varargin) Daniel@0: Daniel@0: %SOM_MAKE Create, initialize and train Self-Organizing Map. Daniel@0: % Daniel@0: % sMap = som_make(D, [[argID,] value, ...]) Daniel@0: % Daniel@0: % sMap = som_make(D); Daniel@0: % sMap = som_make(D, 'munits', 20); Daniel@0: % sMap = som_make(D, 'munits', 20, 'hexa', 'sheet'); Daniel@0: % sMap = som_make(D, 'msize', [4 6 7], 'lattice', 'rect'); Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % D (matrix) training data, size dlen x dim Daniel@0: % (struct) data struct Daniel@0: % [argID, (string) See below. The values which are unambiguous can Daniel@0: % value] (varies) be given without the preceeding argID. Daniel@0: % Daniel@0: % sMap (struct) map struct Daniel@0: % Daniel@0: % Here are the valid argument IDs and corresponding values. The values Daniel@0: % which are unambiguous (marked with '*') can be given without the Daniel@0: % preceeding argID. Daniel@0: % 'init' *(string) initialization: 'randinit' or 'lininit' (default) Daniel@0: % 'algorithm' *(string) training: 'seq' or 'batch' (default) or 'sompak' Daniel@0: % 'munits' (scalar) the preferred number of map units Daniel@0: % 'msize' (vector) map grid size Daniel@0: % 'mapsize' *(string) do you want a 'small', 'normal' or 'big' map Daniel@0: % Any explicit settings of munits or msize override this. Daniel@0: % 'lattice' *(string) map lattice, 'hexa' or 'rect' Daniel@0: % 'shape' *(string) map shape, 'sheet', 'cyl' or 'toroid' Daniel@0: % 'neigh' *(string) neighborhood function, 'gaussian', 'cutgauss', Daniel@0: % 'ep' or 'bubble' Daniel@0: % 'topol' *(struct) topology struct Daniel@0: % 'som_topol','sTopol' = 'topol' Daniel@0: % 'mask' (vector) BMU search mask, size dim x 1 Daniel@0: % 'name' (string) map name Daniel@0: % 'comp_names' (string array / cellstr) component names, size dim x 1 Daniel@0: % 'tracking' (scalar) how much to report, default = 1 Daniel@0: % 'training' (string) 'short', 'default', 'long' Daniel@0: % (vector) size 1 x 2, first length of rough training in epochs, Daniel@0: % and then length of finetuning in epochs Daniel@0: % Daniel@0: % For more help, try 'type som_make' or check out online documentation. Daniel@0: % See also SOM_MAP_STRUCT, SOM_TOPOL_STRUCT, SOM_TRAIN_STRUCT, Daniel@0: % SOM_RANDINIT, SOM_LININIT, SOM_SEQTRAIN, SOM_BATCHTRAIN. Daniel@0: Daniel@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Daniel@0: % som_make Daniel@0: % Daniel@0: % PURPOSE Daniel@0: % Daniel@0: % Creates, initializes and trains a SOM using default parameters. Daniel@0: % Daniel@0: % SYNTAX Daniel@0: % Daniel@0: % sMap = som_make(D); Daniel@0: % sMap = som_make(...,'argID',value,...); Daniel@0: % sMap = som_make(...,value,...); Daniel@0: % Daniel@0: % DESCRIPTION Daniel@0: % Daniel@0: % Creates, initializes and trains a SOM with default parameters. Uses functions Daniel@0: % SOM_TOPOL_STRUCT, SOM_TRAIN_STRUCT, SOM_DATA_STRUCT and SOM_MAP_STRUCT to come Daniel@0: % up with the default values. Daniel@0: % Daniel@0: % First, the number of map units is determined. Unless they are Daniel@0: % explicitly defined, function SOM_TOPOL_STRUCT is used to determine this. Daniel@0: % It uses a heuristic formula of 'munits = 5*dlen^0.54321'. The 'mapsize' Daniel@0: % argument influences the final number of map units: a 'big' map has Daniel@0: % x4 the default number of map units and a 'small' map has x0.25 the Daniel@0: % default number of map units. Daniel@0: % Daniel@0: % After the number of map units has been determined, the map size is Daniel@0: % determined. Basically, the two biggest eigenvalues of the training Daniel@0: % data are calculated and the ratio between sidelengths of the map grid Daniel@0: % is set to this ratio. The actual sidelengths are then set so that Daniel@0: % their product is as close to the desired number of map units as Daniel@0: % possible. Daniel@0: % Daniel@0: % Then the SOM is initialized. First, linear initialization along two Daniel@0: % greatest eigenvectors is tried, but if this can't be done (the Daniel@0: % eigenvectors cannot be calculated), random initialization is used Daniel@0: % instead. After initialization, the SOM is trained in two phases: Daniel@0: % first rough training and then fine-tuning. If the 'tracking' Daniel@0: % argument is greater than zero, the average quantization error and Daniel@0: % topographic error of the final map are calculated. Daniel@0: % Daniel@0: % REQUIRED INPUT ARGUMENTS Daniel@0: % Daniel@0: % D The data to use in the training. Daniel@0: % (struct) A data struct. If a struct is given, '.comp_names' field as Daniel@0: % well as '.comp_norm' field is copied to the map struct. Daniel@0: % (matrix) A data matrix, size dlen x dim. The data matrix may Daniel@0: % contain unknown values, indicated by NaNs. Daniel@0: % Daniel@0: % OPTIONAL INPUT ARGUMENTS Daniel@0: % Daniel@0: % argID (string) Argument identifier string (see below). Daniel@0: % value (varies) Value for the argument (see below). Daniel@0: % Daniel@0: % Here are the valid argument IDs and corresponding values. The values Daniel@0: % which are unambiguous (marked with '*') can be given without the Daniel@0: % preceeding argID. Daniel@0: % 'init' *(string) initialization: 'randinit' or 'lininit' (default) Daniel@0: % 'algorithm' *(string) training: 'seq' or 'batch' (default) or 'sompak' Daniel@0: % 'munits' (scalar) the preferred number of map units Daniel@0: % 'msize' (vector) map grid size Daniel@0: % 'mapsize' *(string) do you want a 'small', 'normal' or 'big' map Daniel@0: % Any explicit settings of munits or msize override this. Daniel@0: % 'lattice' *(string) map lattice, 'hexa' or 'rect' Daniel@0: % 'shape' *(string) map shape, 'sheet', 'cyl' or 'toroid' Daniel@0: % 'neigh' *(string) neighborhood function, 'gaussian', 'cutgauss', Daniel@0: % 'ep' or 'bubble' Daniel@0: % 'topol' *(struct) topology struct Daniel@0: % 'som_topol','sTopol' = 'topol' Daniel@0: % 'mask' (vector) BMU search mask, size dim x 1 Daniel@0: % 'name' (string) map name Daniel@0: % 'comp_names' (string array / cellstr) component names, size dim x 1 Daniel@0: % 'tracking' (scalar) how much to report, default = 1 Daniel@0: % 'training' (string) 'short', 'default' or 'long' Daniel@0: % (vector) size 1 x 2, first length of rough training in epochs, Daniel@0: % and then length of finetuning in epochs Daniel@0: % Daniel@0: % OUTPUT ARGUMENTS Daniel@0: % Daniel@0: % sMap (struct) the trained map struct Daniel@0: % Daniel@0: % EXAMPLES Daniel@0: % Daniel@0: % To simply train a map with default parameters: Daniel@0: % Daniel@0: % sMap = som_make(D); Daniel@0: % Daniel@0: % With the optional arguments, the initialization and training can be Daniel@0: % influenced. To change map size, use 'msize', 'munits' or 'mapsize' Daniel@0: % arguments: Daniel@0: % Daniel@0: % sMap = som_make(D,'mapsize','big'); or sMap=som_make(D,'big'); Daniel@0: % sMap = som_make(D,'munits', 100); Daniel@0: % sMap = som_make(D,'msize', [20 10]); Daniel@0: % Daniel@0: % Argument 'algorithm' can be used to switch between 'seq' and 'batch' Daniel@0: % algorithms. 'batch' is the default, so to use 'seq' algorithm: Daniel@0: % Daniel@0: % sMap = som_make(D,'algorithm','seq'); or sMap = som_make(D,'seq'); Daniel@0: % Daniel@0: % The 'tracking' argument can be used to control the amout of reporting Daniel@0: % during training. The argument is used in this function, and it is Daniel@0: % passed to the training functions. To make the function work silently Daniel@0: % set it to 0. Daniel@0: % Daniel@0: % sMap = som_make(D,'tracking',0); Daniel@0: % Daniel@0: % SEE ALSO Daniel@0: % Daniel@0: % som_map_struct Create a map struct. Daniel@0: % som_topol_struct Default values for SOM topology. Daniel@0: % som_train_struct Default values for SOM training parameters. Daniel@0: % som_randinint Random initialization algorithm. Daniel@0: % som_lininit Linear initialization algorithm. Daniel@0: % som_seqtrain Sequential training algorithm. Daniel@0: % som_batchtrain Batch training algorithm. Daniel@0: Daniel@0: % Copyright (c) 1999-2000 by the SOM toolbox programming team. Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 2.0beta juuso 111199 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% check arguments Daniel@0: Daniel@0: % D Daniel@0: if isstruct(D) Daniel@0: data_name = D.name; Daniel@0: comp_names = D.comp_names; Daniel@0: comp_norm = D.comp_norm; Daniel@0: D = D.data; Daniel@0: else Daniel@0: data_name = inputname(1); Daniel@0: sDummy = som_data_struct(D(1,:)); Daniel@0: comp_names = sDummy.comp_names; Daniel@0: comp_norm = sDummy.comp_norm; Daniel@0: end Daniel@0: [dlen dim] = size(D); Daniel@0: Daniel@0: % defaults Daniel@0: mapsize = ''; Daniel@0: sM = som_map_struct(dim); Daniel@0: sTopol = sM.topol; Daniel@0: munits = prod(sTopol.msize); % should be zero Daniel@0: mask = sM.mask; Daniel@0: name = sM.name; Daniel@0: neigh = sM.neigh; Daniel@0: tracking = 1; Daniel@0: algorithm = 'batch'; Daniel@0: initalg = 'lininit'; Daniel@0: training = 'default'; Daniel@0: Daniel@0: % varargin Daniel@0: i=1; Daniel@0: while i<=length(varargin), Daniel@0: argok = 1; Daniel@0: if ischar(varargin{i}), Daniel@0: switch varargin{i}, Daniel@0: % argument IDs Daniel@0: case 'mask', i=i+1; mask = varargin{i}; Daniel@0: case 'munits', i=i+1; munits = varargin{i}; Daniel@0: case 'msize', i=i+1; sTopol.msize = varargin{i}; Daniel@0: munits = prod(sTopol.msize); Daniel@0: case 'mapsize', i=i+1; mapsize = varargin{i}; Daniel@0: case 'name', i=i+1; name = varargin{i}; Daniel@0: case 'comp_names', i=i+1; comp_names = varargin{i}; Daniel@0: case 'lattice', i=i+1; sTopol.lattice = varargin{i}; Daniel@0: case 'shape', i=i+1; sTopol.shape = varargin{i}; Daniel@0: case {'topol','som_topol','sTopol'}, Daniel@0: i=i+1; sTopol = varargin{i}; munits = prod(sTopol.msize); Daniel@0: case 'neigh', i=i+1; neigh = varargin{i}; Daniel@0: case 'tracking', i=i+1; tracking = varargin{i}; Daniel@0: case 'algorithm', i=i+1; algorithm = varargin{i}; Daniel@0: case 'init', i=i+1; initalg = varargin{i}; Daniel@0: case 'training', i=i+1; training = varargin{i}; Daniel@0: % unambiguous values Daniel@0: case {'hexa','rect'}, sTopol.lattice = varargin{i}; Daniel@0: case {'sheet','cyl','toroid'}, sTopol.shape = varargin{i}; Daniel@0: case {'gaussian','cutgauss','ep','bubble'}, neigh = varargin{i}; Daniel@0: case {'seq','batch','sompak'}, algorithm = varargin{i}; Daniel@0: case {'small','normal','big'}, mapsize = varargin{i}; Daniel@0: case {'randinit','lininit'}, initalg = varargin{i}; Daniel@0: case {'short','default','long'}, training = varargin{i}; Daniel@0: otherwise argok=0; Daniel@0: end Daniel@0: elseif isstruct(varargin{i}) & isfield(varargin{i},'type'), Daniel@0: switch varargin{i}(1).type, Daniel@0: case 'som_topol', sTopol = varargin{i}; Daniel@0: otherwise argok=0; Daniel@0: end Daniel@0: else Daniel@0: argok = 0; Daniel@0: end Daniel@0: if ~argok, Daniel@0: disp(['(som_make) Ignoring invalid argument #' num2str(i+1)]); Daniel@0: end Daniel@0: i = i+1; Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% make the map struct Daniel@0: Daniel@0: %% map size Daniel@0: if isempty(sTopol.msize) | ~prod(sTopol.msize), Daniel@0: if tracking>0, fprintf(1,'Determining map size...\n'); end Daniel@0: if ~munits, Daniel@0: sTemp = som_topol_struct('dlen',dlen); Daniel@0: munits = prod(sTemp.msize); Daniel@0: switch mapsize, Daniel@0: case 'small', munits = max(9,ceil(munits/4)); Daniel@0: case 'big', munits = munits*4; Daniel@0: otherwise % nil Daniel@0: end Daniel@0: end Daniel@0: sTemp = som_topol_struct('data',D,'munits',munits); Daniel@0: sTopol.msize = sTemp.msize; Daniel@0: if tracking>0, Daniel@0: fprintf(1,' map size [%d, %d]\n',sTopol.msize(1), sTopol.msize(2)); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % map struct Daniel@0: sMap = som_map_struct(dim,sTopol,neigh,'mask',mask,'name',name, ... Daniel@0: 'comp_names', comp_names, 'comp_norm', comp_norm); Daniel@0: Daniel@0: % function Daniel@0: if strcmp(algorithm,'sompak'), Daniel@0: algorithm = 'seq'; Daniel@0: func = 'sompak'; Daniel@0: else Daniel@0: func = algorithm; Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% initialization Daniel@0: Daniel@0: if tracking>0, fprintf(1,'Initialization...\n'); end Daniel@0: Daniel@0: switch initalg, Daniel@0: case 'randinit', sMap = som_randinit(D, sMap); Daniel@0: case 'lininit', sMap = som_lininit(D, sMap); Daniel@0: end Daniel@0: sMap.trainhist(1) = som_set(sMap.trainhist(1),'data_name',data_name); Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% training Daniel@0: Daniel@0: if tracking>0, fprintf(1,'Training using %s algorithm...\n',algorithm); end Daniel@0: Daniel@0: % rough train Daniel@0: if tracking>0, fprintf(1,'Rough training phase...\n'); end Daniel@0: sTrain = som_train_struct(sMap,'dlen',dlen,'algorithm',algorithm,'phase','rough'); Daniel@0: sTrain = som_set(sTrain,'data_name',data_name); Daniel@0: if isnumeric(training), sTrain.trainlen = training(1); Daniel@0: else Daniel@0: switch training, Daniel@0: case 'short', sTrain.trainlen = max(1,sTrain.trainlen/4); Daniel@0: case 'long', sTrain.trainlen = sTrain.trainlen*4; Daniel@0: end Daniel@0: end Daniel@0: switch func, Daniel@0: case 'seq', sMap = som_seqtrain(sMap,D,sTrain,'tracking',tracking,'mask',mask); Daniel@0: case 'sompak', sMap = som_sompaktrain(sMap,D,sTrain,'tracking',tracking,'mask',mask); Daniel@0: case 'batch', sMap = som_batchtrain(sMap,D,sTrain,'tracking',tracking,'mask',mask); Daniel@0: end Daniel@0: Daniel@0: % finetune Daniel@0: if tracking>0, fprintf(1,'Finetuning phase...\n'); end Daniel@0: sTrain = som_train_struct(sMap,'dlen',dlen,'phase','finetune'); Daniel@0: sTrain = som_set(sTrain,'data_name',data_name,'algorithm',algorithm); Daniel@0: if isnumeric(training), sTrain.trainlen = training(2); Daniel@0: else Daniel@0: switch training, Daniel@0: case 'short', sTrain.trainlen = max(1,sTrain.trainlen/4); Daniel@0: case 'long', sTrain.trainlen = sTrain.trainlen*4; Daniel@0: end Daniel@0: end Daniel@0: switch func, Daniel@0: case 'seq', sMap = som_seqtrain(sMap,D,sTrain,'tracking',tracking,'mask',mask); Daniel@0: case 'sompak', sMap = som_sompaktrain(sMap,D,sTrain,'tracking',tracking,'mask',mask); Daniel@0: case 'batch', sMap = som_batchtrain(sMap,D,sTrain,'tracking',tracking,'mask',mask); Daniel@0: end Daniel@0: Daniel@0: % quality Daniel@0: if tracking>0, Daniel@0: [mqe,tge] = som_quality(sMap,D); Daniel@0: fprintf(1,'Final quantization error: %5.3f\n',mqe) Daniel@0: fprintf(1,'Final topographic error: %5.3f\n',tge) Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: