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