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