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