Daniel@0: function [sMap, sTrain] = som_seqtrain(sMap, D, varargin) Daniel@0: Daniel@0: %SOM_SEQTRAIN Use sequential algorithm to train the Self-Organizing Map. Daniel@0: % Daniel@0: % [sM,sT] = som_seqtrain(sM, D, [[argID,] value, ...]) Daniel@0: % Daniel@0: % sM = som_seqtrain(sM,D); Daniel@0: % sM = som_seqtrain(sM,sD,'alpha_type','power','tracking',3); Daniel@0: % [M,sT] = som_seqtrain(M,D,'ep','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: % [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: % 'mask' (vector) BMU search mask, size dim x 1 Daniel@0: % 'msize' (vector) map size Daniel@0: % 'radius' (vector) neighborhood radiuses, length 1, 2 or trainlen Daniel@0: % 'radius_ini' (scalar) initial training radius Daniel@0: % 'radius_fin' (scalar) final training radius Daniel@0: % 'alpha' (vector) learning rates, length trainlen Daniel@0: % 'alpha_ini' (scalar) initial learning rate Daniel@0: % 'tracking' (scalar) tracking level, 0-3 Daniel@0: % 'trainlen' (scalar) training length Daniel@0: % 'trainlen_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', 'linear' or 'power' Daniel@0: % 'sample_order'*(string) order of samples: 'random' or 'ordered' Daniel@0: % 'neigh' *(string) neighborhood function, 'gaussian', 'cutgauss', Daniel@0: % 'ep' or 'bubble' Daniel@0: % 'topol' *(struct) topology struct Daniel@0: % 'som_topol','sTopo l' = 'topol' Daniel@0: % 'lattice' *(string) map lattice, 'hexa' or 'rect' Daniel@0: % 'shape' *(string) map shape, 'sheet', 'cyl' or 'toroid' Daniel@0: % Daniel@0: % For more help, try 'type som_seqtrain' or check out online documentation. Daniel@0: % See also SOM_MAKE, SOM_BATCHTRAIN, SOM_TRAIN_STRUCT. Daniel@0: Daniel@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Daniel@0: % som_seqtrain Daniel@0: % Daniel@0: % PURPOSE Daniel@0: % Daniel@0: % Trains a Self-Organizing Map using the sequential algorithm. Daniel@0: % Daniel@0: % SYNTAX Daniel@0: % Daniel@0: % sM = som_seqtrain(sM,D); Daniel@0: % sM = som_seqtrain(sM,sD); Daniel@0: % sM = som_seqtrain(...,'argID',value,...); Daniel@0: % sM = som_seqtrain(...,value,...); Daniel@0: % [sM,sT] = som_seqtrain(M,D,...); Daniel@0: % Daniel@0: % DESCRIPTION Daniel@0: % Daniel@0: % Trains the given SOM (sM or M above) with the given training data Daniel@0: % (sD or D) using sequential SOM training algorithm. If no optional Daniel@0: % arguments (argID, value) are given, a default training is done, the Daniel@0: % parameters are obtained from SOM_TRAIN_STRUCT function. Using Daniel@0: % optional arguments the training parameters can be specified. Returns Daniel@0: % the trained and updated SOM and a train struct which contains Daniel@0: % information on the training. Daniel@0: % Daniel@0: % REFERENCES Daniel@0: % Daniel@0: % Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag, Daniel@0: % Berlin, 1995, pp. 78-82. Daniel@0: % Kohonen, T., "Clustering, Taxonomy, and Topological Maps of Daniel@0: % Patterns", International Conference on Pattern Recognition Daniel@0: % (ICPR), 1982, pp. 114-128. Daniel@0: % Kohonen, T., "Self-Organized formation of topologically correct Daniel@0: % feature maps", Biological Cybernetics 43, 1982, pp. 59-69. 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: % 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: % 'mask' (vector) BMU search mask, size dim x 1. Default is Daniel@0: % the one in sM (field '.mask') or a vector of Daniel@0: % ones if only a codebook matrix was given. 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' (vector) neighborhood radius Daniel@0: % length = 1: radius_ini = radius Daniel@0: % length = 2: [radius_ini radius_fin] = radius Daniel@0: % length > 2: the vector given neighborhood Daniel@0: % radius for each step separately Daniel@0: % trainlen = length(radius) Daniel@0: % 'radius_ini' (scalar) initial training radius Daniel@0: % 'radius_fin' (scalar) final training radius Daniel@0: % 'alpha' (vector) learning rate Daniel@0: % length = 1: alpha_ini = alpha Daniel@0: % length > 1: the vector gives learning rate Daniel@0: % for each step separately Daniel@0: % trainlen is set to length(alpha) Daniel@0: % alpha_type is set to 'user defined' Daniel@0: % 'alpha_ini' (scalar) initial learning rate Daniel@0: % 'tracking' (scalar) tracking level: 0, 1 (default), 2 or 3 Daniel@0: % 0 - estimate time Daniel@0: % 1 - track time and quantization error Daniel@0: % 2 - plot quantization error Daniel@0: % 3 - plot quantization error and two first Daniel@0: % components Daniel@0: % 'trainlen' (scalar) training length (see also 'tlen_type') Daniel@0: % 'trainlen_type' *(string) is the trainlen argument given in 'epochs' Daniel@0: % or in 'samples'. Default is 'epochs'. Daniel@0: % 'sample_order'*(string) is the sample order 'random' (which is the Daniel@0: % the default) or 'ordered' in which case Daniel@0: % samples are taken in the order in which they Daniel@0: % appear in the data set 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_train' (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. Other Daniel@0: % possible values is 'cutgauss', 'ep' and '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', 'linear' or 'power' 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: % 'shape' *(string) map shape. Default is the one in sM Daniel@0: % (field sM.topol.shape) or 'sheet' 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_seqtrain(sM,D); Daniel@0: % sM = som_seqtrain(sM,sD); Daniel@0: % Daniel@0: % To change the tracking level, 'tracking' argument is specified: Daniel@0: % sM = som_seqtrain(sM,D,'tracking',3); Daniel@0: % Daniel@0: % The change training parameters, the optional arguments 'train', Daniel@0: % 'neigh','mask','trainlen','radius','radius_ini', 'radius_fin', Daniel@0: % 'alpha', 'alpha_type' and 'alpha_ini' are used. Daniel@0: % sM = som_seqtrain(sM,D,'neigh','cutgauss','trainlen',10,'radius_fin',0); 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','cutgauss'); Daniel@0: % sM = som_seqtrain(sM,D,sTrain); Daniel@0: % Daniel@0: % By default the neighborhood radius goes linearly from radius_ini to Daniel@0: % radius_fin. If you want to change this, you can use the 'radius' argument Daniel@0: % to specify the neighborhood radius for each step separately: Daniel@0: % sM = som_seqtrain(sM,D,'radius',[5 3 1 1 1 1 0.5 0.5 0.5]); Daniel@0: % Daniel@0: % By default the learning rate (alpha) goes from the alpha_ini to 0 Daniel@0: % along the function defined by alpha_type. If you want to change this, Daniel@0: % you can use the 'alpha' argument to specify the learning rate Daniel@0: % for each step separately: Daniel@0: % alpha = 0.2*(1 - log([1:100])); Daniel@0: % sM = som_seqtrain(sM,D,'alpha',alpha); 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_seqtrain(M,D,'msize',[20 10],'lattice','hexa','shape','cyl'); Daniel@0: % Daniel@0: % M = som_seqtrain(M,D,'msize',[20 10],'hexa','cyl'); Daniel@0: % Daniel@0: % sT= som_set('som_topol','msize',[20 10],'lattice','hexa','shape','cyl'); Daniel@0: % M = som_seqtrain(M,D,sT); Daniel@0: % Daniel@0: % M = reshape(M,[20 10 dim]); Daniel@0: % M = som_seqtrain(M,D,'hexa','cyl'); Daniel@0: % Daniel@0: % The som_seqtrain 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_seqtrain(M,D,'msize',[20 10]); Daniel@0: % Daniel@0: % [sM,sTrain] = som_seqtrain(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_batchtrain Train SOM with batch algorithm. Daniel@0: % som_train_struct Determine default training parameters. Daniel@0: Daniel@0: % Copyright (c) 1997-2000 by the SOM toolbox programming team. Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 1.0beta juuso 220997 Daniel@0: % Version 2.0beta juuso 101199 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: 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 dim ~= ddim, error('Map and data input space dimensions disagree.'); end Daniel@0: Daniel@0: % varargin Daniel@0: sTrain = som_set('som_train','algorithm','seq','neigh', ... Daniel@0: sMap.neigh,'mask',sMap.mask,'data_name',data_name); Daniel@0: radius = []; Daniel@0: alpha = []; Daniel@0: tracking = 1; Daniel@0: sample_order_type = 'random'; Daniel@0: tlen_type = 'epochs'; 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 'shape', i=i+1; sTopol.shape = varargin{i}; Daniel@0: case 'mask', i=i+1; sTrain.mask = 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 'trainlen_type', i=i+1; tlen_type = varargin{i}; Daniel@0: case 'tracking', i=i+1; tracking = varargin{i}; Daniel@0: case 'sample_order', i=i+1; sample_order_type = varargin{i}; Daniel@0: case 'radius_ini', i=i+1; sTrain.radius_ini = varargin{i}; Daniel@0: case 'radius_fin', i=i+1; sTrain.radius_fin = varargin{i}; Daniel@0: case 'radius', Daniel@0: i=i+1; Daniel@0: l = length(varargin{i}); Daniel@0: if l==1, Daniel@0: sTrain.radius_ini = varargin{i}; Daniel@0: else Daniel@0: sTrain.radius_ini = varargin{i}(1); Daniel@0: sTrain.radius_fin = varargin{i}(end); Daniel@0: if l>2, radius = varargin{i}; tlen_type = 'samples'; end Daniel@0: end 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', Daniel@0: i=i+1; Daniel@0: sTrain.alpha_ini = varargin{i}(1); Daniel@0: if length(varargin{i})>1, Daniel@0: alpha = varargin{i}; tlen_type = 'samples'; Daniel@0: sTrain.alpha_type = 'user defined'; Daniel@0: end 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','power'}, sTrain.alpha_type = varargin{i}; 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'}, sTrain.neigh = varargin{i}; Daniel@0: case {'epochs','samples'}, tlen_type = varargin{i}; Daniel@0: case {'random', 'ordered'}, sample_order_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_seqtrain) Ignoring invalid argument #' num2str(i+2)]); Daniel@0: end Daniel@0: i = i+1; Daniel@0: end Daniel@0: Daniel@0: % training length Daniel@0: if ~isempty(radius) | ~isempty(alpha), Daniel@0: lr = length(radius); Daniel@0: la = length(alpha); Daniel@0: if lr>2 | la>1, Daniel@0: tlen_type = 'samples'; Daniel@0: if lr> 2 & la<=1, sTrain.trainlen = lr; Daniel@0: elseif lr<=2 & la> 1, sTrain.trainlen = la; Daniel@0: elseif lr==la, sTrain.trainlen = la; Daniel@0: else Daniel@0: error('Mismatch between radius and learning rate vector lengths.') Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: if strcmp(tlen_type,'samples'), sTrain.trainlen = sTrain.trainlen/dlen; 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: sTrain = som_train_struct(sTrain,sMap,'dlen',dlen); Daniel@0: if isempty(sTrain.mask), sTrain.mask = ones(dim,1); end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% initialize Daniel@0: Daniel@0: M = sMap.codebook; Daniel@0: mask = sTrain.mask; Daniel@0: trainlen = sTrain.trainlen*dlen; Daniel@0: Daniel@0: % neighborhood radius Daniel@0: if length(radius)>2, Daniel@0: radius_type = 'user defined'; Daniel@0: else Daniel@0: radius = [sTrain.radius_ini sTrain.radius_fin]; Daniel@0: rini = radius(1); Daniel@0: rstep = (radius(end)-radius(1))/(trainlen-1); Daniel@0: radius_type = 'linear'; Daniel@0: end Daniel@0: Daniel@0: % learning rate Daniel@0: if length(alpha)>1, Daniel@0: sTrain.alpha_type ='user defined'; Daniel@0: if length(alpha) ~= trainlen, Daniel@0: error('Trainlen and length of neighborhood radius vector do not match.') Daniel@0: end Daniel@0: if any(isnan(alpha)), Daniel@0: error('NaN is an illegal learning rate.') Daniel@0: end Daniel@0: else Daniel@0: if isempty(alpha), alpha = sTrain.alpha_ini; end Daniel@0: if strcmp(sTrain.alpha_type,'inv'), Daniel@0: % alpha(t) = a / (t+b), where a and b are chosen suitably Daniel@0: % below, they are chosen so that alpha_fin = alpha_ini/100 Daniel@0: b = (trainlen - 1) / (100 - 1); Daniel@0: a = b * alpha; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % initialize random number generator Daniel@0: rand('state',sum(100*clock)); Daniel@0: Daniel@0: % distance between map units in the output space Daniel@0: % Since in the case of gaussian and ep neighborhood functions, the Daniel@0: % equations utilize squares of the unit distances and in bubble case Daniel@0: % it doesn't matter which is used, the unitdistances and neighborhood Daniel@0: % radiuses are squared. Daniel@0: Ud = som_unit_dists(sTopol).^2; Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% Action Daniel@0: Daniel@0: update_step = 100; Daniel@0: mu_x_1 = ones(munits,1); Daniel@0: samples = ones(update_step,1); Daniel@0: r = samples; Daniel@0: alfa = samples; Daniel@0: Daniel@0: qe = 0; Daniel@0: start = clock; Daniel@0: if tracking > 0, % initialize tracking Daniel@0: track_table = zeros(update_step,1); Daniel@0: qe = zeros(floor(trainlen/update_step),1); Daniel@0: end Daniel@0: Daniel@0: for t = 1:trainlen, Daniel@0: Daniel@0: % Every update_step, new values for sample indeces, neighborhood Daniel@0: % radius and learning rate are calculated. This could be done Daniel@0: % every step, but this way it is more efficient. Or this could Daniel@0: % be done all at once outside the loop, but it would require much Daniel@0: % more memory. Daniel@0: ind = rem(t,update_step); if ind==0, ind = update_step; end Daniel@0: if ind==1, Daniel@0: steps = [t:min(trainlen,t+update_step-1)]; Daniel@0: % sample order Daniel@0: switch sample_order_type, Daniel@0: case 'ordered', samples = rem(steps,dlen)+1; Daniel@0: case 'random', samples = ceil(dlen*rand(update_step,1)+eps); Daniel@0: end Daniel@0: Daniel@0: % neighborhood radius Daniel@0: switch radius_type, Daniel@0: case 'linear', r = rini+(steps-1)*rstep; Daniel@0: case 'user defined', r = radius(steps); Daniel@0: end Daniel@0: r=r.^2; % squared radius (see notes about Ud above) Daniel@0: r(r==0) = eps; % zero radius might cause div-by-zero error Daniel@0: Daniel@0: % learning rate Daniel@0: switch sTrain.alpha_type, Daniel@0: case 'linear', alfa = (1-steps/trainlen)*alpha; Daniel@0: case 'inv', alfa = a ./ (b + steps-1); Daniel@0: case 'power', alfa = alpha * (0.005/alpha).^((steps-1)/trainlen); Daniel@0: case 'user defined', alfa = alpha(steps); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % find BMU Daniel@0: x = D(samples(ind),:); % pick one sample vector Daniel@0: known = ~isnan(x); % its known components Daniel@0: Dx = M(:,known) - x(mu_x_1,known); % each map unit minus the vector Daniel@0: [qerr bmu] = min((Dx.^2)*mask(known)); % minimum distance(^2) and the BMU Daniel@0: Daniel@0: % tracking Daniel@0: if tracking>0, Daniel@0: track_table(ind) = sqrt(qerr); Daniel@0: if ind==update_step, Daniel@0: n = ceil(t/update_step); Daniel@0: qe(n) = mean(track_table); Daniel@0: trackplot(M,D,tracking,start,n,qe); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % neighborhood & learning rate Daniel@0: % notice that the elements Ud and radius have been squared! Daniel@0: % (see notes about Ud above) Daniel@0: switch sTrain.neigh, Daniel@0: case 'bubble', h = (Ud(:,bmu)<=r(ind)); Daniel@0: case 'gaussian', h = exp(-Ud(:,bmu)/(2*r(ind))); Daniel@0: case 'cutgauss', h = exp(-Ud(:,bmu)/(2*r(ind))) .* (Ud(:,bmu)<=r(ind)); Daniel@0: case 'ep', h = (1-Ud(:,bmu)/r(ind)) .* (Ud(:,bmu)<=r(ind)); Daniel@0: end Daniel@0: h = h*alfa(ind); Daniel@0: Daniel@0: % update M Daniel@0: M(:,known) = M(:,known) - h(:,ones(sum(known),1)).*Dx; Daniel@0: Daniel@0: end; % for t = 1:trainlen Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% Build / clean up the return arguments Daniel@0: Daniel@0: if tracking, fprintf(1,'\n'); end 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: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% subfunctions Daniel@0: Daniel@0: %%%%%%%% Daniel@0: function [] = trackplot(M,D,tracking,start,n,qe) Daniel@0: Daniel@0: l = length(qe); Daniel@0: elap_t = etime(clock,start); Daniel@0: tot_t = elap_t*l/n; Daniel@0: fprintf(1,'\rTraining: %3.0f/ %3.0f s',elap_t,tot_t) Daniel@0: switch tracking Daniel@0: case 1, Daniel@0: case 2, Daniel@0: plot(1:n,qe(1:n),(n+1):l,qe((n+1):l)) Daniel@0: title('Quantization errors for latest samples') Daniel@0: drawnow Daniel@0: otherwise, Daniel@0: subplot(2,1,1), plot(1:n,qe(1:n),(n+1):l,qe((n+1):l)) Daniel@0: title('Quantization error for latest samples'); Daniel@0: subplot(2,1,2), plot(M(:,1),M(:,2),'ro',D(:,1),D(:,2),'b.'); Daniel@0: title('First two components of map units (o) and data vectors (+)'); Daniel@0: drawnow Daniel@0: end Daniel@0: % end of trackplot Daniel@0: