comparison toolboxes/FullBNT-1.0.7/netlab3.3/demsom1.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 %DEMSOM1 Demonstrate SOM for visualisation.
2 %
3 % Description
4 % This script demonstrates the use of a SOM with a two-dimensional
5 % grid to map onto data in two-dimensional space. Both on-line and
6 % batch training algorithms are shown.
7 %
8 % See also
9 % SOM, SOMPAK, SOMTRAIN
10 %
11
12 % Copyright (c) Ian T Nabney (1996-2001)
13
14
15 randn('state', 42);
16 rand('state', 42);
17 nin = 2;
18 ndata = 300;
19 % Give data an offset so that network has something to learn.
20 x = rand(ndata, nin) + ones(ndata, 1)*[1.5 1.5];
21
22 clc;
23 disp('This demonstration of the SOM, or Kohonen network, shows how the')
24 disp('network units after training lie in regions of high data density.')
25 disp('First we show the data, which is generated uniformly from a square.')
26 disp('Red crosses denote the data and black dots are the initial locations')
27 disp('of the SOM units.')
28 disp(' ')
29 disp('Press any key to continue.')
30 pause
31 net = som(nin, [8, 7]);
32 c1 = sompak(net);
33 h1 = figure;
34 plot(x(:, 1), x(:, 2), 'r+');
35 hold on
36 plot(c1(:,1), c1(:, 2), 'k.');
37 drawnow; % Force figure to be drawn before training starts
38 options = foptions;
39
40 % Ordering phase
41 options(1) = 1;
42 options(14) = 50;
43 %options(14) = 5; % Just for testing
44 options(18) = 0.9; % Initial learning rate
45 options(16) = 0.05; % Final learning rate
46 options(17) = 8; % Initial neighbourhood size
47 options(15) = 1; % Final neighbourhood size
48
49 disp('The SOM network is trained in two phases using an on-line algorithm.')
50 disp('Initially the neighbourhood is set to 8 and is then reduced')
51 disp('linearly to 1 over the first 50 iterations.')
52 disp('Each iteration consists of a pass through the complete')
53 disp('dataset, while the weights are adjusted after each pattern.')
54 disp('The learning rate is reduced linearly from 0.9 to 0.05.')
55 disp('This ordering phase puts the units in a rough grid shape.')
56 disp('Blue circles denote the units at the end of this phase.')
57 disp(' ')
58 disp('Press any key to continue.')
59 pause
60 net2 = somtrain(net, options, x);
61 c2 = sompak(net2);
62 plot(c2(:, 1), c2(:, 2), 'bo');
63 drawnow;
64
65 % Convergence phase
66 options(1) = 1;
67 options(14) = 400;
68 options(18) = 0.05;
69 options(16) = 0.01;
70 options(17) = 0;
71 options(15) = 0;
72
73 disp('The second, convergence, phase of learning just updates the winning node.')
74 disp('The learning rate is reduced from 0.05 to 0.01 over 400 iterations.')
75 disp('Note how the error value does not decrease monotonically; it is')
76 disp('difficult to decide when training is complete in a principled way.')
77 disp('The units are plotted as green stars.')
78 disp(' ')
79 disp('Press any key to continue.')
80 pause
81 net3 = somtrain(net2, options, x);
82 c3 = sompak(net3);
83 plot(c3(:, 1), c3(:, 2), 'g*');
84 drawnow;
85
86 % Now try batch training
87 options(1) = 1;
88 options(6) = 1;
89 options(14) = 50;
90 options(17) = 3;
91 options(15) = 0;
92 disp('An alternative approach to the on-line algorithm is a batch update')
93 disp('rule. Each unit is updated to be the average weights')
94 disp('in a neighbourhood (which reduces from 3 to 0) over 50 iterations.');
95 disp('Note how the error is even more unstable at first, though eventually')
96 disp('it does converge.')
97 disp('The final units are shown as black triangles.')
98 disp(' ')
99 disp('Press any key to continue.')
100 pause
101 net4 = somtrain(net, options, x);
102 c4 = sompak(net4);
103 plot(c4(:, 1), c4(:, 2), 'k^')
104 legend('Data', 'Initial weights', 'Weights after ordering', ...
105 'Weights after convergence', 'Batch weights', 2);
106 drawnow;
107
108 disp(' ')
109 disp('Press any key to end.')
110 disp(' ')
111 pause
112
113 close(h1);