annotate toolboxes/FullBNT-1.0.7/netlab3.3/demgtm1.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 %DEMGTM1 Demonstrate EM for GTM.
Daniel@0 2 %
Daniel@0 3 % Description
Daniel@0 4 % This script demonstrates the use of the EM algorithm to fit a one-
Daniel@0 5 % dimensional GTM to a two-dimensional set of data using maximum
Daniel@0 6 % likelihood. The location and spread of the Gaussian kernels in the
Daniel@0 7 % data space is shown during training.
Daniel@0 8 %
Daniel@0 9 % See also
Daniel@0 10 % DEMGTM2, 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 % Demonstrates the GTM with a 2D target space and a 1D latent space.
Daniel@0 16 %
Daniel@0 17 % This script generates a simple data set in 2 dimensions,
Daniel@0 18 % with an intrinsic dimensionality of 1, and trains a GTM
Daniel@0 19 % with a 1-dimensional latent variable to model this data
Daniel@0 20 % set, visually illustrating the training process
Daniel@0 21 %
Daniel@0 22 % Synopsis: gtm_demo
Daniel@0 23
Daniel@0 24 % Generate and plot a 2D data set
Daniel@0 25
Daniel@0 26 data_min = 0.15;
Daniel@0 27 data_max = 3.05;
Daniel@0 28 T = [data_min:0.05:data_max]';
Daniel@0 29 T = [T (T + 1.25*sin(2*T))];
Daniel@0 30 fh1 = figure;
Daniel@0 31 plot(T(:,1), T(:,2), 'ro');
Daniel@0 32 axis([data_min-0.05 data_max+0.05 data_min-0.05 data_max+0.05]);
Daniel@0 33 clc;
Daniel@0 34 disp('This demonstration shows in detail how the EM algorithm works')
Daniel@0 35 disp('for training a GTM with a one dimensional latent space.')
Daniel@0 36 disp(' ')
Daniel@0 37 fprintf([...
Daniel@0 38 'The figure shows data generated by feeding a 1D uniform distribution\n', ...
Daniel@0 39 '(on the X-axis) through a non-linear function (y = x + 1.25*sin(2*x))\n', ...
Daniel@0 40 '\nPress any key to continue ...\n\n']);
Daniel@0 41 pause;
Daniel@0 42
Daniel@0 43 % Generate a unit circle figure, to be used for plotting
Daniel@0 44 src = [0:(2*pi)/(20-1):2*pi]';
Daniel@0 45 unitC = [sin(src) cos(src)];
Daniel@0 46
Daniel@0 47 % Generate and plot (along with the data) an initial GTM model
Daniel@0 48
Daniel@0 49 clc;
Daniel@0 50 num_latent_points = 20;
Daniel@0 51 num_rbf_centres = 5;
Daniel@0 52
Daniel@0 53 net = gtm(1, num_latent_points, 2, num_rbf_centres, 'gaussian');
Daniel@0 54
Daniel@0 55 options = zeros(1, 18);
Daniel@0 56 options(7) = 1;
Daniel@0 57 net = gtminit(net, options, T, 'regular', num_latent_points, ...
Daniel@0 58 num_rbf_centres);
Daniel@0 59
Daniel@0 60 mix = gtmfwd(net);
Daniel@0 61 % Replot the figure
Daniel@0 62 hold off;
Daniel@0 63 plot(mix.centres(:,1), mix.centres(:,2), 'g');
Daniel@0 64 hold on;
Daniel@0 65 for i=1:num_latent_points
Daniel@0 66 c = 2*unitC*sqrt(mix.covars(1)) + [ones(20,1)*mix.centres(i,1) ...
Daniel@0 67 ones(num_latent_points,1)*mix.centres(i,2)];
Daniel@0 68 fill(c(:,1), c(:,2), [0.8 1 0.8]);
Daniel@0 69 end
Daniel@0 70 plot(T(:,1), T(:,2), 'ro');
Daniel@0 71 plot(mix.centres(:,1), mix.centres(:,2), 'g+');
Daniel@0 72 plot(mix.centres(:,1), mix.centres(:,2), 'g');
Daniel@0 73 axis([data_min-0.05 data_max+0.05 data_min-0.05 data_max+0.05]);
Daniel@0 74 drawnow;
Daniel@0 75 title('Initial configuration');
Daniel@0 76 disp(' ')
Daniel@0 77 fprintf([...
Daniel@0 78 'The figure shows the starting point for the GTM, before the training.\n', ...
Daniel@0 79 'A discrete latent variable distribution of %d points in 1 dimension \n', ...
Daniel@0 80 'is mapped to the 1st principal component of the target data by an RBF.\n', ...
Daniel@0 81 'with %d basis functions. Each of the %d points defines the centre of\n', ...
Daniel@0 82 'a Gaussian in a Gaussian mixture, marked by the green ''+''-signs. The\n', ...
Daniel@0 83 'mixture components all have equal variance, illustrated by the filled\n', ...
Daniel@0 84 'circle around each ''+''-sign, the radii corresponding to 2 standard\n', ...
Daniel@0 85 'deviations. The ''+''-signs are connected with a line according to their\n', ...
Daniel@0 86 'corresponding ordering in latent space.\n\n', ...
Daniel@0 87 'Press any key to begin training ...\n\n'], num_latent_points, ...
Daniel@0 88 num_rbf_centres, num_latent_points);
Daniel@0 89 pause;
Daniel@0 90
Daniel@0 91 figure(fh1);
Daniel@0 92 %%%% Train the GTM and plot it (along with the data) as training proceeds %%%%
Daniel@0 93 options = foptions;
Daniel@0 94 options(1) = -1; % Turn off all warning messages
Daniel@0 95 options(14) = 1;
Daniel@0 96 for j = 1:15
Daniel@0 97 [net, options] = gtmem(net, T, options);
Daniel@0 98 hold off;
Daniel@0 99 mix = gtmfwd(net);
Daniel@0 100 plot(mix.centres(:,1), mix.centres(:,2), 'g');
Daniel@0 101 hold on;
Daniel@0 102 for i=1:20
Daniel@0 103 c = 2*unitC*sqrt(mix.covars(1)) + [ones(20,1)*mix.centres(i,1) ...
Daniel@0 104 ones(20,1)*mix.centres(i,2)];
Daniel@0 105 fill(c(:,1), c(:,2), [0.8 1.0 0.8]);
Daniel@0 106 end
Daniel@0 107 plot(T(:,1), T(:,2), 'ro');
Daniel@0 108 plot(mix.centres(:,1), mix.centres(:,2), 'g+');
Daniel@0 109 plot(mix.centres(:,1), mix.centres(:,2), 'g');
Daniel@0 110 axis([0 3.5 0 3.5]);
Daniel@0 111 title(['After ', int2str(j),' iterations of training.']);
Daniel@0 112 drawnow;
Daniel@0 113 if (j == 4)
Daniel@0 114 fprintf([...
Daniel@0 115 'The GTM initially adapts relatively quickly - already after \n', ...
Daniel@0 116 '4 iterations of training, a rough fit is attained.\n\n', ...
Daniel@0 117 'Press any key to continue training ...\n\n']);
Daniel@0 118 pause;
Daniel@0 119 figure(fh1);
Daniel@0 120 elseif (j == 8)
Daniel@0 121 fprintf([...
Daniel@0 122 'After another 4 iterations of training: from now on further \n', ...
Daniel@0 123 'training only makes small changes to the mapping, which combined with \n', ...
Daniel@0 124 'decrements of the Gaussian mixture variance, optimize the fit in \n', ...
Daniel@0 125 'terms of likelihood.\n\n', ...
Daniel@0 126 'Press any key to continue training ...\n\n']);
Daniel@0 127 pause;
Daniel@0 128 figure(fh1);
Daniel@0 129 else
Daniel@0 130 pause(1);
Daniel@0 131 end
Daniel@0 132 end
Daniel@0 133
Daniel@0 134 clc;
Daniel@0 135 fprintf([...
Daniel@0 136 'After 15 iterations of training the GTM can be regarded as converged. \n', ...
Daniel@0 137 'Is has been adapted to fit the target data distribution as well \n', ...
Daniel@0 138 'as possible, given prior smoothness constraints on the mapping. It \n', ...
Daniel@0 139 'captures the fact that the probabilty density is higher at the two \n', ...
Daniel@0 140 'bends of the curve, and lower towards its end points.\n\n']);
Daniel@0 141 disp(' ');
Daniel@0 142 disp('Press any key to exit.');
Daniel@0 143 pause;
Daniel@0 144
Daniel@0 145 close(fh1);
Daniel@0 146 clear all;
Daniel@0 147