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