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