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