Daniel@0: Daniel@0: %SOM_DEMO1 Basic properties and behaviour of the Self-Organizing Map. Daniel@0: Daniel@0: % Contributed to SOM Toolbox 2.0, February 11th, 2000 by Juha Vesanto Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 1.0beta juuso 071197 Daniel@0: % Version 2.0beta juuso 030200 Daniel@0: Daniel@0: clf reset; Daniel@0: figure(gcf) Daniel@0: echo on Daniel@0: Daniel@0: Daniel@0: Daniel@0: clc Daniel@0: % ========================================================== Daniel@0: % SOM_DEMO1 - BEHAVIOUR AND PROPERTIES OF SOM Daniel@0: % ========================================================== Daniel@0: Daniel@0: % som_make - Create, initialize and train a SOM. Daniel@0: % som_randinit - Create and initialize a SOM. Daniel@0: % som_lininit - Create and initialize a SOM. Daniel@0: % som_seqtrain - Train a SOM. Daniel@0: % som_batchtrain - Train a SOM. Daniel@0: % som_bmus - Find best-matching units (BMUs). Daniel@0: % som_quality - Measure quality of SOM. Daniel@0: Daniel@0: % SELF-ORGANIZING MAP (SOM): Daniel@0: Daniel@0: % A self-organized map (SOM) is a "map" of the training data, Daniel@0: % dense where there is a lot of data and thin where the data Daniel@0: % density is low. Daniel@0: Daniel@0: % The map constitutes of neurons located on a regular map grid. Daniel@0: % The lattice of the grid can be either hexagonal or rectangular. Daniel@0: Daniel@0: subplot(1,2,1) Daniel@0: som_cplane('hexa',[10 15],'none') Daniel@0: title('Hexagonal SOM grid') Daniel@0: Daniel@0: subplot(1,2,2) Daniel@0: som_cplane('rect',[10 15],'none') Daniel@0: title('Rectangular SOM grid') Daniel@0: Daniel@0: % Each neuron (hexagon on the left, rectangle on the right) has an Daniel@0: % associated prototype vector. After training, neighboring neurons Daniel@0: % have similar prototype vectors. Daniel@0: Daniel@0: % The SOM can be used for data visualization, clustering (or Daniel@0: % classification), estimation and a variety of other purposes. Daniel@0: Daniel@0: pause % Strike any key to continue... Daniel@0: Daniel@0: clf Daniel@0: clc Daniel@0: % INITIALIZE AND TRAIN THE SELF-ORGANIZING MAP Daniel@0: % ============================================ Daniel@0: Daniel@0: % Here are 300 data points sampled from the unit square: Daniel@0: Daniel@0: D = rand(300,2); Daniel@0: Daniel@0: % The map will be a 2-dimensional grid of size 10 x 10. Daniel@0: Daniel@0: msize = [10 10]; Daniel@0: Daniel@0: % SOM_RANDINIT and SOM_LININIT can be used to initialize the Daniel@0: % prototype vectors in the map. The map size is actually an Daniel@0: % optional argument. If omitted, it is determined automatically Daniel@0: % based on the amount of data vectors and the principal Daniel@0: % eigenvectors of the data set. Below, the random initialization Daniel@0: % algorithm is used. Daniel@0: Daniel@0: sMap = som_randinit(D, 'msize', msize); Daniel@0: Daniel@0: % Actually, each map unit can be thought as having two sets Daniel@0: % of coordinates: Daniel@0: % (1) in the input space: the prototype vectors Daniel@0: % (2) in the output space: the position on the map Daniel@0: % In the two spaces, the map looks like this: Daniel@0: Daniel@0: subplot(1,3,1) Daniel@0: som_grid(sMap) Daniel@0: axis([0 11 0 11]), view(0,-90), title('Map in output space') Daniel@0: Daniel@0: subplot(1,3,2) Daniel@0: plot(D(:,1),D(:,2),'+r'), hold on Daniel@0: som_grid(sMap,'Coord',sMap.codebook) Daniel@0: title('Map in input space') Daniel@0: Daniel@0: % The black dots show positions of map units, and the gray lines Daniel@0: % show connections between neighboring map units. Since the map Daniel@0: % was initialized randomly, the positions in in the input space are Daniel@0: % completely disorganized. The red crosses are training data. Daniel@0: Daniel@0: pause % Strike any key to train the SOM... Daniel@0: Daniel@0: % During training, the map organizes and folds to the training Daniel@0: % data. Here, the sequential training algorithm is used: Daniel@0: Daniel@0: sMap = som_seqtrain(sMap,D,'radius',[5 1],'trainlen',10); Daniel@0: Daniel@0: subplot(1,3,3) Daniel@0: som_grid(sMap,'Coord',sMap.codebook) Daniel@0: hold on, plot(D(:,1),D(:,2),'+r') Daniel@0: title('Trained map') Daniel@0: Daniel@0: pause % Strike any key to view more closely the training process... Daniel@0: Daniel@0: Daniel@0: clf Daniel@0: Daniel@0: clc Daniel@0: % TRAINING THE SELF-ORGANIZING MAP Daniel@0: % ================================ Daniel@0: Daniel@0: % To get a better idea of what happens during training, let's look Daniel@0: % at how the map gradually unfolds and organizes itself. To make it Daniel@0: % even more clear, the map is now initialized so that it is away Daniel@0: % from the data. Daniel@0: Daniel@0: sMap = som_randinit(D,'msize',msize); Daniel@0: sMap.codebook = sMap.codebook + 1; Daniel@0: Daniel@0: subplot(1,2,1) Daniel@0: som_grid(sMap,'Coord',sMap.codebook) Daniel@0: hold on, plot(D(:,1),D(:,2),'+r'), hold off Daniel@0: title('Data and original map') Daniel@0: Daniel@0: % The training is based on two principles: Daniel@0: % Daniel@0: % Competitive learning: the prototype vector most similar to a Daniel@0: % data vector is modified so that it it is even more similar to Daniel@0: % it. This way the map learns the position of the data cloud. Daniel@0: % Daniel@0: % Cooperative learning: not only the most similar prototype Daniel@0: % vector, but also its neighbors on the map are moved towards the Daniel@0: % data vector. This way the map self-organizes. Daniel@0: Daniel@0: pause % Strike any key to train the map... Daniel@0: Daniel@0: echo off Daniel@0: subplot(1,2,2) Daniel@0: o = ones(5,1); Daniel@0: r = (1-[1:60]/60); Daniel@0: for i=1:60, Daniel@0: sMap = som_seqtrain(sMap,D,'tracking',0,... Daniel@0: 'trainlen',5,'samples',... Daniel@0: 'alpha',0.1*o,'radius',(4*r(i)+1)*o); Daniel@0: som_grid(sMap,'Coord',sMap.codebook) Daniel@0: hold on, plot(D(:,1),D(:,2),'+r'), hold off Daniel@0: title(sprintf('%d/300 training steps',5*i)) Daniel@0: drawnow Daniel@0: end Daniel@0: title('Sequential training after 300 steps') Daniel@0: echo on Daniel@0: Daniel@0: pause % Strike any key to continue with 3D data... Daniel@0: Daniel@0: clf Daniel@0: Daniel@0: clc Daniel@0: % TRAINING DATA: THE UNIT CUBE Daniel@0: % ============================ Daniel@0: Daniel@0: % Above, the map dimension was equal to input space dimension: both Daniel@0: % were 2-dimensional. Typically, the input space dimension is much Daniel@0: % higher than the 2-dimensional map. In this case the map cannot Daniel@0: % follow perfectly the data set any more but must find a balance Daniel@0: % between two goals: Daniel@0: Daniel@0: % - data representation accuracy Daniel@0: % - data set topology representation accuracy Daniel@0: Daniel@0: % Here are 500 data points sampled from the unit cube: Daniel@0: Daniel@0: D = rand(500,3); Daniel@0: Daniel@0: subplot(1,3,1), plot3(D(:,1),D(:,2),D(:,3),'+r') Daniel@0: view(3), axis on, rotate3d on Daniel@0: title('Data') Daniel@0: Daniel@0: % The ROTATE3D command enables you to rotate the picture by Daniel@0: % dragging the pointer above the picture with the leftmost mouse Daniel@0: % button pressed down. Daniel@0: Daniel@0: pause % Strike any key to train the SOM... Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: clc Daniel@0: % DEFAULT TRAINING PROCEDURE Daniel@0: % ========================== Daniel@0: Daniel@0: % Above, the initialization was done randomly and training was done Daniel@0: % with sequential training function (SOM_SEQTRAIN). By default, the Daniel@0: % initialization is linear, and batch training algorithm is Daniel@0: % used. In addition, the training is done in two phases: first with Daniel@0: % large neighborhood radius, and then finetuning with small radius. Daniel@0: Daniel@0: % The function SOM_MAKE can be used to both initialize and train Daniel@0: % the map using default parameters: Daniel@0: Daniel@0: pause % Strike any key to use SOM_MAKE... Daniel@0: Daniel@0: sMap = som_make(D); Daniel@0: Daniel@0: % Here, the linear initialization is done again, so that Daniel@0: % the results can be compared. Daniel@0: Daniel@0: sMap0 = som_lininit(D); Daniel@0: Daniel@0: subplot(1,3,2) Daniel@0: som_grid(sMap0,'Coord',sMap0.codebook,... Daniel@0: 'Markersize',2,'Linecolor','k','Surf',sMap0.codebook(:,3)) Daniel@0: axis([0 1 0 1 0 1]), view(-120,-25), title('After initialization') Daniel@0: Daniel@0: subplot(1,3,3) Daniel@0: som_grid(sMap,'Coord',sMap.codebook,... Daniel@0: 'Markersize',2,'Linecolor','k','Surf',sMap.codebook(:,3)) Daniel@0: axis([0 1 0 1 0 1]), view(3), title('After training'), hold on Daniel@0: Daniel@0: % Here you can see that the 2-dimensional map has folded into the Daniel@0: % 3-dimensional space in order to be able to capture the whole data Daniel@0: % space. Daniel@0: Daniel@0: pause % Strike any key to evaluate the quality of maps... Daniel@0: Daniel@0: Daniel@0: Daniel@0: clc Daniel@0: % BEST-MATCHING UNITS (BMU) Daniel@0: % ========================= Daniel@0: Daniel@0: % Before going to the quality, an important concept needs to be Daniel@0: % introduced: the Best-Matching Unit (BMU). The BMU of a data Daniel@0: % vector is the unit on the map whose model vector best resembles Daniel@0: % the data vector. In practise the similarity is measured as the Daniel@0: % minimum distance between data vector and each model vector on the Daniel@0: % map. The BMUs can be calculated using function SOM_BMUS. This Daniel@0: % function gives the index of the unit. Daniel@0: Daniel@0: % Here the BMU is searched for the origin point (from the Daniel@0: % trained map): Daniel@0: Daniel@0: bmu = som_bmus(sMap,[0 0 0]); Daniel@0: Daniel@0: % Here the corresponding unit is shown in the figure. You can Daniel@0: % rotate the figure to see better where the BMU is. Daniel@0: Daniel@0: co = sMap.codebook(bmu,:); Daniel@0: text(co(1),co(2),co(3),'BMU','Fontsize',20) Daniel@0: plot3([0 co(1)],[0 co(2)],[0 co(3)],'ro-') Daniel@0: Daniel@0: pause % Strike any key to analyze map quality... Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: clc Daniel@0: % SELF-ORGANIZING MAP QUALITY Daniel@0: % =========================== Daniel@0: Daniel@0: % The maps have two primary quality properties: Daniel@0: % - data representation accuracy Daniel@0: % - data set topology representation accuracy Daniel@0: Daniel@0: % The former is usually measured using average quantization error Daniel@0: % between data vectors and their BMUs on the map. For the latter Daniel@0: % several measures have been proposed, e.g. the topographic error Daniel@0: % measure: percentage of data vectors for which the first- and Daniel@0: % second-BMUs are not adjacent units. Daniel@0: Daniel@0: % Both measures have been implemented in the SOM_QUALITY function. Daniel@0: % Here are the quality measures for the trained map: Daniel@0: Daniel@0: [q,t] = som_quality(sMap,D) Daniel@0: Daniel@0: % And here for the initial map: Daniel@0: Daniel@0: [q0,t0] = som_quality(sMap0,D) Daniel@0: Daniel@0: % As can be seen, by folding the SOM has reduced the average Daniel@0: % quantization error, but on the other hand the topology Daniel@0: % representation capability has suffered. By using a larger final Daniel@0: % neighborhood radius in the training, the map becomes stiffer and Daniel@0: % preserves the topology of the data set better. Daniel@0: Daniel@0: Daniel@0: echo off Daniel@0: Daniel@0: