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