wolffd@0: function [y, z, a] = mlpfwd(net, x) wolffd@0: %MLPFWD Forward propagation through 2-layer network. wolffd@0: % wolffd@0: % Description wolffd@0: % Y = MLPFWD(NET, X) takes a network data structure NET together with a wolffd@0: % matrix X of input vectors, and forward propagates the inputs through wolffd@0: % the network to generate a matrix Y of output vectors. Each row of X wolffd@0: % corresponds to one input vector and each row of Y corresponds to one wolffd@0: % output vector. wolffd@0: % wolffd@0: % [Y, Z] = MLPFWD(NET, X) also generates a matrix Z of the hidden unit wolffd@0: % activations where each row corresponds to one pattern. wolffd@0: % wolffd@0: % [Y, Z, A] = MLPFWD(NET, X) also returns a matrix A giving the summed wolffd@0: % inputs to each output unit, where each row corresponds to one wolffd@0: % pattern. wolffd@0: % wolffd@0: % See also wolffd@0: % MLP, MLPPAK, MLPUNPAK, MLPERR, MLPBKP, MLPGRAD wolffd@0: % wolffd@0: wolffd@0: % Copyright (c) Ian T Nabney (1996-2001) wolffd@0: wolffd@0: % Check arguments for consistency wolffd@0: errstring = consist(net, 'mlp', x); wolffd@0: if ~isempty(errstring); wolffd@0: error(errstring); wolffd@0: end wolffd@0: wolffd@0: ndata = size(x, 1); wolffd@0: wolffd@0: z = tanh(x*net.w1 + ones(ndata, 1)*net.b1); wolffd@0: a = z*net.w2 + ones(ndata, 1)*net.b2; wolffd@0: wolffd@0: switch net.outfn wolffd@0: wolffd@0: case 'linear' % Linear outputs wolffd@0: wolffd@0: y = a; wolffd@0: wolffd@0: case 'logistic' % Logistic outputs wolffd@0: % Prevent overflow and underflow: use same bounds as mlperr wolffd@0: % Ensure that log(1-y) is computable: need exp(a) > eps wolffd@0: maxcut = -log(eps); wolffd@0: % Ensure that log(y) is computable wolffd@0: mincut = -log(1/realmin - 1); wolffd@0: a = min(a, maxcut); wolffd@0: a = max(a, mincut); wolffd@0: y = 1./(1 + exp(-a)); wolffd@0: wolffd@0: case 'softmax' % Softmax outputs wolffd@0: wolffd@0: % Prevent overflow and underflow: use same bounds as glmerr wolffd@0: % Ensure that sum(exp(a), 2) does not overflow wolffd@0: maxcut = log(realmax) - log(net.nout); wolffd@0: % Ensure that exp(a) > 0 wolffd@0: mincut = log(realmin); wolffd@0: a = min(a, maxcut); wolffd@0: a = max(a, mincut); wolffd@0: temp = exp(a); wolffd@0: y = temp./(sum(temp, 2)*ones(1, net.nout)); wolffd@0: wolffd@0: otherwise wolffd@0: error(['Unknown activation function ', net.outfn]); wolffd@0: end