annotate toolboxes/FullBNT-1.0.7/netlab3.3/glmfwd.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [y, a] = glmfwd(net, x)
wolffd@0 2 %GLMFWD Forward propagation through generalized linear model.
wolffd@0 3 %
wolffd@0 4 % Description
wolffd@0 5 % Y = GLMFWD(NET, X) takes a generalized linear model data structure
wolffd@0 6 % NET together with a matrix X of input vectors, and forward propagates
wolffd@0 7 % the inputs through the network to generate a matrix Y of output
wolffd@0 8 % vectors. Each row of X corresponds to one input vector and each row
wolffd@0 9 % of Y corresponds to one output vector.
wolffd@0 10 %
wolffd@0 11 % [Y, A] = GLMFWD(NET, X) also returns a matrix A giving the summed
wolffd@0 12 % inputs to each output unit, where each row corresponds to one
wolffd@0 13 % pattern.
wolffd@0 14 %
wolffd@0 15 % See also
wolffd@0 16 % GLM, GLMPAK, GLMUNPAK, GLMERR, GLMGRAD
wolffd@0 17 %
wolffd@0 18
wolffd@0 19 % Copyright (c) Ian T Nabney (1996-2001)
wolffd@0 20
wolffd@0 21 % Check arguments for consistency
wolffd@0 22 errstring = consist(net, 'glm', x);
wolffd@0 23 if ~isempty(errstring);
wolffd@0 24 error(errstring);
wolffd@0 25 end
wolffd@0 26
wolffd@0 27 ndata = size(x, 1);
wolffd@0 28
wolffd@0 29 a = x*net.w1 + ones(ndata, 1)*net.b1;
wolffd@0 30
wolffd@0 31 switch net.outfn
wolffd@0 32
wolffd@0 33 case 'linear' % Linear outputs
wolffd@0 34 y = a;
wolffd@0 35
wolffd@0 36 case 'logistic' % Logistic outputs
wolffd@0 37 % Prevent overflow and underflow: use same bounds as glmerr
wolffd@0 38 % Ensure that log(1-y) is computable: need exp(a) > eps
wolffd@0 39 maxcut = -log(eps);
wolffd@0 40 % Ensure that log(y) is computable
wolffd@0 41 mincut = -log(1/realmin - 1);
wolffd@0 42 a = min(a, maxcut);
wolffd@0 43 a = max(a, mincut);
wolffd@0 44 y = 1./(1 + exp(-a));
wolffd@0 45
wolffd@0 46 case 'softmax' % Softmax outputs
wolffd@0 47 nout = size(a,2);
wolffd@0 48 % Prevent overflow and underflow: use same bounds as glmerr
wolffd@0 49 % Ensure that sum(exp(a), 2) does not overflow
wolffd@0 50 maxcut = log(realmax) - log(nout);
wolffd@0 51 % Ensure that exp(a) > 0
wolffd@0 52 mincut = log(realmin);
wolffd@0 53 a = min(a, maxcut);
wolffd@0 54 a = max(a, mincut);
wolffd@0 55 temp = exp(a);
wolffd@0 56 y = temp./(sum(temp, 2)*ones(1,nout));
wolffd@0 57 % Ensure that log(y) is computable
wolffd@0 58 y(y<realmin) = realmin;
wolffd@0 59
wolffd@0 60 otherwise
wolffd@0 61 error(['Unknown activation function ', net.outfn]);
wolffd@0 62 end