Daniel@0: function [h, hdata] = glmhess(net, x, t, hdata) Daniel@0: %GLMHESS Evaluate the Hessian matrix for a generalised linear model. Daniel@0: % Daniel@0: % Description Daniel@0: % H = GLMHESS(NET, X, T) takes a GLM network data structure NET, a Daniel@0: % matrix X of input values, and a matrix T of target values and returns Daniel@0: % the full Hessian matrix H corresponding to the second derivatives of Daniel@0: % the negative log posterior distribution, evaluated for the current Daniel@0: % weight and bias values as defined by NET. Note that the target data Daniel@0: % is not required in the calculation, but is included to make the Daniel@0: % interface uniform with NETHESS. For linear and logistic outputs, the Daniel@0: % computation is very simple and is done (in effect) in one line in Daniel@0: % GLMTRAIN. Daniel@0: % Daniel@0: % [H, HDATA] = GLMHESS(NET, X, T) returns both the Hessian matrix H and Daniel@0: % the contribution HDATA arising from the data dependent term in the Daniel@0: % Hessian. Daniel@0: % Daniel@0: % H = GLMHESS(NET, X, T, HDATA) takes a network data structure NET, a Daniel@0: % matrix X of input values, and a matrix T of target values, together Daniel@0: % with the contribution HDATA arising from the data dependent term in Daniel@0: % the Hessian, and returns the full Hessian matrix H corresponding to Daniel@0: % the second derivatives of the negative log posterior distribution. Daniel@0: % This version saves computation time if HDATA has already been Daniel@0: % evaluated for the current weight and bias values. Daniel@0: % Daniel@0: % See also Daniel@0: % GLM, GLMTRAIN, HESSCHEK, NETHESS Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: % Check arguments for consistency Daniel@0: errstring = consist(net, 'glm', x, t); Daniel@0: if ~isempty(errstring); Daniel@0: error(errstring); Daniel@0: end Daniel@0: Daniel@0: ndata = size(x, 1); Daniel@0: nparams = net.nwts; Daniel@0: nout = net.nout; Daniel@0: p = glmfwd(net, x); Daniel@0: inputs = [x ones(ndata, 1)]; Daniel@0: Daniel@0: if nargin == 3 Daniel@0: hdata = zeros(nparams); % Full Hessian matrix Daniel@0: % Calculate data component of Hessian Daniel@0: switch net.outfn Daniel@0: Daniel@0: case 'linear' Daniel@0: % No weighting function here Daniel@0: out_hess = [x ones(ndata, 1)]'*[x ones(ndata, 1)]; Daniel@0: for j = 1:nout Daniel@0: hdata = rearrange_hess(net, j, out_hess, hdata); Daniel@0: end Daniel@0: case 'logistic' Daniel@0: % Each output is independent Daniel@0: e = ones(1, net.nin+1); Daniel@0: link_deriv = p.*(1-p); Daniel@0: out_hess = zeros(net.nin+1); Daniel@0: for j = 1:nout Daniel@0: inputs = [x ones(ndata, 1)].*(sqrt(link_deriv(:,j))*e); Daniel@0: out_hess = inputs'*inputs; % Hessian for this output Daniel@0: hdata = rearrange_hess(net, j, out_hess, hdata); Daniel@0: end Daniel@0: Daniel@0: case 'softmax' Daniel@0: bb_start = nparams - nout + 1; % Start of bias weights block Daniel@0: ex_hess = zeros(nparams); % Contribution to Hessian from single example Daniel@0: for m = 1:ndata Daniel@0: X = x(m,:)'*x(m,:); Daniel@0: a = diag(p(m,:))-((p(m,:)')*p(m,:)); Daniel@0: ex_hess(1:nparams-nout,1:nparams-nout) = kron(a, X); Daniel@0: ex_hess(bb_start:nparams, bb_start:nparams) = a.*ones(net.nout, net.nout); Daniel@0: temp = kron(a, x(m,:)); Daniel@0: ex_hess(bb_start:nparams, 1:nparams-nout) = temp; Daniel@0: ex_hess(1:nparams-nout, bb_start:nparams) = temp'; Daniel@0: hdata = hdata + ex_hess; Daniel@0: end Daniel@0: otherwise Daniel@0: error(['Unknown activation function ', net.outfn]); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: [h, hdata] = hbayes(net, hdata); Daniel@0: Daniel@0: function hdata = rearrange_hess(net, j, out_hess, hdata) Daniel@0: Daniel@0: % Because all the biases come after all the input weights, Daniel@0: % we have to rearrange the blocks that make up the network Hessian. Daniel@0: % This function assumes that we are on the jth output and that all outputs Daniel@0: % are independent. Daniel@0: Daniel@0: bb_start = net.nwts - net.nout + 1; % Start of bias weights block Daniel@0: ob_start = 1+(j-1)*net.nin; % Start of weight block for jth output Daniel@0: ob_end = j*net.nin; % End of weight block for jth output Daniel@0: b_index = bb_start+(j-1); % Index of bias weight Daniel@0: % Put input weight block in right place Daniel@0: hdata(ob_start:ob_end, ob_start:ob_end) = out_hess(1:net.nin, 1:net.nin); Daniel@0: % Put second derivative of bias weight in right place Daniel@0: hdata(b_index, b_index) = out_hess(net.nin+1, net.nin+1); Daniel@0: % Put cross terms (input weight v bias weight) in right place Daniel@0: hdata(b_index, ob_start:ob_end) = out_hess(net.nin+1,1:net.nin); Daniel@0: hdata(ob_start:ob_end, b_index) = out_hess(1:net.nin, net.nin+1); Daniel@0: Daniel@0: return