annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_prototrain.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,sTrain] = som_prototrain(sM, D)
Daniel@0 2
Daniel@0 3 %SOM_PROTOTRAIN Use sequential algorithm to train the Self-Organizing Map.
Daniel@0 4 %
Daniel@0 5 % [sM,sT] = som_prototrain(sM, D)
Daniel@0 6 %
Daniel@0 7 % sM = som_prototrain(sM,D);
Daniel@0 8 %
Daniel@0 9 % Input and output arguments:
Daniel@0 10 % sM (struct) map struct, the trained and updated map is returned
Daniel@0 11 % (matrix) codebook matrix of a self-organizing map
Daniel@0 12 % size munits x dim or msize(1) x ... x msize(k) x dim
Daniel@0 13 % The trained map codebook is returned.
Daniel@0 14 % D (struct) training data; data struct
Daniel@0 15 % (matrix) training data, size dlen x dim
Daniel@0 16 %
Daniel@0 17 % This function is otherwise just like SOM_SEQTRAIN except that
Daniel@0 18 % the implementation of the sequential training algorithm is very
Daniel@0 19 % straightforward (and slower). This should make it easy for you
Daniel@0 20 % to modify the algorithm, if you want to.
Daniel@0 21 %
Daniel@0 22 % For help on input and output parameters, try
Daniel@0 23 % 'type som_prototrain' or check out the help for SOM_SEQTRAIN.
Daniel@0 24 % See also SOM_SEQTRAIN, SOM_BATCHTRAIN.
Daniel@0 25
Daniel@0 26 % Contributed to SOM Toolbox vs2, February 2nd, 2000 by Juha Vesanto
Daniel@0 27 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 28
Daniel@0 29 % Version 2.0beta juuso 080200 130300
Daniel@0 30
Daniel@0 31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 32 %% Check input arguments
Daniel@0 33
Daniel@0 34 % map
Daniel@0 35 struct_mode = isstruct(sM);
Daniel@0 36 if struct_mode,
Daniel@0 37 M = sM.codebook;
Daniel@0 38 sTopol = sM.topol;
Daniel@0 39 mask = sM.mask;
Daniel@0 40 msize = sTopol.msize;
Daniel@0 41 neigh = sM.neigh;
Daniel@0 42 else
Daniel@0 43 M = sM; orig_size = size(M);
Daniel@0 44 if ndims(sM) > 2,
Daniel@0 45 si = size(sM); dim = si(end); msize = si(1:end-1);
Daniel@0 46 M = reshape(sM,[prod(msize) dim]);
Daniel@0 47 else
Daniel@0 48 msize = [orig_size(1) 1]; dim = orig_size(2);
Daniel@0 49 end
Daniel@0 50 sM = som_map_struct(dim,'msize',msize); sTopol = sM.topol;
Daniel@0 51 mask = ones(dim,1);
Daniel@0 52 neigh = 'gaussian';
Daniel@0 53 end
Daniel@0 54 [munits dim] = size(M);
Daniel@0 55
Daniel@0 56 % data
Daniel@0 57 if isstruct(D), data_name = D.name; D = D.data;
Daniel@0 58 else data_name = inputname(2);
Daniel@0 59 end
Daniel@0 60 D = D(find(sum(isnan(D),2) < dim),:); % remove empty vectors from the data
Daniel@0 61 [dlen ddim] = size(D); % check input dimension
Daniel@0 62 if dim ~= ddim, error('Map and data input space dimensions disagree.'); end
Daniel@0 63
Daniel@0 64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 65 %% initialize (these are default values, change as you will)
Daniel@0 66
Daniel@0 67 % training length
Daniel@0 68 trainlen = 20*dlen; % 20 epochs by default
Daniel@0 69
Daniel@0 70 % neighborhood radius
Daniel@0 71 radius_type = 'linear';
Daniel@0 72 rini = max(msize)/2;
Daniel@0 73 rfin = 1;
Daniel@0 74
Daniel@0 75 % learning rate
Daniel@0 76 alpha_type = 'inv';
Daniel@0 77 alpha_ini = 0.2;
Daniel@0 78
Daniel@0 79 % initialize random number generator
Daniel@0 80 rand('state',sum(100*clock));
Daniel@0 81
Daniel@0 82 % tracking
Daniel@0 83 start = clock; trackstep = 100;
Daniel@0 84
Daniel@0 85 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 86 %% Action
Daniel@0 87
Daniel@0 88 Ud = som_unit_dists(sTopol); % distance between map units on the grid
Daniel@0 89 mu_x_1 = ones(munits,1); % this is used pretty often
Daniel@0 90
Daniel@0 91 for t = 1:trainlen,
Daniel@0 92
Daniel@0 93 %% find BMU
Daniel@0 94 ind = ceil(dlen*rand(1)+eps); % select one vector
Daniel@0 95 x = D(ind,:); % pick it up
Daniel@0 96 known = ~isnan(x); % its known components
Daniel@0 97 Dx = M(:,known) - x(mu_x_1,known); % each map unit minus the vector
Daniel@0 98 dist2 = (Dx.^2)*mask(known); % squared distances
Daniel@0 99 [qerr bmu] = min(dist2); % find BMU
Daniel@0 100
Daniel@0 101 %% neighborhood
Daniel@0 102 switch radius_type, % radius
Daniel@0 103 case 'linear', r = rini+(rfin-rini)*(t-1)/(trainlen-1);
Daniel@0 104 end
Daniel@0 105 if ~r, r=eps; end % zero neighborhood radius may cause div-by-zero error
Daniel@0 106 switch neigh, % neighborhood function
Daniel@0 107 case 'bubble', h = (Ud(:,bmu) <= r);
Daniel@0 108 case 'gaussian', h = exp(-(Ud(:,bmu).^2)/(2*r*r));
Daniel@0 109 case 'cutgauss', h = exp(-(Ud(:,bmu).^2)/(2*r*r)) .* (Ud(:,bmu) <= r);
Daniel@0 110 case 'ep', h = (1 - (Ud(:,bmu).^2)/(r*r)) .* (Ud(:,bmu) <= r);
Daniel@0 111 end
Daniel@0 112
Daniel@0 113 %% learning rate
Daniel@0 114 switch alpha_type,
Daniel@0 115 case 'linear', a = (1-t/trainlen)*alpha_ini;
Daniel@0 116 case 'inv', a = alpha_ini / (1 + 99*(t-1)/(trainlen-1));
Daniel@0 117 case 'power', a = alpha_ini * (0.005/alpha_ini)^((t-1)/trainlen);
Daniel@0 118 end
Daniel@0 119
Daniel@0 120 %% update
Daniel@0 121 M(:,known) = M(:,known) - a*h(:,ones(sum(known),1)).*Dx;
Daniel@0 122
Daniel@0 123 %% tracking
Daniel@0 124 if t==1 | ~rem(t,trackstep),
Daniel@0 125 elap_t = etime(clock,start); tot_t = elap_t*trainlen/t;
Daniel@0 126 fprintf(1,'\rTraining: %3.0f/ %3.0f s',elap_t,tot_t)
Daniel@0 127 end
Daniel@0 128
Daniel@0 129 end; % for t = 1:trainlen
Daniel@0 130 fprintf(1,'\n');
Daniel@0 131
Daniel@0 132 % outputs
Daniel@0 133 sTrain = som_set('som_train','algorithm','proto',...
Daniel@0 134 'data_name',data_name,...
Daniel@0 135 'neigh',neigh,...
Daniel@0 136 'mask',mask,...
Daniel@0 137 'radius_ini',rini,...
Daniel@0 138 'radius_fin',rfin,...
Daniel@0 139 'alpha_ini',alpha_ini,...
Daniel@0 140 'alpha_type',alpha_type,...
Daniel@0 141 'trainlen',trainlen,...
Daniel@0 142 'time',datestr(now,0));
Daniel@0 143
Daniel@0 144 if struct_mode,
Daniel@0 145 sM = som_set(sM,'codebook',M,'mask',mask,'neigh',neigh);
Daniel@0 146 sM.trainhist(end+1) = sTrain;
Daniel@0 147 else
Daniel@0 148 sM = reshape(M,orig_size);
Daniel@0 149 end
Daniel@0 150
Daniel@0 151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 152
Daniel@0 153
Daniel@0 154
Daniel@0 155