annotate toolboxes/FullBNT-1.0.7/netlab3.3/mlp.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 function net = mlp(nin, nhidden, nout, outfunc, prior, beta)
Daniel@0 2 %MLP Create a 2-layer feedforward network.
Daniel@0 3 %
Daniel@0 4 % Description
Daniel@0 5 % NET = MLP(NIN, NHIDDEN, NOUT, FUNC) takes the number of inputs,
Daniel@0 6 % hidden units and output units for a 2-layer feed-forward network,
Daniel@0 7 % together with a string FUNC which specifies the output unit
Daniel@0 8 % activation function, and returns a data structure NET. The weights
Daniel@0 9 % are drawn from a zero mean, unit variance isotropic Gaussian, with
Daniel@0 10 % varianced scaled by the fan-in of the hidden or output units as
Daniel@0 11 % appropriate. This makes use of the Matlab function RANDN and so the
Daniel@0 12 % seed for the random weight initialization can be set using
Daniel@0 13 % RANDN('STATE', S) where S is the seed value. The hidden units use
Daniel@0 14 % the TANH activation function.
Daniel@0 15 %
Daniel@0 16 % The fields in NET are
Daniel@0 17 % type = 'mlp'
Daniel@0 18 % nin = number of inputs
Daniel@0 19 % nhidden = number of hidden units
Daniel@0 20 % nout = number of outputs
Daniel@0 21 % nwts = total number of weights and biases
Daniel@0 22 % actfn = string describing the output unit activation function:
Daniel@0 23 % 'linear'
Daniel@0 24 % 'logistic
Daniel@0 25 % 'softmax'
Daniel@0 26 % w1 = first-layer weight matrix
Daniel@0 27 % b1 = first-layer bias vector
Daniel@0 28 % w2 = second-layer weight matrix
Daniel@0 29 % b2 = second-layer bias vector
Daniel@0 30 % Here W1 has dimensions NIN times NHIDDEN, B1 has dimensions 1 times
Daniel@0 31 % NHIDDEN, W2 has dimensions NHIDDEN times NOUT, and B2 has dimensions
Daniel@0 32 % 1 times NOUT.
Daniel@0 33 %
Daniel@0 34 % NET = MLP(NIN, NHIDDEN, NOUT, FUNC, PRIOR), in which PRIOR is a
Daniel@0 35 % scalar, allows the field NET.ALPHA in the data structure NET to be
Daniel@0 36 % set, corresponding to a zero-mean isotropic Gaussian prior with
Daniel@0 37 % inverse variance with value PRIOR. Alternatively, PRIOR can consist
Daniel@0 38 % of a data structure with fields ALPHA and INDEX, allowing individual
Daniel@0 39 % Gaussian priors to be set over groups of weights in the network. Here
Daniel@0 40 % ALPHA is a column vector in which each element corresponds to a
Daniel@0 41 % separate group of weights, which need not be mutually exclusive. The
Daniel@0 42 % membership of the groups is defined by the matrix INDX in which the
Daniel@0 43 % columns correspond to the elements of ALPHA. Each column has one
Daniel@0 44 % element for each weight in the matrix, in the order defined by the
Daniel@0 45 % function MLPPAK, and each element is 1 or 0 according to whether the
Daniel@0 46 % weight is a member of the corresponding group or not. A utility
Daniel@0 47 % function MLPPRIOR is provided to help in setting up the PRIOR data
Daniel@0 48 % structure.
Daniel@0 49 %
Daniel@0 50 % NET = MLP(NIN, NHIDDEN, NOUT, FUNC, PRIOR, BETA) also sets the
Daniel@0 51 % additional field NET.BETA in the data structure NET, where beta
Daniel@0 52 % corresponds to the inverse noise variance.
Daniel@0 53 %
Daniel@0 54 % See also
Daniel@0 55 % MLPPRIOR, MLPPAK, MLPUNPAK, MLPFWD, MLPERR, MLPBKP, MLPGRAD
Daniel@0 56 %
Daniel@0 57
Daniel@0 58 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 59
Daniel@0 60 net.type = 'mlp';
Daniel@0 61 net.nin = nin;
Daniel@0 62 net.nhidden = nhidden;
Daniel@0 63 net.nout = nout;
Daniel@0 64 net.nwts = (nin + 1)*nhidden + (nhidden + 1)*nout;
Daniel@0 65
Daniel@0 66 outfns = {'linear', 'logistic', 'softmax'};
Daniel@0 67
Daniel@0 68 if sum(strcmp(outfunc, outfns)) == 0
Daniel@0 69 error('Undefined output function. Exiting.');
Daniel@0 70 else
Daniel@0 71 net.outfn = outfunc;
Daniel@0 72 end
Daniel@0 73
Daniel@0 74 if nargin > 4
Daniel@0 75 if isstruct(prior)
Daniel@0 76 net.alpha = prior.alpha;
Daniel@0 77 net.index = prior.index;
Daniel@0 78 elseif size(prior) == [1 1]
Daniel@0 79 net.alpha = prior;
Daniel@0 80 else
Daniel@0 81 error('prior must be a scalar or a structure');
Daniel@0 82 end
Daniel@0 83 end
Daniel@0 84
Daniel@0 85 net.w1 = randn(nin, nhidden)/sqrt(nin + 1);
Daniel@0 86 net.b1 = randn(1, nhidden)/sqrt(nin + 1);
Daniel@0 87 net.w2 = randn(nhidden, nout)/sqrt(nhidden + 1);
Daniel@0 88 net.b2 = randn(1, nout)/sqrt(nhidden + 1);
Daniel@0 89
Daniel@0 90 if nargin == 6
Daniel@0 91 net.beta = beta;
Daniel@0 92 end