annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/lvq3.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 codebook = lvq3(codebook,data,rlen,alpha,win,epsilon)
Daniel@0 2
Daniel@0 3 %LVQ3 trains codebook with LVQ3 -algorithm
Daniel@0 4 %
Daniel@0 5 % sM = lvq3(sM,D,rlen,alpha,win,epsilon)
Daniel@0 6 %
Daniel@0 7 % sM = lvq3(sM,sD,50*length(sM.codebook),0.05,0.2,0.3);
Daniel@0 8 %
Daniel@0 9 % Input and output arguments:
Daniel@0 10 % sM (struct) map struct, the class information must be
Daniel@0 11 % present on the first column of .labels field
Daniel@0 12 % D (struct) data struct, the class information must
Daniel@0 13 % be present on the first column of .labels field
Daniel@0 14 % rlen (scalar) running length
Daniel@0 15 % alpha (scalar) learning parameter, e.g. 0.05
Daniel@0 16 % win (scalar) window width parameter, e.g. 0.25
Daniel@0 17 % epsilon (scalar) relative learning parameter, e.g. 0.3
Daniel@0 18 %
Daniel@0 19 % sM (struct) map struct, the trained codebook
Daniel@0 20 %
Daniel@0 21 % NOTE: does not take mask into account.
Daniel@0 22 %
Daniel@0 23 % For more help, try 'type lvq3', or check out online documentation.
Daniel@0 24 % See also LVQ1, SOM_SUPERVISED, SOM_SEQTRAIN.
Daniel@0 25
Daniel@0 26 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 27 %
Daniel@0 28 % lvq3
Daniel@0 29 %
Daniel@0 30 % PURPOSE
Daniel@0 31 %
Daniel@0 32 % Trains codebook with the LVQ3 -algorithm (described below).
Daniel@0 33 %
Daniel@0 34 % SYNTAX
Daniel@0 35 %
Daniel@0 36 % sM = lvq3(sM, data, rlen, alpha, win, epsilon)
Daniel@0 37 %
Daniel@0 38 % DESCRIPTION
Daniel@0 39 %
Daniel@0 40 % Trains codebook with the LVQ3 -algorithm. Codebook contains a number
Daniel@0 41 % of vectors (mi, i=1,2,...,n) and so does data (vectors xj, j=1,2,...k).
Daniel@0 42 % Both vector sets are classified: vectors may have a class (classes are
Daniel@0 43 % set to data- or map -structure's 'labels' -field. For each xj the two
Daniel@0 44 % closest codebookvectors mc1 and mc2 are searched (euclidean distances
Daniel@0 45 % d1 and d2). xj must fall into the zone of window. That happens if:
Daniel@0 46 %
Daniel@0 47 % min(d1/d2, d2/d1) > s, where s = (1-win) / (1+win).
Daniel@0 48 %
Daniel@0 49 % If xj belongs to the same class of one of the mc1 and mc1, codebook
Daniel@0 50 % is updated as follows (let mc1 belong to the same class as xj):
Daniel@0 51 % mc1(t+1) = mc1(t) + alpha * (xj(t) - mc1(t))
Daniel@0 52 % mc2(t+1) = mc2(t) - alpha * (xj(t) - mc2(t))
Daniel@0 53 % If both mc1 and mc2 belong to the same class as xj, codebook is
Daniel@0 54 % updated as follows:
Daniel@0 55 % mc1(t+1) = mc1(t) + epsilon * alpha * (xj(t) - mc1(t))
Daniel@0 56 % mc2(t+1) = mc2(t) + epsilon * alpha * (xj(t) - mc2(t))
Daniel@0 57 % Otherwise updating is not performed.
Daniel@0 58 %
Daniel@0 59 % Argument 'rlen' tells how many times training -sequence is performed.
Daniel@0 60 %
Daniel@0 61 % Argument 'alpha' is recommended to be smaller than 0.1 and argument
Daniel@0 62 % 'epsilon' should be between 0.1 and 0.5.
Daniel@0 63 %
Daniel@0 64 % NOTE: does not take mask into account.
Daniel@0 65 %
Daniel@0 66 % REFERENCES
Daniel@0 67 %
Daniel@0 68 % Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag,
Daniel@0 69 % Berlin, 1995, pp. 181-182.
Daniel@0 70 %
Daniel@0 71 % See also LVQ_PAK from http://www.cis.hut.fi/research/som_lvq_pak.shtml
Daniel@0 72 %
Daniel@0 73 % REQUIRED INPUT ARGUMENTS
Daniel@0 74 %
Daniel@0 75 % sM The data to be trained.
Daniel@0 76 % (struct) A map struct.
Daniel@0 77 %
Daniel@0 78 % data The data to use in training.
Daniel@0 79 % (struct) A data struct.
Daniel@0 80 %
Daniel@0 81 % rlen (integer) Running length of LVQ3 -algorithm.
Daniel@0 82 %
Daniel@0 83 % alpha (float) Learning rate used in training, e.g. 0.05
Daniel@0 84 %
Daniel@0 85 % win (float) Window length, e.g. 0.25
Daniel@0 86 %
Daniel@0 87 % epsilon (float) Relative learning parameter, e.g. 0.3
Daniel@0 88 %
Daniel@0 89 % OUTPUT ARGUMENTS
Daniel@0 90 %
Daniel@0 91 % sM Trained data.
Daniel@0 92 % (struct) A map struct.
Daniel@0 93 %
Daniel@0 94 % EXAMPLE
Daniel@0 95 %
Daniel@0 96 % lab = unique(sD.labels(:,1)); % different classes
Daniel@0 97 % mu = length(lab)*5; % 5 prototypes for each
Daniel@0 98 % sM = som_randinit(sD,'msize',[mu 1]); % initial prototypes
Daniel@0 99 % sM.labels = [lab;lab;lab;lab;lab]; % their classes
Daniel@0 100 % sM = lvq1(sM,sD,50*mu,0.05); % use LVQ1 to adjust
Daniel@0 101 % % the prototypes
Daniel@0 102 % sM = lvq3(sM,sD,50*mu,0.05,0.2,0.3); % then use LVQ3
Daniel@0 103 %
Daniel@0 104 % SEE ALSO
Daniel@0 105 %
Daniel@0 106 % lvq1 Use LVQ1 algorithm for training.
Daniel@0 107 % som_supervised Train SOM using supervised training.
Daniel@0 108 % som_seqtrain Train SOM with sequential algorithm.
Daniel@0 109
Daniel@0 110 % Contributed to SOM Toolbox vs2, February 2nd, 2000 by Juha Parhankangas
Daniel@0 111 % Copyright (c) by Juha Parhankangas
Daniel@0 112 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 113
Daniel@0 114 % Juha Parhankangas 310100 juuso 020200
Daniel@0 115
Daniel@0 116 NOTFOUND = 1;
Daniel@0 117
Daniel@0 118 cod = codebook.codebook;
Daniel@0 119 dat = data.data;
Daniel@0 120
Daniel@0 121 c_class = codebook.labels(:,1);
Daniel@0 122 d_class = data.labels(:,1);
Daniel@0 123
Daniel@0 124 s = (1-win)/(1+win);
Daniel@0 125
Daniel@0 126 x = size(dat,1);
Daniel@0 127 y = size(cod,2);
Daniel@0 128
Daniel@0 129 c_class=class2num(c_class);
Daniel@0 130 d_class=class2num(d_class);
Daniel@0 131
Daniel@0 132 ONES=ones(size(cod,1),1);
Daniel@0 133
Daniel@0 134 for t=1:rlen
Daniel@0 135 fprintf('\rTraining round: %d/%d',t,rlen);
Daniel@0 136 tmp = NaN*ones(x,y);
Daniel@0 137
Daniel@0 138 for j=1:x
Daniel@0 139 flag = 0;
Daniel@0 140 mj = 0;
Daniel@0 141 mi = 0;
Daniel@0 142 no_NaN=find(~isnan(dat(j,:)));
Daniel@0 143 di=sqrt(sum([cod(:,no_NaN) - ONES*dat(j,no_NaN)].^2,2));
Daniel@0 144 [foo, ind1] = min(di);
Daniel@0 145 di(ind1)=Inf;
Daniel@0 146 [foo,ind2] = min(di);
Daniel@0 147
Daniel@0 148 %ind2=ind2+1;
Daniel@0 149
Daniel@0 150 if d_class(j) & d_class(j)==c_class(ind1)
Daniel@0 151 mj = ind1;
Daniel@0 152 mi = ind2;
Daniel@0 153 if d_class(j)==c_class(ind2)
Daniel@0 154 flag = 1;
Daniel@0 155 end
Daniel@0 156 elseif d_class(j) & d_class(j)==c_class(ind2)
Daniel@0 157 mj = ind2;
Daniel@0 158 mi = ind1;
Daniel@0 159 if d_class(j)==c_class(ind1)
Daniel@0 160 flag = 1;
Daniel@0 161 end
Daniel@0 162 end
Daniel@0 163
Daniel@0 164 if mj & mi
Daniel@0 165 if flag
Daniel@0 166 tmp([mj mi],:) = cod([mj mi],:) + epsilon*alpha*...
Daniel@0 167 (dat([j j],:) - cod([mj mi],:));
Daniel@0 168 else
Daniel@0 169 tmp(mj,:) = cod(mj,:) + alpha * (dat(j,:)-cod(mj,:));
Daniel@0 170 tmp(mi,:) = cod(mi,:) - alpha * (dat(j,:)-cod(mj,:));
Daniel@0 171 end
Daniel@0 172 end
Daniel@0 173 end
Daniel@0 174 inds = find(~isnan(sum(tmp,2)));
Daniel@0 175 cod(inds,:) = tmp(inds,:);
Daniel@0 176 end
Daniel@0 177 fprintf(1,'\n');
Daniel@0 178
Daniel@0 179 sTrain = som_set('som_train','algorithm','lvq3',...
Daniel@0 180 'data_name',data.name,...
Daniel@0 181 'neigh','',...
Daniel@0 182 'mask',ones(y,1),...
Daniel@0 183 'radius_ini',NaN,...
Daniel@0 184 'radius_fin',NaN,...
Daniel@0 185 'alpha_ini',alpha,...
Daniel@0 186 'alpha_type','constant',...
Daniel@0 187 'trainlen',rlen,...
Daniel@0 188 'time',datestr(now,0));
Daniel@0 189 codebook.trainhist(end+1) = sTrain;
Daniel@0 190
Daniel@0 191 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 192
Daniel@0 193 function nos = class2num(class)
Daniel@0 194
Daniel@0 195 names = {};
Daniel@0 196 nos = zeros(length(class),1);
Daniel@0 197
Daniel@0 198 for i=1:length(class)
Daniel@0 199 if ~isempty(class{i}) & ~any(strcmp(class{i},names))
Daniel@0 200 names=cat(1,names,class(i));
Daniel@0 201 end
Daniel@0 202 end
Daniel@0 203
Daniel@0 204 tmp_nos = (1:length(names))';
Daniel@0 205
Daniel@0 206 for i=1:length(class)
Daniel@0 207 if ~isempty(class{i})
Daniel@0 208 nos(i,1) = find(strcmp(class{i},names));
Daniel@0 209 end
Daniel@0 210 end
Daniel@0 211
Daniel@0 212
Daniel@0 213
Daniel@0 214
Daniel@0 215