annotate toolboxes/FullBNT-1.0.7/netlab3.3/demhmc2.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 %DEMHMC2 Demonstrate Bayesian regression with Hybrid Monte Carlo sampling.
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 X at equal intervals and then
Daniel@0 6 % generating target data by computing SIN(2*PI*X) and adding Gaussian
Daniel@0 7 % noise. The model is a 2-layer network with linear outputs, and the
Daniel@0 8 % hybrid Monte Carlo algorithm (without persistence) is used to sample
Daniel@0 9 % from the posterior distribution of the weights. The graph shows the
Daniel@0 10 % underlying function, 100 samples from the function given by the
Daniel@0 11 % posterior distribution of the weights, and the average prediction
Daniel@0 12 % (weighted by the posterior probabilities).
Daniel@0 13 %
Daniel@0 14 % See also
Daniel@0 15 % DEMHMC3, HMC, MLP, MLPERR, MLPGRAD
Daniel@0 16 %
Daniel@0 17
Daniel@0 18 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 19
Daniel@0 20
Daniel@0 21 % Generate the matrix of inputs x and targets t.
Daniel@0 22 ndata = 20; % Number of data points.
Daniel@0 23 noise = 0.1; % Standard deviation of noise distribution.
Daniel@0 24 nin = 1; % Number of inputs.
Daniel@0 25 nout = 1; % Number of outputs.
Daniel@0 26
Daniel@0 27 seed = 42; % Seed for random weight initialization.
Daniel@0 28 randn('state', seed);
Daniel@0 29 rand('state', seed);
Daniel@0 30
Daniel@0 31 x = 0.25 + 0.1*randn(ndata, nin);
Daniel@0 32 t = sin(2*pi*x) + noise*randn(size(x));
Daniel@0 33
Daniel@0 34 clc
Daniel@0 35 disp('This demonstration illustrates the use of the hybrid Monte Carlo')
Daniel@0 36 disp('algorithm to sample from the posterior weight distribution of a')
Daniel@0 37 disp('multi-layer perceptron.')
Daniel@0 38 disp(' ')
Daniel@0 39 disp('A regression problem is used, with the one-dimensional data drawn')
Daniel@0 40 disp('from a noisy sine function. The x values are sampled from a normal')
Daniel@0 41 disp('distribution with mean 0.25 and variance 0.01.')
Daniel@0 42 disp(' ')
Daniel@0 43 disp('First we initialise the network.')
Daniel@0 44 disp(' ')
Daniel@0 45 disp('Press any key to continue.')
Daniel@0 46 pause
Daniel@0 47
Daniel@0 48 % Set up network parameters.
Daniel@0 49 nhidden = 5; % Number of hidden units.
Daniel@0 50 alpha = 0.001; % Coefficient of weight-decay prior.
Daniel@0 51 beta = 100.0; % Coefficient of data error.
Daniel@0 52
Daniel@0 53 % Create and initialize network model.
Daniel@0 54 % Initialise weights reasonably close to 0
Daniel@0 55 net = mlp(nin, nhidden, nout, 'linear', alpha, beta);
Daniel@0 56 net = mlpinit(net, 10);
Daniel@0 57
Daniel@0 58 clc
Daniel@0 59 disp('Next we take 100 samples from the posterior distribution. The first')
Daniel@0 60 disp('200 samples at the start of the chain are omitted. As persistence')
Daniel@0 61 disp('is not used, the momentum is randomised at each step. 100 iterations')
Daniel@0 62 disp('are used at each step. The new state is accepted if the threshold')
Daniel@0 63 disp('value is greater than a random number between 0 and 1.')
Daniel@0 64 disp(' ')
Daniel@0 65 disp('Negative step numbers indicate samples discarded from the start of the')
Daniel@0 66 disp('chain.')
Daniel@0 67 disp(' ')
Daniel@0 68 disp('Press any key to continue.')
Daniel@0 69 pause
Daniel@0 70 % Set up vector of options for hybrid Monte Carlo.
Daniel@0 71 nsamples = 100; % Number of retained samples.
Daniel@0 72
Daniel@0 73 options = foptions; % Default options vector.
Daniel@0 74 options(1) = 1; % Switch on diagnostics.
Daniel@0 75 options(7) = 100; % Number of steps in trajectory.
Daniel@0 76 options(14) = nsamples; % Number of Monte Carlo samples returned.
Daniel@0 77 options(15) = 200; % Number of samples omitted at start of chain.
Daniel@0 78 options(18) = 0.002; % Step size.
Daniel@0 79
Daniel@0 80 w = mlppak(net);
Daniel@0 81 % Initialise HMC
Daniel@0 82 hmc('state', 42);
Daniel@0 83 [samples, energies] = hmc('neterr', w, options, 'netgrad', net, x, t);
Daniel@0 84
Daniel@0 85 clc
Daniel@0 86 disp('The plot shows the underlying noise free function, the 100 samples')
Daniel@0 87 disp('produced from the MLP, and their average as a Monte Carlo estimate')
Daniel@0 88 disp('of the true posterior average.')
Daniel@0 89 disp(' ')
Daniel@0 90 disp('Press any key to continue.')
Daniel@0 91 pause
Daniel@0 92 nplot = 300;
Daniel@0 93 plotvals = [0 : 1/(nplot - 1) : 1]';
Daniel@0 94 pred = zeros(size(plotvals));
Daniel@0 95 fh = figure;
Daniel@0 96 for k = 1:nsamples
Daniel@0 97 w2 = samples(k,:);
Daniel@0 98 net2 = mlpunpak(net, w2);
Daniel@0 99 y = mlpfwd(net2, plotvals);
Daniel@0 100 % Average sample predictions as Monte Carlo estimate of true integral
Daniel@0 101 pred = pred + y;
Daniel@0 102 h4 = plot(plotvals, y, '-r', 'LineWidth', 1);
Daniel@0 103 if k == 1
Daniel@0 104 hold on
Daniel@0 105 end
Daniel@0 106 end
Daniel@0 107 pred = pred./nsamples;
Daniel@0 108
Daniel@0 109 % Plot data
Daniel@0 110 h1 = plot(x, t, 'ob', 'LineWidth', 2, 'MarkerFaceColor', 'blue');
Daniel@0 111 axis([0 1 -3 3])
Daniel@0 112
Daniel@0 113 % Plot function
Daniel@0 114 [fx, fy] = fplot('sin(2*pi*x)', [0 1], '--g');
Daniel@0 115 h2 = plot(fx, fy, '--g', 'LineWidth', 2);
Daniel@0 116 set(gca, 'box', 'on');
Daniel@0 117
Daniel@0 118 % Plot averaged prediction
Daniel@0 119 h3 = plot(plotvals, pred, '-c', 'LineWidth', 2);
Daniel@0 120 hold off
Daniel@0 121
Daniel@0 122 lstrings = char('Data', 'Function', 'Prediction', 'Samples');
Daniel@0 123 legend([h1 h2 h3 h4], lstrings, 3);
Daniel@0 124
Daniel@0 125 disp('Note how the predictions become much further from the true function')
Daniel@0 126 disp('away from the region of high data density.')
Daniel@0 127 disp(' ')
Daniel@0 128 disp('Press any key to exit.')
Daniel@0 129 pause
Daniel@0 130 close(fh);
Daniel@0 131 clear all;
Daniel@0 132