wolffd@0
|
1 %DEMMLP1 Demonstrate simple regression using a multi-layer perceptron
|
wolffd@0
|
2 %
|
wolffd@0
|
3 % Description
|
wolffd@0
|
4 % The problem consists of one input variable X and one target variable
|
wolffd@0
|
5 % T with data generated by sampling X at equal intervals and then
|
wolffd@0
|
6 % generating target data by computing SIN(2*PI*X) and adding Gaussian
|
wolffd@0
|
7 % noise. A 2-layer network with linear outputs is trained by minimizing
|
wolffd@0
|
8 % a sum-of-squares error function using the scaled conjugate gradient
|
wolffd@0
|
9 % optimizer.
|
wolffd@0
|
10 %
|
wolffd@0
|
11 % See also
|
wolffd@0
|
12 % MLP, MLPERR, MLPGRAD, SCG
|
wolffd@0
|
13 %
|
wolffd@0
|
14
|
wolffd@0
|
15 % Copyright (c) Ian T Nabney (1996-2001)
|
wolffd@0
|
16
|
wolffd@0
|
17
|
wolffd@0
|
18 % Generate the matrix of inputs x and targets t.
|
wolffd@0
|
19
|
wolffd@0
|
20 ndata = 20; % Number of data points.
|
wolffd@0
|
21 noise = 0.2; % Standard deviation of noise distribution.
|
wolffd@0
|
22 x = [0:1/(ndata - 1):1]';
|
wolffd@0
|
23 randn('state', 1);
|
wolffd@0
|
24 t = sin(2*pi*x) + noise*randn(ndata, 1);
|
wolffd@0
|
25
|
wolffd@0
|
26 clc
|
wolffd@0
|
27 disp('This demonstration illustrates the use of a Multi-Layer Perceptron')
|
wolffd@0
|
28 disp('network for regression problems. The data is generated from a noisy')
|
wolffd@0
|
29 disp('sine function.')
|
wolffd@0
|
30 disp(' ')
|
wolffd@0
|
31 disp('Press any key to continue.')
|
wolffd@0
|
32 pause
|
wolffd@0
|
33
|
wolffd@0
|
34 % Set up network parameters.
|
wolffd@0
|
35 nin = 1; % Number of inputs.
|
wolffd@0
|
36 nhidden = 3; % Number of hidden units.
|
wolffd@0
|
37 nout = 1; % Number of outputs.
|
wolffd@0
|
38 alpha = 0.01; % Coefficient of weight-decay prior.
|
wolffd@0
|
39
|
wolffd@0
|
40 % Create and initialize network weight vector.
|
wolffd@0
|
41
|
wolffd@0
|
42 net = mlp(nin, nhidden, nout, 'linear', alpha);
|
wolffd@0
|
43
|
wolffd@0
|
44 % Set up vector of options for the optimiser.
|
wolffd@0
|
45
|
wolffd@0
|
46 options = zeros(1,18);
|
wolffd@0
|
47 options(1) = 1; % This provides display of error values.
|
wolffd@0
|
48 options(14) = 100; % Number of training cycles.
|
wolffd@0
|
49
|
wolffd@0
|
50 clc
|
wolffd@0
|
51 disp(['The network has ', num2str(nhidden), ' hidden units and a weight decay'])
|
wolffd@0
|
52 disp(['coefficient of ', num2str(alpha), '.'])
|
wolffd@0
|
53 disp(' ')
|
wolffd@0
|
54 disp('After initializing the network, we train it use the scaled conjugate')
|
wolffd@0
|
55 disp('gradients algorithm for 100 cycles.')
|
wolffd@0
|
56 disp(' ')
|
wolffd@0
|
57 disp('Press any key to continue')
|
wolffd@0
|
58 pause
|
wolffd@0
|
59
|
wolffd@0
|
60 % Train using scaled conjugate gradients.
|
wolffd@0
|
61 [net, options] = netopt(net, options, x, t, 'scg');
|
wolffd@0
|
62
|
wolffd@0
|
63 disp(' ')
|
wolffd@0
|
64 disp('Now we plot the data, underlying function, and network outputs')
|
wolffd@0
|
65 disp('on a single graph to compare the results.')
|
wolffd@0
|
66 disp(' ')
|
wolffd@0
|
67 disp('Press any key to continue.')
|
wolffd@0
|
68 pause
|
wolffd@0
|
69
|
wolffd@0
|
70 % Plot the data, the original function, and the trained network function.
|
wolffd@0
|
71 plotvals = [0:0.01:1]';
|
wolffd@0
|
72 y = mlpfwd(net, plotvals);
|
wolffd@0
|
73 fh1 = figure;
|
wolffd@0
|
74 plot(x, t, 'ob')
|
wolffd@0
|
75 hold on
|
wolffd@0
|
76 xlabel('Input')
|
wolffd@0
|
77 ylabel('Target')
|
wolffd@0
|
78 axis([0 1 -1.5 1.5])
|
wolffd@0
|
79 [fx, fy] = fplot('sin(2*pi*x)', [0 1]);
|
wolffd@0
|
80 plot(fx, fy, '-r', 'LineWidth', 2)
|
wolffd@0
|
81 plot(plotvals, y, '-k', 'LineWidth', 2)
|
wolffd@0
|
82 legend('data', 'function', 'network');
|
wolffd@0
|
83
|
wolffd@0
|
84 disp(' ')
|
wolffd@0
|
85 disp('Press any key to end.')
|
wolffd@0
|
86 pause
|
wolffd@0
|
87 close(fh1);
|
wolffd@0
|
88 clear all;
|