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