annotate toolboxes/FullBNT-1.0.7/netlab3.3/demgtm2.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 %DEMGTM2 Demonstrate GTM for visualisation.
Daniel@0 2 %
Daniel@0 3 % Description
Daniel@0 4 % This script demonstrates the use of a GTM with a two-dimensional
Daniel@0 5 % latent space to visualise data in a higher dimensional space. This is
Daniel@0 6 % done through the use of the mean responsibility and magnification
Daniel@0 7 % factors.
Daniel@0 8 %
Daniel@0 9 % See also
Daniel@0 10 % DEMGTM1, GTM, GTMEM, GTMPOST
Daniel@0 11 %
Daniel@0 12
Daniel@0 13 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 14
Daniel@0 15
Daniel@0 16 % Fix seeds for reproducible results
Daniel@0 17 rand('state', 420);
Daniel@0 18 randn('state', 420);
Daniel@0 19
Daniel@0 20 ndata = 300
Daniel@0 21 clc;
Daniel@0 22 disp('This demonstration shows how a Generative Topographic Mapping')
Daniel@0 23 disp('can be used to model and visualise high dimensional data. The')
Daniel@0 24 disp('data is generated from a mixture of two spherical Gaussians in')
Daniel@0 25 dstring = ['four dimensional space. ', num2str(ndata), ...
Daniel@0 26 ' data points are generated.'];
Daniel@0 27 disp(dstring);
Daniel@0 28 disp(' ');
Daniel@0 29 disp('Press any key to continue.')
Daniel@0 30 pause
Daniel@0 31 % Create data
Daniel@0 32 data_dim = 4;
Daniel@0 33 latent_dim = 2;
Daniel@0 34 mix = gmm(data_dim, 2, 'spherical');
Daniel@0 35 mix.centres = [1 1 1 1; 0 0 0 0];
Daniel@0 36 mix.priors = [0.5 0.5];
Daniel@0 37 mix.covars = [0.1 0.1];
Daniel@0 38
Daniel@0 39 [data, labels] = gmmsamp(mix, ndata);
Daniel@0 40
Daniel@0 41 latent_shape = [15 15]; % Number of latent points in each dimension
Daniel@0 42 nlatent = prod(latent_shape); % Number of latent points
Daniel@0 43 num_rbf_centres = 16;
Daniel@0 44
Daniel@0 45 clc;
Daniel@0 46 dstring = ['Next we generate and initialise the GTM. There are ',...
Daniel@0 47 num2str(nlatent), ' latent points'];
Daniel@0 48 disp(dstring);
Daniel@0 49 dstring = ['arranged in a square of ', num2str(latent_shape(1)), ...
Daniel@0 50 ' points on a side. There are ', num2str(num_rbf_centres), ...
Daniel@0 51 ' centres in the'];
Daniel@0 52 disp(dstring);
Daniel@0 53 disp('RBF model, which has Gaussian activation functions.')
Daniel@0 54 disp(' ')
Daniel@0 55 disp('Once the model is created, the latent data sample')
Daniel@0 56 disp('and RBF centres are placed uniformly in the square [-1 1 -1 1].')
Daniel@0 57 disp('The output weights of the RBF are computed to map the latent');
Daniel@0 58 disp('space to the two dimensional PCA subspace of the data.');
Daniel@0 59 disp(' ')
Daniel@0 60 disp('Press any key to continue.');
Daniel@0 61 pause;
Daniel@0 62
Daniel@0 63 % Create and initialise GTM model
Daniel@0 64 net = gtm(latent_dim, nlatent, data_dim, num_rbf_centres, ...
Daniel@0 65 'gaussian', 0.1);
Daniel@0 66
Daniel@0 67 options = foptions;
Daniel@0 68 options(1) = -1;
Daniel@0 69 options(7) = 1; % Set width factor of RBF
Daniel@0 70 net = gtminit(net, options, data, 'regular', latent_shape, [4 4]);
Daniel@0 71
Daniel@0 72 options = foptions;
Daniel@0 73 options(14) = 30;
Daniel@0 74 options(1) = 1;
Daniel@0 75
Daniel@0 76 clc;
Daniel@0 77 dstring = ['We now train the model with ', num2str(options(14)), ...
Daniel@0 78 ' iterations of'];
Daniel@0 79 disp(dstring)
Daniel@0 80 disp('the EM algorithm for the GTM.')
Daniel@0 81 disp(' ')
Daniel@0 82 disp('Press any key to continue.')
Daniel@0 83 pause;
Daniel@0 84
Daniel@0 85 [net, options] = gtmem(net, data, options);
Daniel@0 86
Daniel@0 87 disp(' ')
Daniel@0 88 disp('Press any key to continue.')
Daniel@0 89 pause;
Daniel@0 90
Daniel@0 91 clc;
Daniel@0 92 disp('We now visualise the data by plotting, for each data point,');
Daniel@0 93 disp('the posterior mean and mode (in latent space). These give');
Daniel@0 94 disp('a summary of the entire posterior distribution in latent space.')
Daniel@0 95 disp('The corresponding values are joined by a line to aid the')
Daniel@0 96 disp('interpretation.')
Daniel@0 97 disp(' ')
Daniel@0 98 disp('Press any key to continue.');
Daniel@0 99 pause;
Daniel@0 100 % Plot posterior means
Daniel@0 101 means = gtmlmean(net, data);
Daniel@0 102 modes = gtmlmode(net, data);
Daniel@0 103 PointSize = 12;
Daniel@0 104 ClassSymbol1 = 'r.';
Daniel@0 105 ClassSymbol2 = 'b.';
Daniel@0 106 fh1 = figure;
Daniel@0 107 hold on;
Daniel@0 108 title('Visualisation in latent space')
Daniel@0 109 plot(means((labels==1),1), means(labels==1,2), ...
Daniel@0 110 ClassSymbol1, 'MarkerSize', PointSize)
Daniel@0 111 plot(means((labels>1),1),means(labels>1,2),...
Daniel@0 112 ClassSymbol2, 'MarkerSize', PointSize)
Daniel@0 113
Daniel@0 114 ClassSymbol1 = 'ro';
Daniel@0 115 ClassSymbol2 = 'bo';
Daniel@0 116 plot(modes(labels==1,1), modes(labels==1,2), ...
Daniel@0 117 ClassSymbol1)
Daniel@0 118 plot(modes(labels>1,1),modes(labels>1,2),...
Daniel@0 119 ClassSymbol2)
Daniel@0 120
Daniel@0 121 % Join up means and modes
Daniel@0 122 for n = 1:ndata
Daniel@0 123 plot([means(n,1); modes(n,1)], [means(n,2); modes(n,2)], 'g-')
Daniel@0 124 end
Daniel@0 125 % Place legend outside data plot
Daniel@0 126 legend('Mean (class 1)', 'Mean (class 2)', 'Mode (class 1)',...
Daniel@0 127 'Mode (class 2)', -1);
Daniel@0 128
Daniel@0 129 % Display posterior for a data point
Daniel@0 130 % Choose an interesting one with a large distance between mean and
Daniel@0 131 % mode
Daniel@0 132 [distance, point] = max(sum((means-modes).^2, 2));
Daniel@0 133 resp = gtmpost(net, data(point, :));
Daniel@0 134
Daniel@0 135 disp(' ')
Daniel@0 136 disp('For more detailed information, the full posterior distribution')
Daniel@0 137 disp('(or responsibility) can be plotted in latent space for a')
Daniel@0 138 disp('single data point. This point has been chosen as the one')
Daniel@0 139 disp('with the largest distance between mean and mode.')
Daniel@0 140 disp(' ')
Daniel@0 141 disp('Press any key to continue.');
Daniel@0 142 pause;
Daniel@0 143
Daniel@0 144 R = reshape(resp, fliplr(latent_shape));
Daniel@0 145 XL = reshape(net.X(:,1), fliplr(latent_shape));
Daniel@0 146 YL = reshape(net.X(:,2), fliplr(latent_shape));
Daniel@0 147
Daniel@0 148 fh2 = figure;
Daniel@0 149 imagesc(net.X(:, 1), net.X(:,2), R);
Daniel@0 150 hold on;
Daniel@0 151 tstr = ['Responsibility for point ', num2str(point)];
Daniel@0 152 title(tstr);
Daniel@0 153 set(gca,'YDir','normal')
Daniel@0 154 colormap(hot);
Daniel@0 155 colorbar
Daniel@0 156 disp(' ');
Daniel@0 157 disp('Press any key to continue.')
Daniel@0 158 pause
Daniel@0 159
Daniel@0 160 clc
Daniel@0 161 disp('Finally, we visualise the data with the posterior means in')
Daniel@0 162 disp('latent space as before, but superimpose the magnification')
Daniel@0 163 disp('factors to highlight the separation between clusters.')
Daniel@0 164 disp(' ')
Daniel@0 165 disp('Note the large magnitude factors down the centre of the')
Daniel@0 166 disp('graph, showing that the manifold is stretched more in')
Daniel@0 167 disp('this region than within each of the two clusters.')
Daniel@0 168 ClassSymbol1 = 'g.';
Daniel@0 169 ClassSymbol2 = 'b.';
Daniel@0 170
Daniel@0 171 fh3 = figure;
Daniel@0 172 mags = gtmmag(net, net.X);
Daniel@0 173 % Reshape into grid form
Daniel@0 174 Mags = reshape(mags, fliplr(latent_shape));
Daniel@0 175 imagesc(net.X(:, 1), net.X(:,2), Mags);
Daniel@0 176 hold on
Daniel@0 177 title('Dataset visualisation with magnification factors')
Daniel@0 178 set(gca,'YDir','normal')
Daniel@0 179 colormap(hot);
Daniel@0 180 colorbar
Daniel@0 181 hold on; % Else the magnification plot disappears
Daniel@0 182 plot(means(labels==1,1), means(labels==1,2), ...
Daniel@0 183 ClassSymbol1, 'MarkerSize', PointSize)
Daniel@0 184 plot(means(labels>1,1), means(labels>1,2), ...
Daniel@0 185 ClassSymbol2, 'MarkerSize', PointSize)
Daniel@0 186
Daniel@0 187 disp(' ')
Daniel@0 188 disp('Press any key to exit.')
Daniel@0 189 pause
Daniel@0 190
Daniel@0 191 close(fh1);
Daniel@0 192 close(fh2);
Daniel@0 193 close(fh3);
Daniel@0 194 clear all;