Daniel@0: function [g, gdata, gprior] = gbayes(net, gdata) Daniel@0: %GBAYES Evaluate gradient of Bayesian error function for network. Daniel@0: % Daniel@0: % Description Daniel@0: % G = GBAYES(NET, GDATA) takes a network data structure NET together Daniel@0: % the data contribution to the error gradient for a set of inputs and Daniel@0: % targets. It returns the regularised error gradient using any zero Daniel@0: % mean Gaussian priors on the weights defined in NET. In addition, if Daniel@0: % a MASK is defined in NET, then the entries in G that correspond to Daniel@0: % weights with a 0 in the mask are removed. Daniel@0: % Daniel@0: % [G, GDATA, GPRIOR] = GBAYES(NET, GDATA) additionally returns the data Daniel@0: % and prior components of the error. Daniel@0: % Daniel@0: % See also Daniel@0: % ERRBAYES, GLMGRAD, MLPGRAD, RBFGRAD Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: % Evaluate the data contribution to the gradient. Daniel@0: if (isfield(net, 'mask')) Daniel@0: gdata = gdata(logical(net.mask)); Daniel@0: end Daniel@0: if isfield(net, 'beta') Daniel@0: g1 = gdata*net.beta; Daniel@0: else Daniel@0: g1 = gdata; Daniel@0: end Daniel@0: Daniel@0: % Evaluate the prior contribution to the gradient. Daniel@0: if isfield(net, 'alpha') Daniel@0: w = netpak(net); Daniel@0: if size(net.alpha) == [1 1] Daniel@0: gprior = w; Daniel@0: g2 = net.alpha*gprior; Daniel@0: else Daniel@0: if (isfield(net, 'mask')) Daniel@0: nindx_cols = size(net.index, 2); Daniel@0: nmask_rows = size(find(net.mask), 1); Daniel@0: index = reshape(net.index(logical(repmat(net.mask, ... Daniel@0: 1, nindx_cols))), nmask_rows, nindx_cols); Daniel@0: else Daniel@0: index = net.index; Daniel@0: end Daniel@0: Daniel@0: ngroups = size(net.alpha, 1); Daniel@0: gprior = index'.*(ones(ngroups, 1)*w); Daniel@0: g2 = net.alpha'*gprior; Daniel@0: end Daniel@0: else Daniel@0: gprior = 0; Daniel@0: g2 = 0; Daniel@0: end Daniel@0: Daniel@0: g = g1 + g2;