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