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