comparison toolboxes/FullBNT-1.0.7/netlab3.3/mlpfwd.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 [y, z, a] = mlpfwd(net, x)
2 %MLPFWD Forward propagation through 2-layer network.
3 %
4 % Description
5 % Y = MLPFWD(NET, X) takes a network data structure NET together with a
6 % matrix X of input vectors, and forward propagates the inputs through
7 % the network to generate a matrix Y of output vectors. Each row of X
8 % corresponds to one input vector and each row of Y corresponds to one
9 % output vector.
10 %
11 % [Y, Z] = MLPFWD(NET, X) also generates a matrix Z of the hidden unit
12 % activations where each row corresponds to one pattern.
13 %
14 % [Y, Z, A] = MLPFWD(NET, X) also returns a matrix A giving the summed
15 % inputs to each output unit, where each row corresponds to one
16 % pattern.
17 %
18 % See also
19 % MLP, MLPPAK, MLPUNPAK, MLPERR, MLPBKP, MLPGRAD
20 %
21
22 % Copyright (c) Ian T Nabney (1996-2001)
23
24 % Check arguments for consistency
25 errstring = consist(net, 'mlp', x);
26 if ~isempty(errstring);
27 error(errstring);
28 end
29
30 ndata = size(x, 1);
31
32 z = tanh(x*net.w1 + ones(ndata, 1)*net.b1);
33 a = z*net.w2 + ones(ndata, 1)*net.b2;
34
35 switch net.outfn
36
37 case 'linear' % Linear outputs
38
39 y = a;
40
41 case 'logistic' % Logistic outputs
42 % Prevent overflow and underflow: use same bounds as mlperr
43 % Ensure that log(1-y) is computable: need exp(a) > eps
44 maxcut = -log(eps);
45 % Ensure that log(y) is computable
46 mincut = -log(1/realmin - 1);
47 a = min(a, maxcut);
48 a = max(a, mincut);
49 y = 1./(1 + exp(-a));
50
51 case 'softmax' % Softmax outputs
52
53 % Prevent overflow and underflow: use same bounds as glmerr
54 % Ensure that sum(exp(a), 2) does not overflow
55 maxcut = log(realmax) - log(net.nout);
56 % Ensure that exp(a) > 0
57 mincut = log(realmin);
58 a = min(a, maxcut);
59 a = max(a, mincut);
60 temp = exp(a);
61 y = temp./(sum(temp, 2)*ones(1, net.nout));
62
63 otherwise
64 error(['Unknown activation function ', net.outfn]);
65 end