wolffd@0: function [h, hdata] = glmhess_weighted(net, x, t, eso_w, hdata) wolffd@0: %GLMHESS Evaluate the Hessian matrix for a generalised linear model. wolffd@0: % wolffd@0: % Description wolffd@0: % H = GLMHESS(NET, X, T) takes a GLM network data structure NET, a wolffd@0: % matrix X of input values, and a matrix T of target values and returns wolffd@0: % the full Hessian matrix H corresponding to the second derivatives of wolffd@0: % the negative log posterior distribution, evaluated for the current wolffd@0: % weight and bias values as defined by NET. Note that the target data wolffd@0: % is not required in the calculation, but is included to make the wolffd@0: % interface uniform with NETHESS. For linear and logistic outputs, the wolffd@0: % computation is very simple and is done (in effect) in one line in wolffd@0: % GLMTRAIN. wolffd@0: % wolffd@0: % See also wolffd@0: % GLM, GLMTRAIN, HESSCHEK, NETHESS wolffd@0: % wolffd@0: % Copyright (c) Ian T Nabney (1996-9) wolffd@0: wolffd@0: % Check arguments for consistency wolffd@0: errstring = consist(net, 'glm', x, t); wolffd@0: if ~isempty(errstring); wolffd@0: error(errstring); wolffd@0: end wolffd@0: wolffd@0: ndata = size(x, 1); wolffd@0: nparams = net.nwts; wolffd@0: nout = net.nout; wolffd@0: p = glmfwd(net, x); wolffd@0: inputs = [x ones(ndata, 1)]; wolffd@0: wolffd@0: if nargin == 4 wolffd@0: hdata = zeros(nparams); % Full Hessian matrix wolffd@0: % Calculate data component of Hessian wolffd@0: switch net.outfn wolffd@0: wolffd@0: case 'softmax' wolffd@0: bb_start = nparams - nout + 1; % Start of bias weights block wolffd@0: ex_hess = zeros(nparams); % Contribution to Hessian from single example wolffd@0: for m = 1:ndata wolffd@0: X = x(m,:)'*x(m,:); wolffd@0: a = diag(p(m,:))-((p(m,:)')*p(m,:)); wolffd@0: a=eso_w(m,1)*a; wolffd@0: ex_hess(1:nparams-nout,1:nparams-nout) = kron(a, X); wolffd@0: ex_hess(bb_start:nparams, bb_start:nparams) = a.*ones(net.nout, net.nout); wolffd@0: temp = kron(a, x(m,:)); wolffd@0: ex_hess(bb_start:nparams, 1:nparams-nout) = temp; wolffd@0: ex_hess(1:nparams-nout, bb_start:nparams) = temp'; wolffd@0: hdata = hdata + ex_hess; wolffd@0: end wolffd@0: wolffd@0: otherwise wolffd@0: error(['Unknown activation function ', net.actfn]); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: [h, hdata] = hbayes(net, hdata);