annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/lvq1.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=lvq1(codebook, data, rlen, alpha);
Daniel@0 2
Daniel@0 3 %LVQ1 Trains a codebook with the LVQ1 -algorithm.
Daniel@0 4 %
Daniel@0 5 % sM = lvq1(sM, D, rlen, alpha)
Daniel@0 6 %
Daniel@0 7 % sM = lvq1(sM,sD,30*length(sM.codebook),0.08);
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
Daniel@0 16 %
Daniel@0 17 % sM (struct) map struct, the trained codebook
Daniel@0 18 %
Daniel@0 19 % NOTE: does not take mask into account.
Daniel@0 20 %
Daniel@0 21 % For more help, try 'type lvq1', or check out online documentation.
Daniel@0 22 % See also LVQ3, SOM_SUPERVISED, SOM_SEQTRAIN.
Daniel@0 23
Daniel@0 24 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 25 %
Daniel@0 26 % lvq1
Daniel@0 27 %
Daniel@0 28 % PURPOSE
Daniel@0 29 %
Daniel@0 30 % Trains codebook with the LVQ1 -algorithm (described below).
Daniel@0 31 %
Daniel@0 32 % SYNTAX
Daniel@0 33 %
Daniel@0 34 % sM = lvq1(sM, D, rlen, alpha)
Daniel@0 35 %
Daniel@0 36 % DESCRIPTION
Daniel@0 37 %
Daniel@0 38 % Trains codebook with the LVQ1 -algorithm. Codebook contains a number
Daniel@0 39 % of vectors (mi, i=1,2,...,n) and so does data (vectors xj,
Daniel@0 40 % j=1,2,...,k). Both vector sets are classified: vectors may have a
Daniel@0 41 % class (classes are set to the first column of data or map -structs'
Daniel@0 42 % .labels -field). For each xj there is defined the nearest codebook
Daniel@0 43 % -vector index c by searching the minimum of the euclidean distances
Daniel@0 44 % between the current xj and codebook -vectors:
Daniel@0 45 %
Daniel@0 46 % c = min{ ||xj - mi|| }, i=[1,..,n], for fixed xj
Daniel@0 47 % i
Daniel@0 48 % If xj and mc belong to the same class, mc is updated as follows:
Daniel@0 49 % mc(t+1) = mc(t) + alpha * (xj(t) - mc(t))
Daniel@0 50 % If xj and mc belong to different classes, mc is updated as follows:
Daniel@0 51 % mc(t+1) = mc(t) - alpha * (xj(t) - mc(t))
Daniel@0 52 % Otherwise updating is not performed.
Daniel@0 53 %
Daniel@0 54 % Argument 'rlen' tells how many times training sequence is performed.
Daniel@0 55 % LVQ1 -algorithm may be stopped after a number of steps, that is
Daniel@0 56 % 30-50 times the number of codebook vectors.
Daniel@0 57 %
Daniel@0 58 % Argument 'alpha' is the learning rate, recommended to be smaller
Daniel@0 59 % than 0.1.
Daniel@0 60 %
Daniel@0 61 % NOTE: does not take mask into account.
Daniel@0 62 %
Daniel@0 63 % REFERENCES
Daniel@0 64 %
Daniel@0 65 % Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag,
Daniel@0 66 % Berlin, 1995, pp. 176-179.
Daniel@0 67 %
Daniel@0 68 % See also LVQ_PAK from http://www.cis.hut.fi/research/som_lvq_pak.shtml
Daniel@0 69 %
Daniel@0 70 % REQUIRED INPUT ARGUMENTS
Daniel@0 71 %
Daniel@0 72 % sM The data to be trained.
Daniel@0 73 % (struct) A map struct.
Daniel@0 74 %
Daniel@0 75 % D The data to use in training.
Daniel@0 76 % (struct) A data struct.
Daniel@0 77 %
Daniel@0 78 % rlen (integer) Running length of LVQ1 -algorithm.
Daniel@0 79 %
Daniel@0 80 % alpha (float) Learning rate used in training.
Daniel@0 81 %
Daniel@0 82 % OUTPUT ARGUMENTS
Daniel@0 83 %
Daniel@0 84 % codebook Trained data.
Daniel@0 85 % (struct) A map struct.
Daniel@0 86 %
Daniel@0 87 % EXAMPLE
Daniel@0 88 %
Daniel@0 89 % lab = unique(sD.labels(:,1)); % different classes
Daniel@0 90 % mu = length(lab)*5; % 5 prototypes for each
Daniel@0 91 % sM = som_randinit(sD,'msize',[mu 1]); % initial prototypes
Daniel@0 92 % sM.labels = [lab;lab;lab;lab;lab]; % their classes
Daniel@0 93 % sM = lvq1(sM,sD,50*mu,0.05); % use LVQ1 to adjust
Daniel@0 94 % % the prototypes
Daniel@0 95 % sM = lvq3(sM,sD,50*mu,0.05,0.2,0.3); % then use LVQ3
Daniel@0 96 %
Daniel@0 97 % SEE ALSO
Daniel@0 98 %
Daniel@0 99 % lvq3 Use LVQ3 algorithm for training.
Daniel@0 100 % som_supervised Train SOM using supervised training.
Daniel@0 101 % som_seqtrain Train SOM with sequential algorithm.
Daniel@0 102
Daniel@0 103 % Contributed to SOM Toolbox vs2, February 2nd, 2000 by Juha Parhankangas
Daniel@0 104 % Copyright (c) Juha Parhankangas
Daniel@0 105 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 106
Daniel@0 107 % Juha Parhankangas 310100 juuso 020200
Daniel@0 108
Daniel@0 109 cod = codebook.codebook;
Daniel@0 110 c_class = class2num(codebook.labels(:,1));
Daniel@0 111
Daniel@0 112 dat = data.data;
Daniel@0 113 d_class = class2num(data.labels(:,1));
Daniel@0 114
Daniel@0 115 x=size(dat,1);
Daniel@0 116 y=size(cod,2);
Daniel@0 117
Daniel@0 118 ONES=ones(size(cod,1),1);
Daniel@0 119
Daniel@0 120 for t=1:rlen
Daniel@0 121
Daniel@0 122 fprintf(1,'\rTraining round: %d',t);
Daniel@0 123 tmp=NaN*ones(x,y);
Daniel@0 124
Daniel@0 125 for j=1:x
Daniel@0 126 no_NaN=find(~isnan(dat(j,:)));
Daniel@0 127 di = sqrt(sum([cod(:,no_NaN) - ONES*dat(j,no_NaN)].^2,2));
Daniel@0 128
Daniel@0 129 [foo,ind] = min(di);
Daniel@0 130
Daniel@0 131 if d_class(j) & d_class(j) == c_class(ind) % 0 is for unclassified vectors
Daniel@0 132 tmp(ind,:) = cod(ind,:) + alpha * (dat(j,:) - cod(ind,:));
Daniel@0 133 elseif d_class(j)
Daniel@0 134 tmp(ind,:) = cod(ind,:) - alpha*(dat(j,:) - cod(ind,:));
Daniel@0 135 end
Daniel@0 136 end
Daniel@0 137
Daniel@0 138 inds = find(~isnan(sum(tmp,2)));
Daniel@0 139 cod(inds,:) = tmp(inds,:);
Daniel@0 140 end
Daniel@0 141
Daniel@0 142 codebook.codebook = cod;
Daniel@0 143
Daniel@0 144 sTrain = som_set('som_train','algorithm','lvq1',...
Daniel@0 145 'data_name',data.name,...
Daniel@0 146 'neigh','',...
Daniel@0 147 'mask',ones(y,1),...
Daniel@0 148 'radius_ini',NaN,...
Daniel@0 149 'radius_fin',NaN,...
Daniel@0 150 'alpha_ini',alpha,...
Daniel@0 151 'alpha_type','constant',...
Daniel@0 152 'trainlen',rlen,...
Daniel@0 153 'time',datestr(now,0));
Daniel@0 154 codebook.trainhist(end+1) = sTrain;
Daniel@0 155
Daniel@0 156 return;
Daniel@0 157
Daniel@0 158 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 159
Daniel@0 160 function nos = class2num(class)
Daniel@0 161
Daniel@0 162 names = {};
Daniel@0 163 nos = zeros(length(class),1);
Daniel@0 164
Daniel@0 165 for i=1:length(class)
Daniel@0 166 if ~isempty(class{i}) & ~any(strcmp(class{i},names))
Daniel@0 167 names=cat(1,names,class(i));
Daniel@0 168 end
Daniel@0 169 end
Daniel@0 170
Daniel@0 171 tmp_nos = (1:length(names))';
Daniel@0 172
Daniel@0 173 for i=1:length(class)
Daniel@0 174 if ~isempty(class{i})
Daniel@0 175 nos(i,1) = find(strcmp(class{i},names));
Daniel@0 176 end
Daniel@0 177 end
Daniel@0 178
Daniel@0 179
Daniel@0 180