comparison toolboxes/FullBNT-1.0.7/netlab3.3/rbfhess.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] = rbfhess(net, x, t, hdata)
2 %RBFHESS Evaluate the Hessian matrix for RBF network.
3 %
4 % Description
5 % H = RBFHESS(NET, X, T) takes an RBF 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. Currently, the
10 % implementation only computes the Hessian for the output layer
11 % weights.
12 %
13 % [H, HDATA] = RBFHESS(NET, X, T) returns both the Hessian matrix H and
14 % the contribution HDATA arising from the data dependent term in the
15 % Hessian.
16 %
17 % H = RBFHESS(NET, X, T, HDATA) takes a network data structure NET, a
18 % matrix X of input values, and a matrix T of target values, together
19 % with the contribution HDATA arising from the data dependent term in
20 % the Hessian, and returns the full Hessian matrix H corresponding to
21 % the second derivatives of the negative log posterior distribution.
22 % This version saves computation time if HDATA has already been
23 % evaluated for the current weight and bias values.
24 %
25 % See also
26 % MLPHESS, HESSCHEK, EVIDENCE
27 %
28
29 % Copyright (c) Ian T Nabney (1996-2001)
30
31 % Check arguments for consistency
32 errstring = consist(net, 'rbf', x, t);
33 if ~isempty(errstring);
34 error(errstring);
35 end
36
37 if nargin == 3
38 % Data term in Hessian needs to be computed
39 [a, z] = rbffwd(net, x);
40 hdata = datahess(net, z, t);
41 end
42
43 % Add in effect of regularisation
44 [h, hdata] = hbayes(net, hdata);
45
46 % Sub-function to compute data part of Hessian
47 function hdata = datahess(net, z, t)
48
49 % Only works for output layer Hessian currently
50 if (isfield(net, 'mask') & ~any(net.mask(...
51 1:(net.nwts - net.nout*(net.nhidden+1)))))
52 hdata = zeros(net.nwts);
53 ndata = size(z, 1);
54 out_hess = [z ones(ndata, 1)]'*[z ones(ndata, 1)];
55 for j = 1:net.nout
56 hdata = rearrange_hess(net, j, out_hess, hdata);
57 end
58 else
59 error('Output layer Hessian only.');
60 end
61 return
62
63 % Sub-function to rearrange Hessian matrix
64 function hdata = rearrange_hess(net, j, out_hess, hdata)
65
66 % Because all the biases come after all the input weights,
67 % we have to rearrange the blocks that make up the network Hessian.
68 % This function assumes that we are on the jth output and that all outputs
69 % are independent.
70
71 % Start of bias weights block
72 bb_start = net.nwts - net.nout + 1;
73 % Start of weight block for jth output
74 ob_start = net.nwts - net.nout*(net.nhidden+1) + (j-1)*net.nhidden...
75 + 1;
76 % End of weight block for jth output
77 ob_end = ob_start + net.nhidden - 1;
78 % Index of bias weight
79 b_index = bb_start+(j-1);
80 % Put input weight block in right place
81 hdata(ob_start:ob_end, ob_start:ob_end) = out_hess(1:net.nhidden, ...
82 1:net.nhidden);
83 % Put second derivative of bias weight in right place
84 hdata(b_index, b_index) = out_hess(net.nhidden+1, net.nhidden+1);
85 % Put cross terms (input weight v bias weight) in right place
86 hdata(b_index, ob_start:ob_end) = out_hess(net.nhidden+1, ...
87 1:net.nhidden);
88 hdata(ob_start:ob_end, b_index) = out_hess(1:net.nhidden, ...
89 net.nhidden+1);
90
91 return