comparison toolboxes/FullBNT-1.0.7/netlabKPM/glmhess_weighted.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 [h, hdata] = glmhess_weighted(net, x, t, eso_w, hdata)
2 %GLMHESS Evaluate the Hessian matrix for a generalised linear model.
3 %
4 % Description
5 % H = GLMHESS(NET, X, T) takes a GLM network data structure NET, a
6 % matrix X of input values, and a matrix T of target values and returns
7 % the full Hessian matrix H corresponding to the second derivatives of
8 % the negative log posterior distribution, evaluated for the current
9 % weight and bias values as defined by NET. Note that the target data
10 % is not required in the calculation, but is included to make the
11 % interface uniform with NETHESS. For linear and logistic outputs, the
12 % computation is very simple and is done (in effect) in one line in
13 % GLMTRAIN.
14 %
15 % See also
16 % GLM, GLMTRAIN, HESSCHEK, NETHESS
17 %
18 % Copyright (c) Ian T Nabney (1996-9)
19
20 % Check arguments for consistency
21 errstring = consist(net, 'glm', x, t);
22 if ~isempty(errstring);
23 error(errstring);
24 end
25
26 ndata = size(x, 1);
27 nparams = net.nwts;
28 nout = net.nout;
29 p = glmfwd(net, x);
30 inputs = [x ones(ndata, 1)];
31
32 if nargin == 4
33 hdata = zeros(nparams); % Full Hessian matrix
34 % Calculate data component of Hessian
35 switch net.outfn
36
37 case 'softmax'
38 bb_start = nparams - nout + 1; % Start of bias weights block
39 ex_hess = zeros(nparams); % Contribution to Hessian from single example
40 for m = 1:ndata
41 X = x(m,:)'*x(m,:);
42 a = diag(p(m,:))-((p(m,:)')*p(m,:));
43 a=eso_w(m,1)*a;
44 ex_hess(1:nparams-nout,1:nparams-nout) = kron(a, X);
45 ex_hess(bb_start:nparams, bb_start:nparams) = a.*ones(net.nout, net.nout);
46 temp = kron(a, x(m,:));
47 ex_hess(bb_start:nparams, 1:nparams-nout) = temp;
48 ex_hess(1:nparams-nout, bb_start:nparams) = temp';
49 hdata = hdata + ex_hess;
50 end
51
52 otherwise
53 error(['Unknown activation function ', net.actfn]);
54 end
55 end
56
57 [h, hdata] = hbayes(net, hdata);