annotate toolboxes/FullBNT-1.0.7/netlab3.3/demmdn1.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 %DEMMDN1 Demonstrate fitting a multi-valued function using a Mixture Density Network.
Daniel@0 2 %
Daniel@0 3 % Description
Daniel@0 4 % The problem consists of one input variable X and one target variable
Daniel@0 5 % T with data generated by sampling T at equal intervals and then
Daniel@0 6 % generating target data by computing T + 0.3*SIN(2*PI*T) and adding
Daniel@0 7 % Gaussian noise. A Mixture Density Network with 3 centres in the
Daniel@0 8 % mixture model is trained by minimizing a negative log likelihood
Daniel@0 9 % error function using the scaled conjugate gradient optimizer.
Daniel@0 10 %
Daniel@0 11 % The conditional means, mixing coefficients and variances are plotted
Daniel@0 12 % as a function of X, and a contour plot of the full conditional
Daniel@0 13 % density is also generated.
Daniel@0 14 %
Daniel@0 15 % See also
Daniel@0 16 % MDN, MDNERR, MDNGRAD, SCG
Daniel@0 17 %
Daniel@0 18
Daniel@0 19 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 20
Daniel@0 21
Daniel@0 22 % Generate the matrix of inputs x and targets t.
Daniel@0 23 seedn = 42;
Daniel@0 24 seed = 42;
Daniel@0 25 randn('state', seedn);
Daniel@0 26 rand('state', seed);
Daniel@0 27 ndata = 300; % Number of data points.
Daniel@0 28 noise = 0.2; % Range of noise distribution.
Daniel@0 29 t = [0:1/(ndata - 1):1]';
Daniel@0 30 x = t + 0.3*sin(2*pi*t) + noise*rand(ndata, 1) - noise/2;
Daniel@0 31 axis_limits = [-0.2 1.2 -0.2 1.2];
Daniel@0 32
Daniel@0 33 clc
Daniel@0 34 disp('This demonstration illustrates the use of a Mixture Density Network')
Daniel@0 35 disp('to model multi-valued functions. The data is generated from the')
Daniel@0 36 disp('mapping x = t + 0.3 sin(2 pi t) + e, where e is a noise term.')
Daniel@0 37 disp('We begin by plotting the data.')
Daniel@0 38 disp(' ')
Daniel@0 39 disp('Press any key to continue')
Daniel@0 40 pause
Daniel@0 41 % Plot the data
Daniel@0 42 fh1 = figure;
Daniel@0 43 p1 = plot(x, t, 'ob');
Daniel@0 44 axis(axis_limits);
Daniel@0 45 hold on
Daniel@0 46 disp('Note that for x in the range 0.35 to 0.65, there are three possible')
Daniel@0 47 disp('branches of the function.')
Daniel@0 48 disp(' ')
Daniel@0 49 disp('Press any key to continue')
Daniel@0 50 pause
Daniel@0 51
Daniel@0 52 % Set up network parameters.
Daniel@0 53 nin = 1; % Number of inputs.
Daniel@0 54 nhidden = 5; % Number of hidden units.
Daniel@0 55 ncentres = 3; % Number of mixture components.
Daniel@0 56 dim_target = 1; % Dimension of target space
Daniel@0 57 mdntype = '0'; % Currently unused: reserved for future use
Daniel@0 58 alpha = 100; % Inverse variance for weight initialisation
Daniel@0 59 % Make variance small for good starting point
Daniel@0 60
Daniel@0 61 % Create and initialize network weight vector.
Daniel@0 62 net = mdn(nin, nhidden, ncentres, dim_target, mdntype);
Daniel@0 63 init_options = zeros(1, 18);
Daniel@0 64 init_options(1) = -1; % Suppress all messages
Daniel@0 65 init_options(14) = 10; % 10 iterations of K means in gmminit
Daniel@0 66 net = mdninit(net, alpha, t, init_options);
Daniel@0 67
Daniel@0 68 % Set up vector of options for the optimiser.
Daniel@0 69 options = foptions;
Daniel@0 70 options(1) = 1; % This provides display of error values.
Daniel@0 71 options(14) = 200; % Number of training cycles.
Daniel@0 72
Daniel@0 73 clc
Daniel@0 74 disp('We initialise the neural network model, which is an MLP with a')
Daniel@0 75 disp('Gaussian mixture model with three components and spherical variance')
Daniel@0 76 disp('as the error function. This enables us to model the complete')
Daniel@0 77 disp('conditional density function.')
Daniel@0 78 disp(' ')
Daniel@0 79 disp('Next we train the model for 200 epochs using a scaled conjugate gradient')
Daniel@0 80 disp('optimizer. The error function is the negative log likelihood of the')
Daniel@0 81 disp('training data.')
Daniel@0 82 disp(' ')
Daniel@0 83 disp('Press any key to continue.')
Daniel@0 84 pause
Daniel@0 85
Daniel@0 86 % Train using scaled conjugate gradients.
Daniel@0 87 [net, options] = netopt(net, options, x, t, 'scg');
Daniel@0 88
Daniel@0 89 disp(' ')
Daniel@0 90 disp('Press any key to continue.')
Daniel@0 91 pause
Daniel@0 92
Daniel@0 93 clc
Daniel@0 94 disp('We can also train a conventional MLP with sum of squares error function.')
Daniel@0 95 disp('This will approximate the conditional mean, which is not always a')
Daniel@0 96 disp('good representation of the data. Note that the error function is the')
Daniel@0 97 disp('sum of squares error on the training data, which accounts for the')
Daniel@0 98 disp('different values from training the MDN.')
Daniel@0 99 disp(' ')
Daniel@0 100 disp('We train the network with the quasi-Newton optimizer for 80 epochs.')
Daniel@0 101 disp(' ')
Daniel@0 102 disp('Press any key to continue.')
Daniel@0 103 pause
Daniel@0 104 mlp_nhidden = 8;
Daniel@0 105 net2 = mlp(nin, mlp_nhidden, dim_target, 'linear');
Daniel@0 106 options(14) = 80;
Daniel@0 107 [net2, options] = netopt(net2, options, x, t, 'quasinew');
Daniel@0 108 disp(' ')
Daniel@0 109 disp('Press any key to continue.')
Daniel@0 110 pause
Daniel@0 111
Daniel@0 112 clc
Daniel@0 113 disp('Now we plot the underlying function, the MDN prediction,')
Daniel@0 114 disp('represented by the mode of the conditional distribution, and the')
Daniel@0 115 disp('prediction of the conventional MLP.')
Daniel@0 116 disp(' ')
Daniel@0 117 disp('Press any key to continue.')
Daniel@0 118 pause
Daniel@0 119
Daniel@0 120 % Plot the original function, and the trained network function.
Daniel@0 121 plotvals = [0:0.01:1]';
Daniel@0 122 mixes = mdn2gmm(mdnfwd(net, plotvals));
Daniel@0 123 axis(axis_limits);
Daniel@0 124 yplot = t+0.3*sin(2*pi*t);
Daniel@0 125 p2 = plot(yplot, t, '--y');
Daniel@0 126
Daniel@0 127 % Use the mode to represent the function
Daniel@0 128 y = zeros(1, length(plotvals));
Daniel@0 129 priors = zeros(length(plotvals), ncentres);
Daniel@0 130 c = zeros(length(plotvals), 3);
Daniel@0 131 widths = zeros(length(plotvals), ncentres);
Daniel@0 132 for i = 1:length(plotvals)
Daniel@0 133 [m, j] = max(mixes(i).priors);
Daniel@0 134 y(i) = mixes(i).centres(j,:);
Daniel@0 135 c(i,:) = mixes(i).centres';
Daniel@0 136 end
Daniel@0 137 p3 = plot(plotvals, y, '*r');
Daniel@0 138 p4 = plot(plotvals, mlpfwd(net2, plotvals), 'g');
Daniel@0 139 set(p4, 'LineWidth', 2);
Daniel@0 140 legend([p1 p2 p3 p4], 'data', 'function', 'MDN mode', 'MLP mean', 4);
Daniel@0 141 hold off
Daniel@0 142
Daniel@0 143 clc
Daniel@0 144 disp('We can also plot how the mixture model parameters depend on x.')
Daniel@0 145 disp('First we plot the mixture centres, then the priors and finally')
Daniel@0 146 disp('the variances.')
Daniel@0 147 disp(' ')
Daniel@0 148 disp('Press any key to continue.')
Daniel@0 149 pause
Daniel@0 150 fh2 = figure;
Daniel@0 151 subplot(3, 1, 1)
Daniel@0 152 plot(plotvals, c)
Daniel@0 153 hold on
Daniel@0 154 title('Mixture centres')
Daniel@0 155 legend('centre 1', 'centre 2', 'centre 3')
Daniel@0 156 hold off
Daniel@0 157
Daniel@0 158 priors = reshape([mixes.priors], mixes(1).ncentres, size(mixes, 2))';
Daniel@0 159 %%fh3 = figure;
Daniel@0 160 subplot(3, 1, 2)
Daniel@0 161 plot(plotvals, priors)
Daniel@0 162 hold on
Daniel@0 163 title('Mixture priors')
Daniel@0 164 legend('centre 1', 'centre 2', 'centre 3')
Daniel@0 165 hold off
Daniel@0 166
Daniel@0 167 variances = reshape([mixes.covars], mixes(1).ncentres, size(mixes, 2))';
Daniel@0 168 %%fh4 = figure;
Daniel@0 169 subplot(3, 1, 3)
Daniel@0 170 plot(plotvals, variances)
Daniel@0 171 hold on
Daniel@0 172 title('Mixture variances')
Daniel@0 173 legend('centre 1', 'centre 2', 'centre 3')
Daniel@0 174 hold off
Daniel@0 175
Daniel@0 176 disp('The last figure is a contour plot of the conditional probability')
Daniel@0 177 disp('density generated by the Mixture Density Network. Note how it')
Daniel@0 178 disp('is well matched to the regions of high data density.')
Daniel@0 179 disp(' ')
Daniel@0 180 disp('Press any key to continue.')
Daniel@0 181 pause
Daniel@0 182 % Contour plot for MDN.
Daniel@0 183 i = 0:0.01:1.0;
Daniel@0 184 j = 0:0.01:1.0;
Daniel@0 185
Daniel@0 186 [I, J] = meshgrid(i,j);
Daniel@0 187 I = I(:);
Daniel@0 188 J = J(:);
Daniel@0 189 li = length(i);
Daniel@0 190 lj = length(j);
Daniel@0 191 Z = zeros(li, lj);
Daniel@0 192 for k = 1:li;
Daniel@0 193 Z(:,k) = gmmprob(mixes(k), j');
Daniel@0 194 end
Daniel@0 195 fh5 = figure;
Daniel@0 196 % Set up levels by hand to make a good figure
Daniel@0 197 v = [2 2.5 3 3.5 5:3:18];
Daniel@0 198 contour(i, j, Z, v)
Daniel@0 199 hold on
Daniel@0 200 title('Contour plot of conditional density')
Daniel@0 201 hold off
Daniel@0 202
Daniel@0 203 disp(' ')
Daniel@0 204 disp('Press any key to exit.')
Daniel@0 205 pause
Daniel@0 206 close(fh1);
Daniel@0 207 close(fh2);
Daniel@0 208 %%close(fh3);
Daniel@0 209 %%close(fh4);
Daniel@0 210 close(fh5);
Daniel@0 211 %%clear all;