wolffd@0: %DEMSOM1 Demonstrate SOM for visualisation. wolffd@0: % wolffd@0: % Description wolffd@0: % This script demonstrates the use of a SOM with a two-dimensional wolffd@0: % grid to map onto data in two-dimensional space. Both on-line and wolffd@0: % batch training algorithms are shown. wolffd@0: % wolffd@0: % See also wolffd@0: % SOM, SOMPAK, SOMTRAIN wolffd@0: % wolffd@0: wolffd@0: % Copyright (c) Ian T Nabney (1996-2001) wolffd@0: wolffd@0: wolffd@0: randn('state', 42); wolffd@0: rand('state', 42); wolffd@0: nin = 2; wolffd@0: ndata = 300; wolffd@0: % Give data an offset so that network has something to learn. wolffd@0: x = rand(ndata, nin) + ones(ndata, 1)*[1.5 1.5]; wolffd@0: wolffd@0: clc; wolffd@0: disp('This demonstration of the SOM, or Kohonen network, shows how the') wolffd@0: disp('network units after training lie in regions of high data density.') wolffd@0: disp('First we show the data, which is generated uniformly from a square.') wolffd@0: disp('Red crosses denote the data and black dots are the initial locations') wolffd@0: disp('of the SOM units.') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue.') wolffd@0: pause wolffd@0: net = som(nin, [8, 7]); wolffd@0: c1 = sompak(net); wolffd@0: h1 = figure; wolffd@0: plot(x(:, 1), x(:, 2), 'r+'); wolffd@0: hold on wolffd@0: plot(c1(:,1), c1(:, 2), 'k.'); wolffd@0: drawnow; % Force figure to be drawn before training starts wolffd@0: options = foptions; wolffd@0: wolffd@0: % Ordering phase wolffd@0: options(1) = 1; wolffd@0: options(14) = 50; wolffd@0: %options(14) = 5; % Just for testing wolffd@0: options(18) = 0.9; % Initial learning rate wolffd@0: options(16) = 0.05; % Final learning rate wolffd@0: options(17) = 8; % Initial neighbourhood size wolffd@0: options(15) = 1; % Final neighbourhood size wolffd@0: wolffd@0: disp('The SOM network is trained in two phases using an on-line algorithm.') wolffd@0: disp('Initially the neighbourhood is set to 8 and is then reduced') wolffd@0: disp('linearly to 1 over the first 50 iterations.') wolffd@0: disp('Each iteration consists of a pass through the complete') wolffd@0: disp('dataset, while the weights are adjusted after each pattern.') wolffd@0: disp('The learning rate is reduced linearly from 0.9 to 0.05.') wolffd@0: disp('This ordering phase puts the units in a rough grid shape.') wolffd@0: disp('Blue circles denote the units at the end of this phase.') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue.') wolffd@0: pause wolffd@0: net2 = somtrain(net, options, x); wolffd@0: c2 = sompak(net2); wolffd@0: plot(c2(:, 1), c2(:, 2), 'bo'); wolffd@0: drawnow; wolffd@0: wolffd@0: % Convergence phase wolffd@0: options(1) = 1; wolffd@0: options(14) = 400; wolffd@0: options(18) = 0.05; wolffd@0: options(16) = 0.01; wolffd@0: options(17) = 0; wolffd@0: options(15) = 0; wolffd@0: wolffd@0: disp('The second, convergence, phase of learning just updates the winning node.') wolffd@0: disp('The learning rate is reduced from 0.05 to 0.01 over 400 iterations.') wolffd@0: disp('Note how the error value does not decrease monotonically; it is') wolffd@0: disp('difficult to decide when training is complete in a principled way.') wolffd@0: disp('The units are plotted as green stars.') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue.') wolffd@0: pause wolffd@0: net3 = somtrain(net2, options, x); wolffd@0: c3 = sompak(net3); wolffd@0: plot(c3(:, 1), c3(:, 2), 'g*'); wolffd@0: drawnow; wolffd@0: wolffd@0: % Now try batch training wolffd@0: options(1) = 1; wolffd@0: options(6) = 1; wolffd@0: options(14) = 50; wolffd@0: options(17) = 3; wolffd@0: options(15) = 0; wolffd@0: disp('An alternative approach to the on-line algorithm is a batch update') wolffd@0: disp('rule. Each unit is updated to be the average weights') wolffd@0: disp('in a neighbourhood (which reduces from 3 to 0) over 50 iterations.'); wolffd@0: disp('Note how the error is even more unstable at first, though eventually') wolffd@0: disp('it does converge.') wolffd@0: disp('The final units are shown as black triangles.') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue.') wolffd@0: pause wolffd@0: net4 = somtrain(net, options, x); wolffd@0: c4 = sompak(net4); wolffd@0: plot(c4(:, 1), c4(:, 2), 'k^') wolffd@0: legend('Data', 'Initial weights', 'Weights after ordering', ... wolffd@0: 'Weights after convergence', 'Batch weights', 2); wolffd@0: drawnow; wolffd@0: wolffd@0: disp(' ') wolffd@0: disp('Press any key to end.') wolffd@0: disp(' ') wolffd@0: pause wolffd@0: wolffd@0: close(h1);