wolffd@0: function g = glmderiv(net, x) wolffd@0: %GLMDERIV Evaluate derivatives of GLM outputs with respect to weights. wolffd@0: % wolffd@0: % Description wolffd@0: % G = GLMDERIV(NET, X) takes a network data structure NET and a matrix wolffd@0: % of input vectors X and returns a three-index matrix mat{g} whose I, wolffd@0: % J, K element contains the derivative of network output K with respect wolffd@0: % to weight or bias parameter J for input pattern I. The ordering of wolffd@0: % the weight and bias parameters is defined by GLMUNPAK. wolffd@0: % wolffd@0: wolffd@0: % Copyright (c) Ian T Nabney (1996-2001) wolffd@0: wolffd@0: % Check arguments for consistency wolffd@0: errstring = consist(net, 'glm', x); wolffd@0: if ~isempty(errstring) wolffd@0: error(errstring); wolffd@0: end wolffd@0: wolffd@0: ndata = size(x, 1); wolffd@0: if isfield(net, 'mask') wolffd@0: nwts = size(find(net.mask), 1); wolffd@0: mask_array = logical(net.mask)*ones(1, net.nout); wolffd@0: else wolffd@0: nwts = net.nwts; wolffd@0: end wolffd@0: g = zeros(ndata, nwts, net.nout); wolffd@0: wolffd@0: temp = zeros(net.nwts, net.nout); wolffd@0: for n = 1:ndata wolffd@0: % Weight matrix w1 wolffd@0: temp(1:(net.nin*net.nout), :) = kron(eye(net.nout), (x(n, :))'); wolffd@0: % Bias term b1 wolffd@0: temp(net.nin*net.nout+1:end, :) = eye(net.nout); wolffd@0: if isfield(net, 'mask') wolffd@0: g(n, :, :) = reshape(temp(find(mask_array)), nwts, net.nout); wolffd@0: else wolffd@0: g(n, :, :) = temp; wolffd@0: end wolffd@0: end