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