Daniel@0: function [sM,sTrain] = som_prototrain(sM, D) Daniel@0: Daniel@0: %SOM_PROTOTRAIN Use sequential algorithm to train the Self-Organizing Map. Daniel@0: % Daniel@0: % [sM,sT] = som_prototrain(sM, D) Daniel@0: % Daniel@0: % sM = som_prototrain(sM,D); Daniel@0: % Daniel@0: % Input and output arguments: 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: % Daniel@0: % This function is otherwise just like SOM_SEQTRAIN except that Daniel@0: % the implementation of the sequential training algorithm is very Daniel@0: % straightforward (and slower). This should make it easy for you Daniel@0: % to modify the algorithm, if you want to. Daniel@0: % Daniel@0: % For help on input and output parameters, try Daniel@0: % 'type som_prototrain' or check out the help for SOM_SEQTRAIN. Daniel@0: % See also SOM_SEQTRAIN, SOM_BATCHTRAIN. Daniel@0: Daniel@0: % Contributed to SOM Toolbox vs2, February 2nd, 2000 by Juha Vesanto Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 2.0beta juuso 080200 130300 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% Check input arguments Daniel@0: Daniel@0: % map Daniel@0: struct_mode = isstruct(sM); Daniel@0: if struct_mode, Daniel@0: M = sM.codebook; Daniel@0: sTopol = sM.topol; Daniel@0: mask = sM.mask; Daniel@0: msize = sTopol.msize; Daniel@0: neigh = sM.neigh; Daniel@0: else Daniel@0: M = sM; orig_size = size(M); Daniel@0: if ndims(sM) > 2, Daniel@0: si = size(sM); dim = si(end); msize = si(1:end-1); Daniel@0: M = reshape(sM,[prod(msize) dim]); Daniel@0: else Daniel@0: msize = [orig_size(1) 1]; dim = orig_size(2); Daniel@0: end Daniel@0: sM = som_map_struct(dim,'msize',msize); sTopol = sM.topol; Daniel@0: mask = ones(dim,1); Daniel@0: neigh = 'gaussian'; Daniel@0: end Daniel@0: [munits dim] = size(M); Daniel@0: Daniel@0: % data Daniel@0: if isstruct(D), data_name = D.name; D = D.data; Daniel@0: else 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: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% initialize (these are default values, change as you will) Daniel@0: Daniel@0: % training length Daniel@0: trainlen = 20*dlen; % 20 epochs by default Daniel@0: Daniel@0: % neighborhood radius Daniel@0: radius_type = 'linear'; Daniel@0: rini = max(msize)/2; Daniel@0: rfin = 1; Daniel@0: Daniel@0: % learning rate Daniel@0: alpha_type = 'inv'; Daniel@0: alpha_ini = 0.2; Daniel@0: Daniel@0: % initialize random number generator Daniel@0: rand('state',sum(100*clock)); Daniel@0: Daniel@0: % tracking Daniel@0: start = clock; trackstep = 100; Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% Action Daniel@0: Daniel@0: Ud = som_unit_dists(sTopol); % distance between map units on the grid Daniel@0: mu_x_1 = ones(munits,1); % this is used pretty often Daniel@0: Daniel@0: for t = 1:trainlen, Daniel@0: Daniel@0: %% find BMU Daniel@0: ind = ceil(dlen*rand(1)+eps); % select one vector Daniel@0: x = D(ind,:); % pick it up 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: dist2 = (Dx.^2)*mask(known); % squared distances Daniel@0: [qerr bmu] = min(dist2); % find BMU Daniel@0: Daniel@0: %% neighborhood Daniel@0: switch radius_type, % radius Daniel@0: case 'linear', r = rini+(rfin-rini)*(t-1)/(trainlen-1); Daniel@0: end Daniel@0: if ~r, r=eps; end % zero neighborhood radius may cause div-by-zero error Daniel@0: switch neigh, % neighborhood function Daniel@0: case 'bubble', h = (Ud(:,bmu) <= r); Daniel@0: case 'gaussian', h = exp(-(Ud(:,bmu).^2)/(2*r*r)); Daniel@0: case 'cutgauss', h = exp(-(Ud(:,bmu).^2)/(2*r*r)) .* (Ud(:,bmu) <= r); Daniel@0: case 'ep', h = (1 - (Ud(:,bmu).^2)/(r*r)) .* (Ud(:,bmu) <= r); Daniel@0: end Daniel@0: Daniel@0: %% learning rate Daniel@0: switch alpha_type, Daniel@0: case 'linear', a = (1-t/trainlen)*alpha_ini; Daniel@0: case 'inv', a = alpha_ini / (1 + 99*(t-1)/(trainlen-1)); Daniel@0: case 'power', a = alpha_ini * (0.005/alpha_ini)^((t-1)/trainlen); Daniel@0: end Daniel@0: Daniel@0: %% update Daniel@0: M(:,known) = M(:,known) - a*h(:,ones(sum(known),1)).*Dx; Daniel@0: Daniel@0: %% tracking Daniel@0: if t==1 | ~rem(t,trackstep), Daniel@0: elap_t = etime(clock,start); tot_t = elap_t*trainlen/t; Daniel@0: fprintf(1,'\rTraining: %3.0f/ %3.0f s',elap_t,tot_t) Daniel@0: end Daniel@0: Daniel@0: end; % for t = 1:trainlen Daniel@0: fprintf(1,'\n'); Daniel@0: Daniel@0: % outputs Daniel@0: sTrain = som_set('som_train','algorithm','proto',... Daniel@0: 'data_name',data_name,... Daniel@0: 'neigh',neigh,... Daniel@0: 'mask',mask,... Daniel@0: 'radius_ini',rini,... Daniel@0: 'radius_fin',rfin,... Daniel@0: 'alpha_ini',alpha_ini,... Daniel@0: 'alpha_type',alpha_type,... Daniel@0: 'trainlen',trainlen,... Daniel@0: 'time',datestr(now,0)); Daniel@0: Daniel@0: if struct_mode, Daniel@0: sM = som_set(sM,'codebook',M,'mask',mask,'neigh',neigh); Daniel@0: sM.trainhist(end+1) = sTrain; Daniel@0: else Daniel@0: sM = reshape(M,orig_size); Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: Daniel@0: Daniel@0: