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

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