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