Daniel@0: function [sMap, sTrain] = som_sompaktrain(sMap, D, varargin) Daniel@0: Daniel@0: %SOM_SOMPAKTRAIN Use SOM_PAK to train the Self-Organizing Map. Daniel@0: % Daniel@0: % [sM,sT] = som_sompaktrain(sM, D, [[argID,] value, ...]) Daniel@0: % Daniel@0: % sM = som_sompaktrain(sM,D); Daniel@0: % sM = som_sompaktrain(sM,sD,'alpha_type','inv'); Daniel@0: % [M,sT] = som_sompaktrain(M,D,'bubble','trainlen',10,'inv','hexa'); Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % sM (struct) map struct, the trained and updated map is returned Daniel@0: % (matrix) codebook matrix of a self-organizing map Daniel@0: % size munits x dim or msize(1) x ... x msize(k) x dim Daniel@0: % The trained map codebook is returned. Daniel@0: % D (struct) training data; data struct Daniel@0: % (matrix) training data, size dlen x dim Daniel@0: % (string) name of data file 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: % sT (struct) learning parameters used during the training Daniel@0: % Daniel@0: % Here are the valid argument IDs and corresponding values. The values which Daniel@0: % are unambiguous (marked with '*') can be given without the preceeding argID. Daniel@0: % 'msize' (vector) map size Daniel@0: % 'radius_ini' (scalar) neighborhood radius Daniel@0: % 'radius' = 'radius_ini' Daniel@0: % 'alpha_ini' (scalar) initial learning rate Daniel@0: % 'alpha' = 'alpha_ini' Daniel@0: % 'trainlen' (scalar) training length Daniel@0: % 'seed' (scalar) seed for random number generator Daniel@0: % 'snapfile' (string) base name for snapshot files Daniel@0: % 'snapinterval' (scalar) snapshot interval Daniel@0: % 'tlen_type' *(string) is the given trainlen 'samples' or 'epochs' Daniel@0: % 'train' *(struct) train struct, parameters for training Daniel@0: % 'sTrain','som_train' = 'train' Daniel@0: % 'alpha_type' *(string) learning rate function, 'inv' or 'linear' Daniel@0: % 'neigh' *(string) neighborhood function, 'gaussian' or 'bubble' Daniel@0: % 'topol' *(struct) topology struct Daniel@0: % 'som_topol','sTopol' = 'topol' Daniel@0: % 'lattice' *(string) map lattice, 'hexa' or 'rect' Daniel@0: % Daniel@0: % For more help, try 'type som_sompaktrain' or check out online documentation. Daniel@0: % See also SOM_MAKE, SOM_SEQTRAIN, SOM_BATCHTRAIN, SOM_TRAIN_STRUCT. Daniel@0: Daniel@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Daniel@0: % som_sompaktrain Daniel@0: % Daniel@0: % PURPOSE Daniel@0: % Daniel@0: % Use SOM_PAK to train the Self-Organizing Map. Daniel@0: % Daniel@0: % SYNTAX Daniel@0: % Daniel@0: % sM = som_sompaktrain(sM,D); Daniel@0: % sM = som_sompaktrain(sM,sD); Daniel@0: % sM = som_sompaktrain(...,'argID',value,...); Daniel@0: % sM = som_sompaktrain(...,value,...); Daniel@0: % [sM,sT] = som_sompaktrain(M,D,...); Daniel@0: % Daniel@0: % DESCRIPTION Daniel@0: % Daniel@0: % Trains the given SOM (sM or M above) with the given training data (sD or Daniel@0: % D) using SOM_PAK. If no optional arguments (argID, value) are Daniel@0: % given, a default training is done, the parameters are obtained from Daniel@0: % SOM_TRAIN_STRUCT function. Using optional arguments the training Daniel@0: % parameters can be specified. Returns the trained and updated SOM and a Daniel@0: % train struct which contains information on the training. Daniel@0: % Daniel@0: % Notice that the SOM_PAK program 'vsom' must be in the search path of your Daniel@0: % shell. Alternatively, you can set a variable 'SOM_PAKDIR' in the Matlab Daniel@0: % workspace to tell the som_sompaktrain where to find the 'vsom' program. Daniel@0: % Daniel@0: % Notice also that many of the training parameters are much more limited in Daniel@0: % values than when using SOM Toolbox function for training: Daniel@0: % - the map shape is always 'sheet' Daniel@0: % - only initial value for neighborhood radius can be given Daniel@0: % - neighborhood function can only be 'bubble' or 'gaussian' Daniel@0: % - only initial value for learning rate can be given Daniel@0: % - learning rate can only be 'linear' or 'inv' Daniel@0: % - mask cannot be used: all variables are always used in BMU search Daniel@0: % Any parameters not confirming to these restrictions will be converted Daniel@0: % so that they do before training. On the other hand, there are some Daniel@0: % additional options that are not present in the SOM Toolbox: Daniel@0: % - random seed Daniel@0: % - snapshot file and interval Daniel@0: % Daniel@0: % REQUIRED INPUT ARGUMENTS Daniel@0: % Daniel@0: % sM The map to be trained. Daniel@0: % (struct) map struct Daniel@0: % (matrix) codebook matrix (field .data of map struct) Daniel@0: % Size is either [munits dim], in which case the map grid Daniel@0: % dimensions (msize) should be specified with optional arguments, Daniel@0: % or [msize(1) ... msize(k) dim] in which case the map Daniel@0: % grid dimensions are taken from the size of the matrix. Daniel@0: % Lattice, by default, is 'rect' and shape 'sheet'. Daniel@0: % D Training data. Daniel@0: % (struct) data struct Daniel@0: % (matrix) data matrix, size [dlen dim] Daniel@0: % (string) name of data file 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: % The optional arguments can be given as 'argID',value -pairs. If an Daniel@0: % argument is given value multiple times, the last one is Daniel@0: % used. The valid IDs and corresponding values are listed below. The values Daniel@0: % which are unambiguous (marked with '*') can be given without the Daniel@0: % preceeding argID. Daniel@0: % Daniel@0: % 'msize' (vector) map grid dimensions. Default is the one Daniel@0: % in sM (field sM.topol.msize) or Daniel@0: % 'si = size(sM); msize = si(1:end-1);' Daniel@0: % if only a codebook matrix was given. Daniel@0: % 'radius_ini' (scalar) initial neighborhood radius Daniel@0: % 'radius' (scalar) = 'radius_ini' Daniel@0: % 'alpha_ini' (vector) initial learning rate Daniel@0: % 'alpha' (scalar) = 'alpha_ini' Daniel@0: % 'trainlen' (scalar) training length (see also 'tlen_type') Daniel@0: % 'seed' (scalar) seed for random number generator Daniel@0: % 'snapfile' (string) base name for snapshot files Daniel@0: % 'snapinterval' (scalar) snapshot interval Daniel@0: % 'tlen_type' *(string) is the trainlen argument given in 'epochs' or Daniel@0: % in 'samples'. Default is 'epochs'. Daniel@0: % 'train' *(struct) train struct, parameters for training. Daniel@0: % Default parameters, unless specified, Daniel@0: % are acquired using SOM_TRAIN_STRUCT (this Daniel@0: % also applies for 'trainlen', 'alpha_type', Daniel@0: % 'alpha_ini', 'radius_ini' and 'radius_fin'). Daniel@0: % 'sTrain', 'som_topol' (struct) = 'train' Daniel@0: % 'neigh' *(string) The used neighborhood function. Default is Daniel@0: % the one in sM (field '.neigh') or 'gaussian' Daniel@0: % if only a codebook matrix was given. The other Daniel@0: % possible value is 'bubble'. Daniel@0: % 'topol' *(struct) topology of the map. Default is the one Daniel@0: % in sM (field '.topol'). Daniel@0: % 'sTopol', 'som_topol' (struct) = 'topol' Daniel@0: % 'alpha_type' *(string) learning rate function, 'inv' or 'linear' Daniel@0: % 'lattice' *(string) map lattice. Default is the one in sM Daniel@0: % (field sM.topol.lattice) or 'rect' Daniel@0: % if only a codebook matrix was given. Daniel@0: % Daniel@0: % OUTPUT ARGUMENTS Daniel@0: % Daniel@0: % sM the trained map Daniel@0: % (struct) if a map struct was given as input argument, a Daniel@0: % map struct is also returned. The current training Daniel@0: % is added to the training history (sM.trainhist). Daniel@0: % The 'neigh' and 'mask' fields of the map struct Daniel@0: % are updated to match those of the training. Daniel@0: % (matrix) if a matrix was given as input argument, a matrix Daniel@0: % is also returned with the same size as the input Daniel@0: % argument. Daniel@0: % sT (struct) train struct; information of the accomplished training Daniel@0: % Daniel@0: % EXAMPLES Daniel@0: % Daniel@0: % Simplest case: Daniel@0: % sM = som_sompaktrain(sM,D); Daniel@0: % sM = som_sompaktrain(sM,sD); Daniel@0: % Daniel@0: % The change training parameters, the optional arguments 'train', Daniel@0: % 'neigh','mask','trainlen','radius','radius_ini', 'alpha', Daniel@0: % 'alpha_type' and 'alpha_ini' are used. Daniel@0: % sM = som_sompaktrain(sM,D,'bubble','trainlen',10,'radius_ini',3); Daniel@0: % Daniel@0: % Another way to specify training parameters is to create a train struct: Daniel@0: % sTrain = som_train_struct(sM,'dlen',size(D,1),'algorithm','seq'); Daniel@0: % sTrain = som_set(sTrain,'neigh','gaussian'); Daniel@0: % sM = som_sompaktrain(sM,D,sTrain); Daniel@0: % Daniel@0: % You don't necessarily have to use the map struct, but you can operate Daniel@0: % directly with codebook matrices. However, in this case you have to Daniel@0: % specify the topology of the map in the optional arguments. The Daniel@0: % following commads are identical (M is originally a 200 x dim sized matrix): Daniel@0: % M = som_sompaktrain(M,D,'msize',[20 10],'lattice','hexa'); Daniel@0: % Daniel@0: % M = som_sompaktrain(M,D,'msize',[20 10],'hexa'); Daniel@0: % Daniel@0: % sT= som_set('som_topol','msize',[20 10],'lattice','hexa'); Daniel@0: % M = som_sompaktrain(M,D,sT); Daniel@0: % Daniel@0: % M = reshape(M,[20 10 dim]); Daniel@0: % M = som_sompaktrain(M,D,'hexa'); Daniel@0: % Daniel@0: % The som_sompaktrain also returns a train struct with information on the Daniel@0: % accomplished training. This is the same one as is added to the end of the Daniel@0: % trainhist field of map struct, in case a map struct is given. Daniel@0: % [M,sTrain] = som_sompaktrain(M,D,'msize',[20 10]); Daniel@0: % Daniel@0: % [sM,sTrain] = som_sompaktrain(sM,D); % sM.trainhist(end)==sTrain Daniel@0: % Daniel@0: % SEE ALSO Daniel@0: % Daniel@0: % som_make Initialize and train a SOM using default parameters. Daniel@0: % som_seqtrain Train SOM with sequential algorithm. Daniel@0: % som_batchtrain Train SOM with batch algorithm. Daniel@0: % som_train_struct Determine default training parameters. 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 151199 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% Check arguments Daniel@0: Daniel@0: error(nargchk(2, Inf, nargin)); % check the number of input arguments Daniel@0: Daniel@0: % map Daniel@0: struct_mode = isstruct(sMap); Daniel@0: if struct_mode, Daniel@0: sTopol = sMap.topol; Daniel@0: else Daniel@0: orig_size = size(sMap); Daniel@0: if ndims(sMap) > 2, Daniel@0: si = size(sMap); dim = si(end); msize = si(1:end-1); Daniel@0: M = reshape(sMap,[prod(msize) dim]); Daniel@0: else Daniel@0: msize = [orig_size(1) 1]; Daniel@0: dim = orig_size(2); Daniel@0: end Daniel@0: sMap = som_map_struct(dim,'msize',msize); Daniel@0: sTopol = sMap.topol; Daniel@0: end Daniel@0: [munits dim] = size(sMap.codebook); Daniel@0: Daniel@0: % data Daniel@0: givendatafile = ''; Daniel@0: if ischar(D), Daniel@0: data_name = D; Daniel@0: givendatafile = D; Daniel@0: D = []; Daniel@0: dlen = NaN; Daniel@0: else Daniel@0: if isstruct(D), Daniel@0: data_name = D.name; Daniel@0: D = D.data; Daniel@0: else Daniel@0: data_name = inputname(2); Daniel@0: end Daniel@0: D = D(find(sum(isnan(D),2) < dim),:); % remove empty vectors from the data Daniel@0: [dlen ddim] = size(D); % check input dimension Daniel@0: if ddim ~= dim, error('Map and data dimensions must agree.'); end Daniel@0: end Daniel@0: Daniel@0: % varargin Daniel@0: sTrain = som_set('som_train','algorithm','seq',... Daniel@0: 'neigh',sMap.neigh,... Daniel@0: 'mask',ones(dim,1),... Daniel@0: 'data_name',data_name); Daniel@0: tlen_type = 'epochs'; Daniel@0: random_seed = 0; Daniel@0: snapshotname = ''; Daniel@0: snapshotinterval = 0; Daniel@0: 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 'msize', i=i+1; sTopol.msize = varargin{i}; Daniel@0: case 'lattice', i=i+1; sTopol.lattice = varargin{i}; Daniel@0: case 'neigh', i=i+1; sTrain.neigh = varargin{i}; Daniel@0: case 'trainlen', i=i+1; sTrain.trainlen = varargin{i}; Daniel@0: case 'tlen_type', i=i+1; tlen_type = varargin{i}; Daniel@0: case 'radius_ini', i=i+1; sTrain.radius_ini = varargin{i}; Daniel@0: case 'radius', i=i+1; sTrain.radius_ini = varargin{i}(1); Daniel@0: case 'alpha_type', i=i+1; sTrain.alpha_type = varargin{i}; Daniel@0: case 'alpha_ini', i=i+1; sTrain.alpha_ini = varargin{i}; Daniel@0: case 'alpha', i=i+1; sTrain.alpha_ini = varargin{i}(1); Daniel@0: case 'seed', i=i+1; random_seed = varargin{i}; Daniel@0: case 'snapshotname',i=i+1; snapshotname = varargin{i}; Daniel@0: case 'snapshotinterval',i=i+1; snapshotinterval = varargin{i}; Daniel@0: case {'sTrain','train','som_train'}, i=i+1; sTrain = varargin{i}; Daniel@0: case {'topol','sTopol','som_topol'}, Daniel@0: i=i+1; Daniel@0: sTopol = varargin{i}; Daniel@0: if prod(sTopol.msize) ~= munits, Daniel@0: error('Given map grid size does not match the codebook size.'); Daniel@0: end Daniel@0: % unambiguous values Daniel@0: case {'inv','linear'}, sTrain.alpha_type = varargin{i}; Daniel@0: case {'hexa','rect'}, sTopol.lattice = varargin{i}; Daniel@0: case {'gaussian','bubble'}, sTrain.neigh = varargin{i}; Daniel@0: case {'epochs','samples'}, tlen_type = 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', Daniel@0: sTopol = varargin{i}; Daniel@0: if prod(sTopol.msize) ~= munits, Daniel@0: error('Given map grid size does not match the codebook size.'); Daniel@0: end Daniel@0: case 'som_train', sTrain = 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_sompaktrain) Ignoring invalid argument #' num2str(i+2)]); Daniel@0: end Daniel@0: i = i+1; Daniel@0: end Daniel@0: Daniel@0: % check topology Daniel@0: if struct_mode, Daniel@0: if ~strcmp(sTopol.lattice,sMap.topol.lattice) | ... Daniel@0: ~strcmp(sTopol.shape,sMap.topol.shape) | ... Daniel@0: any(sTopol.msize ~= sMap.topol.msize), Daniel@0: warning('Changing the original map topology.'); Daniel@0: end Daniel@0: end Daniel@0: sMap.topol = sTopol; Daniel@0: Daniel@0: % complement the training struct Daniel@0: if ~isnan(dlen), Daniel@0: sTrain = som_train_struct(sTrain,sMap,'dlen',dlen); Daniel@0: else Daniel@0: sTrain = som_train_struct(sTrain,sMap); Daniel@0: end Daniel@0: if isempty(sTrain.mask), sTrain.mask = ones(dim,1); end Daniel@0: Daniel@0: % training length Daniel@0: if strcmp(tlen_type,'epochs'), Daniel@0: if isnan(dlen), Daniel@0: error('Training length given as epochs, but data length is not known.\n'); Daniel@0: else Daniel@0: rlen = sTrain.trainlen*dlen; Daniel@0: end Daniel@0: else Daniel@0: rlen = sTrain.trainlen; Daniel@0: sTrain.trainlen = sTrain.trainlen/dlen; Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% check arguments Daniel@0: Daniel@0: % mask Daniel@0: if any(sTrain.mask~=1), Daniel@0: sTrain.mask = ones(dim,1); Daniel@0: fprintf(1,'Ignoring given mask.\n'); Daniel@0: end Daniel@0: Daniel@0: % learning rate Daniel@0: if strcmp(sTrain.alpha_type,'power'), Daniel@0: sTrain.alpha_type = 'inv'; Daniel@0: fprintf(1,'Using ''inv'' learning rate type instead of ''power''\n'); Daniel@0: end Daniel@0: Daniel@0: % neighborhood Daniel@0: if any(strcmp(sTrain.neigh,{'cutgauss','ep'})), Daniel@0: fprintf(1,'Using ''gaussian'' neighborhood function instead of %s.\n',sTrain.neigh); Daniel@0: sTrain.neigh = 'gaussian'; Daniel@0: end Daniel@0: Daniel@0: % map shape Daniel@0: if ~strcmp(sMap.topol.shape,'sheet'), Daniel@0: fprintf(1,'Using ''sheet'' map shape of %s.\n',sMap.topol.shape); Daniel@0: sMap.topol.shape = 'sheet'; Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% Action Daniel@0: Daniel@0: % write files Daniel@0: if ~isempty(givendatafile), Daniel@0: temp_din = givendatafile; Daniel@0: else Daniel@0: temp_din = tempname; Daniel@0: som_write_data(D, temp_din, 'x') Daniel@0: end Daniel@0: temp_cin = tempname; Daniel@0: som_write_cod(sMap, temp_cin) Daniel@0: temp_cout = tempname; Daniel@0: Daniel@0: % check if the environment variable 'SOM_PAKDIR' has been defined Daniel@0: if any(strcmp('SOM_PAKDIR', evalin('base', 'who'))) Daniel@0: som_pak_dir = evalin('base', 'SOM_PAKDIR'); Daniel@0: else Daniel@0: som_pak_dir = ''; Daniel@0: end Daniel@0: if ~isempty(som_pak_dir) & ~strncmp(som_pak_dir(end), '/', 1) Daniel@0: som_pak_dir(end + 1) = '/'; Daniel@0: end Daniel@0: Daniel@0: aini = sTrain.alpha_ini; Daniel@0: atype = sTrain.alpha_type; Daniel@0: if strcmp(atype,'inv'), atype = 'inverse_t'; end Daniel@0: rad = sTrain.radius_ini; Daniel@0: str = [som_pak_dir 'vsom ' ... Daniel@0: sprintf('-cin %s -din %s -cout %s', temp_cin, temp_din, temp_cout) ... Daniel@0: sprintf(' -rlen %d -alpha %g -alpha_type %s', rlen, aini, atype) ... Daniel@0: sprintf(' -radius %g -rand %g ',rad,random_seed)]; Daniel@0: if ~isempty(snapshotname) & snapinterval>0, Daniel@0: str = [str, sprintf(' -snapfile %s -snapinterval %d',snapshotname,snapshotinterval)]; Daniel@0: end Daniel@0: Daniel@0: fprintf(1,'Execute: %s\n',str); Daniel@0: if isunix, Daniel@0: [status,w] = unix(str); Daniel@0: if status, fprintf(1,'Execution failed.\n'); end Daniel@0: if ~isempty(w), fprintf(1,'%s\n',w); end Daniel@0: else Daniel@0: [status,w] = dos(str); Daniel@0: if status, fprintf(1,'Execution failed.\n'); end Daniel@0: if ~isempty(w), fprintf(1,'%s\n',w); end Daniel@0: end Daniel@0: Daniel@0: sMap_temp = som_read_cod(temp_cout); Daniel@0: M = sMap_temp.codebook; Daniel@0: Daniel@0: if isunix Daniel@0: unix(['/bin/rm -f ' temp_din ' ' temp_cin ' ' temp_cout]); Daniel@0: else Daniel@0: dos(['del ' temp_din ' ' temp_cin ' ' temp_cout]); Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% Build / clean up the return arguments Daniel@0: Daniel@0: % update structures Daniel@0: sTrain = som_set(sTrain,'time',datestr(now,0)); Daniel@0: if struct_mode, Daniel@0: sMap = som_set(sMap,'codebook',M,'mask',sTrain.mask,'neigh',sTrain.neigh); Daniel@0: tl = length(sMap.trainhist); Daniel@0: sMap.trainhist(tl+1) = sTrain; Daniel@0: else Daniel@0: sMap = reshape(M,orig_size); Daniel@0: end Daniel@0: Daniel@0: return; Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: