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