wolffd@0: function [y, a] = glmfwd(net, x) wolffd@0: %GLMFWD Forward propagation through generalized linear model. wolffd@0: % wolffd@0: % Description wolffd@0: % Y = GLMFWD(NET, X) takes a generalized linear model data structure wolffd@0: % NET together with a matrix X of input vectors, and forward propagates wolffd@0: % the inputs through the network to generate a matrix Y of output wolffd@0: % vectors. Each row of X corresponds to one input vector and each row wolffd@0: % of Y corresponds to one output vector. wolffd@0: % wolffd@0: % [Y, A] = GLMFWD(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: % GLM, GLMPAK, GLMUNPAK, GLMERR, GLMGRAD 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, 'glm', 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: a = x*net.w1 + ones(ndata, 1)*net.b1; wolffd@0: wolffd@0: switch net.outfn wolffd@0: wolffd@0: case 'linear' % Linear outputs wolffd@0: y = a; wolffd@0: wolffd@0: case 'logistic' % Logistic outputs wolffd@0: % Prevent overflow and underflow: use same bounds as glmerr 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: nout = size(a,2); 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(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,nout)); wolffd@0: % Ensure that log(y) is computable wolffd@0: y(y