comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/neural_gas.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function [Neurons] = neural_gas(D,n,epochs,alpha0,lambda0)
2
3 %NEURAL_GAS Quantizes the data space using the neural gas algorithm.
4 %
5 % Neurons = neural_gas(D, n, epochs, [alpha0], [lambda0])
6 %
7 % C = neural_gas(D,50,10);
8 % sM = som_map_struct(sD);
9 % sM.codebook = neural_gas(sD,size(sM.codebook,1),10);
10 %
11 % Input and output arguments ([]'s are optional):
12 % D (matrix) the data matrix, size dlen x dim
13 % (struct) a data struct
14 % n (scalar) the number of neurons
15 % epochs (scalar) the number of training epochs (the number of
16 % training steps is dlen*epochs)
17 % [alpha0] (scalar) initial step size, 0.5 by default
18 % [lambda0] (scalar) initial decay constant, n/2 by default
19 %
20 % Neurons (matrix) the neuron matrix, size n x dim
21 %
22 % See also SOM_MAKE, KMEANS.
23
24 % References:
25 % T.M.Martinetz, S.G.Berkovich, and K.J.Schulten. "Neural-gas" network
26 % for vector quantization and its application to time-series prediction.
27 % IEEE Transactions on Neural Networks, 4(4):558-569, 1993.
28
29 % Contributed to SOM Toolbox vs2, February 2nd, 2000 by Juha Vesanto
30 % Copyright (c) by Juha Vesanto
31 % http://www.cis.hut.fi/projects/somtoolbox/
32
33 % juuso 101297 020200
34
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %% Check arguments and initialize
37
38 error(nargchk(3, 5, nargin)); % check the number of input arguments
39
40 if isstruct(D), D = D.data; end
41 [dlen,dim] = size(D);
42 Neurons = (rand(n,dim)-0.5)*10e-5; % small initial values
43 train_len = epochs*dlen;
44
45 if nargin<4 | isempty(alpha0) | isnan(alpha0), alpha0 = 0.5; end
46 if nargin<5 | isempty(lambda0) | isnan(lambda0), lambda0 = n/2; end
47
48 % random sample order
49 rand('state',sum(100*clock));
50 sample_inds = ceil(dlen*rand(train_len,1));
51
52 % lambda
53 lambda = lambda0 * (0.01/lambda0).^([0:(train_len-1)]/train_len);
54
55 % alpha
56 alpha = alpha0 * (0.005/alpha0).^([0:(train_len-1)]/train_len);
57
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59 %% Action
60
61 for i=1:train_len,
62
63 % sample vector
64 x = D(sample_inds(i),:); % sample vector
65 known = ~isnan(x); % its known components
66 X = x(ones(n,1),known); % we'll need this
67
68 % neighborhood ranking
69 Dx = Neurons(:,known) - X; % difference between vector and all map units
70 [qerrs, inds] = sort((Dx.^2)*known'); % 1-BMU, 2-BMU, etc.
71 ranking(inds) = [0:(n-1)];
72 h = exp(-ranking/lambda(i));
73 H = h(ones(length(known),1),:)';
74
75 % update
76 Neurons = Neurons + alpha(i)*H.*(x(ones(n,1),known) - Neurons(:,known));
77
78 % track
79 fprintf(1,'%d / %d \r',i,train_len);
80 if 0 & mod(i,50) == 0,
81 hold off, plot3(D(:,1),D(:,2),D(:,3),'bo')
82 hold on, plot3(Neurons(:,1),Neurons(:,2),Neurons(:,3),'r+')
83 drawnow
84 end
85 end
86
87 fprintf(1,'\n');
88
89 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%