annotate toolboxes/FullBNT-1.0.7/netlab3.3/demns1.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 %DEMNS1 Demonstrate Neuroscale for visualisation.
wolffd@0 2 %
wolffd@0 3 % Description
wolffd@0 4 % This script demonstrates the use of the Neuroscale algorithm for
wolffd@0 5 % topographic projection and visualisation. A data sample is generated
wolffd@0 6 % from a mixture of two Gaussians in 4d space, and an RBF is trained
wolffd@0 7 % with the stress error function to project the data into 2d. The
wolffd@0 8 % training data and a test sample are both plotted in this projection.
wolffd@0 9 %
wolffd@0 10 % See also
wolffd@0 11 % RBF, RBFTRAIN, RBFPRIOR
wolffd@0 12 %
wolffd@0 13
wolffd@0 14 % Copyright (c) Ian T Nabney (1996-2001)
wolffd@0 15
wolffd@0 16 % Generate the data
wolffd@0 17 % Fix seeds for reproducible results
wolffd@0 18 rand('state', 420);
wolffd@0 19 randn('state', 420);
wolffd@0 20
wolffd@0 21 input_dim = 4;
wolffd@0 22 output_dim = 2;
wolffd@0 23 mix = gmm(input_dim, 2, 'spherical');
wolffd@0 24 mix.centres = [1 1 1 1; 0 0 0 0];
wolffd@0 25 mix.priors = [0.5 0.5];
wolffd@0 26 mix.covars = [0.1 0.1];
wolffd@0 27
wolffd@0 28 ndata = 60;
wolffd@0 29 [data, labels] = gmmsamp(mix, ndata);
wolffd@0 30
wolffd@0 31 clc
wolffd@0 32 disp('This demonstration illustrates the use of the Neuroscale model')
wolffd@0 33 disp('to perform a topographic projection of data. We begin by generating')
wolffd@0 34 disp('60 data points from a mixture of two Gaussians in 4 dimensional space.')
wolffd@0 35 disp(' ')
wolffd@0 36 disp('Press any key to continue')
wolffd@0 37 pause
wolffd@0 38
wolffd@0 39 ncentres = 10;
wolffd@0 40 net = rbf(input_dim, ncentres, output_dim, 'tps', 'neuroscale');
wolffd@0 41 dstring = ['the Sammon mapping. The model has ', num2str(ncentres), ...
wolffd@0 42 ' centres, two outputs, and uses'];
wolffd@0 43 clc
wolffd@0 44 disp('The Neuroscale model is an RBF with a Stress error measure as used in')
wolffd@0 45 disp(dstring)
wolffd@0 46 disp('thin plate spline basis functions.')
wolffd@0 47 disp(' ')
wolffd@0 48 disp('It is trained using the shadow targets algorithm for at most 60 iterations.')
wolffd@0 49 disp(' ')
wolffd@0 50 disp('Press any key to continue')
wolffd@0 51 pause
wolffd@0 52
wolffd@0 53 % First row controls shadow targets, second row controls rbfsetbf
wolffd@0 54 options(1, :) = foptions;
wolffd@0 55 options(2, :) = foptions;
wolffd@0 56 options(1, 1) = 1;
wolffd@0 57 options(1, 2) = 1e-2;
wolffd@0 58 options(1, 3) = 1e-2;
wolffd@0 59 options(1, 6) = 1; % Switch on PCA initialisation
wolffd@0 60 options(1, 14) = 60;
wolffd@0 61 options(2, 1) = -1; % Switch off all warnings
wolffd@0 62 options(2, 5) = 1;
wolffd@0 63 options(2, 14) = 10;
wolffd@0 64 net2 = rbftrain(net, options, data);
wolffd@0 65
wolffd@0 66 disp(' ')
wolffd@0 67 disp('After training the model, we project the training data by a normal')
wolffd@0 68 disp('forward propagation through the RBF network. Because there are two')
wolffd@0 69 disp('outputs, the results can be plotted and visualised.')
wolffd@0 70 disp(' ')
wolffd@0 71 disp('Press any key to continue')
wolffd@0 72 pause
wolffd@0 73
wolffd@0 74 % Plot the result
wolffd@0 75 y = rbffwd(net2, data);
wolffd@0 76 ClassSymbol1 = 'r.';
wolffd@0 77 ClassSymbol2 = 'b.';
wolffd@0 78 PointSize = 12;
wolffd@0 79 fh1 = figure;
wolffd@0 80 hold on;
wolffd@0 81 plot(y((labels==1),1),y(labels==1,2),ClassSymbol1, 'MarkerSize', PointSize)
wolffd@0 82 plot(y((labels>1),1),y(labels>1,2),ClassSymbol2, 'MarkerSize', PointSize)
wolffd@0 83
wolffd@0 84 disp(' ')
wolffd@0 85 disp('In this plot, the red dots denote the first class and the blue')
wolffd@0 86 disp('dots the second class.')
wolffd@0 87 disp(' ')
wolffd@0 88 disp('Press any key to continue.')
wolffd@0 89 disp(' ')
wolffd@0 90 pause
wolffd@0 91
wolffd@0 92 disp('We now generate a further 100 data points from the original distribution')
wolffd@0 93 disp('and plot their projection using star symbols. Note that a Sammon')
wolffd@0 94 disp('mapping cannot be used to generalise to new data in this fashion.')
wolffd@0 95
wolffd@0 96 [test_data, test_labels] = gmmsamp(mix, 100);
wolffd@0 97 ytest = rbffwd(net2, test_data);
wolffd@0 98 ClassSymbol1 = 'ro';
wolffd@0 99 ClassSymbol2 = 'bo';
wolffd@0 100 % Circles are rather large symbols
wolffd@0 101 PointSize = 6;
wolffd@0 102 hold on
wolffd@0 103 plot(ytest((test_labels==1),1),ytest(test_labels==1,2), ...
wolffd@0 104 ClassSymbol1, 'MarkerSize', PointSize)
wolffd@0 105 plot(ytest((test_labels>1),1),ytest(test_labels>1,2),...
wolffd@0 106 ClassSymbol2, 'MarkerSize', PointSize)
wolffd@0 107 hold on
wolffd@0 108 legend('Class 1', 'Class 2', 'Test Class 1', 'Test Class 2')
wolffd@0 109 disp('Press any key to exit.')
wolffd@0 110 pause
wolffd@0 111
wolffd@0 112 close(fh1);
wolffd@0 113 clear all;
wolffd@0 114