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