annotate toolboxes/FullBNT-1.0.7/netlab3.3/demrbf1.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 %DEMRBF1 Demonstrate simple regression using a radial basis function 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 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. This data is the same as that used in demmlp1.
Daniel@0 8 %
Daniel@0 9 % Three different RBF networks (with different activation functions)
Daniel@0 10 % are trained in two stages. First, a Gaussian mixture model is trained
Daniel@0 11 % using the EM algorithm, and the centres of this model are used to set
Daniel@0 12 % the centres of the RBF. Second, the output weights (and biases) are
Daniel@0 13 % determined using the pseudo-inverse of the design matrix.
Daniel@0 14 %
Daniel@0 15 % See also
Daniel@0 16 % DEMMLP1, RBF, RBFFWD, GMM, GMMEM
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 randn('state', 42);
Daniel@0 24 rand('state', 42);
Daniel@0 25 ndata = 20; % Number of data points.
Daniel@0 26 noise = 0.2; % Standard deviation of noise distribution.
Daniel@0 27 x = (linspace(0, 1, ndata))';
Daniel@0 28 t = sin(2*pi*x) + noise*randn(ndata, 1);
Daniel@0 29 mu = mean(x);
Daniel@0 30 sigma = std(x);
Daniel@0 31 tr_in = (x - mu)./(sigma);
Daniel@0 32
Daniel@0 33 clc
Daniel@0 34 disp('This demonstration illustrates the use of a Radial Basis Function')
Daniel@0 35 disp('network for regression problems. The data is generated from a noisy')
Daniel@0 36 disp('sine function.')
Daniel@0 37 disp(' ')
Daniel@0 38 disp('Press any key to continue.')
Daniel@0 39 pause
Daniel@0 40 % Set up network parameters.
Daniel@0 41 nin = 1; % Number of inputs.
Daniel@0 42 nhidden = 7; % Number of hidden units.
Daniel@0 43 nout = 1; % Number of outputs.
Daniel@0 44
Daniel@0 45 clc
Daniel@0 46 disp('We assess the effect of three different activation functions.')
Daniel@0 47 disp('First we create a network with Gaussian activations.')
Daniel@0 48 disp(' ')
Daniel@0 49 disp('Press any key to continue.')
Daniel@0 50 pause
Daniel@0 51 % Create and initialize network weight and parameter vectors.
Daniel@0 52 net = rbf(nin, nhidden, nout, 'gaussian');
Daniel@0 53
Daniel@0 54 disp('A two-stage training algorithm is used: it uses a small number of')
Daniel@0 55 disp('iterations of EM to position the centres, and then the pseudo-inverse')
Daniel@0 56 disp('of the design matrix to find the second layer weights.')
Daniel@0 57 disp(' ')
Daniel@0 58 disp('Press any key to continue.')
Daniel@0 59 pause
Daniel@0 60 disp('Error values from EM training.')
Daniel@0 61 % Use fast training method
Daniel@0 62 options = foptions;
Daniel@0 63 options(1) = 1; % Display EM training
Daniel@0 64 options(14) = 10; % number of iterations of EM
Daniel@0 65 net = rbftrain(net, options, tr_in, t);
Daniel@0 66
Daniel@0 67 disp(' ')
Daniel@0 68 disp('Press any key to continue.')
Daniel@0 69 pause
Daniel@0 70 clc
Daniel@0 71 disp('The second RBF network has thin plate spline activations.')
Daniel@0 72 disp('The same centres are used again, so we just need to calculate')
Daniel@0 73 disp('the second layer weights.')
Daniel@0 74 disp(' ')
Daniel@0 75 disp('Press any key to continue.')
Daniel@0 76 pause
Daniel@0 77 % Create a second RBF with thin plate spline functions
Daniel@0 78 net2 = rbf(nin, nhidden, nout, 'tps');
Daniel@0 79
Daniel@0 80 % Re-use previous centres rather than calling rbftrain again
Daniel@0 81 net2.c = net.c;
Daniel@0 82 [y, act2] = rbffwd(net2, tr_in);
Daniel@0 83
Daniel@0 84 % Solve for new output weights and biases from RBF activations
Daniel@0 85 temp = pinv([act2 ones(ndata, 1)]) * t;
Daniel@0 86 net2.w2 = temp(1:nhidden, :);
Daniel@0 87 net2.b2 = temp(nhidden+1, :);
Daniel@0 88
Daniel@0 89 disp('The third RBF network has r^4 log r activations.')
Daniel@0 90 disp(' ')
Daniel@0 91 disp('Press any key to continue.')
Daniel@0 92 pause
Daniel@0 93 % Create a third RBF with r^4 log r functions
Daniel@0 94 net3 = rbf(nin, nhidden, nout, 'r4logr');
Daniel@0 95
Daniel@0 96 % Overwrite weight vector with parameters from first RBF
Daniel@0 97 net3.c = net.c;
Daniel@0 98 [y, act3] = rbffwd(net3, tr_in);
Daniel@0 99 temp = pinv([act3 ones(ndata, 1)]) * t;
Daniel@0 100 net3.w2 = temp(1:nhidden, :);
Daniel@0 101 net3.b2 = temp(nhidden+1, :);
Daniel@0 102
Daniel@0 103 disp('Now we plot the data, underlying function, and network outputs')
Daniel@0 104 disp('on a single graph to compare the results.')
Daniel@0 105 disp(' ')
Daniel@0 106 disp('Press any key to continue.')
Daniel@0 107 pause
Daniel@0 108 % Plot the data, the original function, and the trained network functions.
Daniel@0 109 plotvals = [x(1):0.01:x(end)]';
Daniel@0 110 inputvals = (plotvals-mu)./sigma;
Daniel@0 111 y = rbffwd(net, inputvals);
Daniel@0 112 y2 = rbffwd(net2, inputvals);
Daniel@0 113 y3 = rbffwd(net3, inputvals);
Daniel@0 114 fh1 = figure;
Daniel@0 115
Daniel@0 116 plot(x, t, 'ob')
Daniel@0 117 hold on
Daniel@0 118 xlabel('Input')
Daniel@0 119 ylabel('Target')
Daniel@0 120 axis([x(1) x(end) -1.5 1.5])
Daniel@0 121 [fx, fy] = fplot('sin(2*pi*x)', [x(1) x(end)]);
Daniel@0 122 plot(fx, fy, '-r', 'LineWidth', 2)
Daniel@0 123 plot(plotvals, y, '--g', 'LineWidth', 2)
Daniel@0 124 plot(plotvals, y2, 'k--', 'LineWidth', 2)
Daniel@0 125 plot(plotvals, y3, '-.c', 'LineWidth', 2)
Daniel@0 126 legend('data', 'function', 'Gaussian RBF', 'Thin plate spline RBF', ...
Daniel@0 127 'r^4 log r RBF');
Daniel@0 128 hold off
Daniel@0 129
Daniel@0 130 disp('RBF training errors are');
Daniel@0 131 disp(['Gaussian ', num2str(rbferr(net, tr_in, t)), ' TPS ', ...
Daniel@0 132 num2str(rbferr(net2, tr_in, t)), ' R4logr ', num2str(rbferr(net3, tr_in, t))]);
Daniel@0 133
Daniel@0 134 disp(' ')
Daniel@0 135 disp('Press any key to end.')
Daniel@0 136 pause
Daniel@0 137 close(fh1);
Daniel@0 138 clear all;