annotate toolboxes/FullBNT-1.0.7/netlab3.3/gpgrad.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function g = gpgrad(net, x, t)
Daniel@0 2 %GPGRAD Evaluate error gradient for Gaussian Process.
Daniel@0 3 %
Daniel@0 4 % Description
Daniel@0 5 % G = GPGRAD(NET, X, T) takes a Gaussian Process data structure NET
Daniel@0 6 % together with a matrix X of input vectors and a matrix T of target
Daniel@0 7 % vectors, and evaluates the error gradient G. Each row of X
Daniel@0 8 % corresponds to one input vector and each row of T corresponds to one
Daniel@0 9 % target vector.
Daniel@0 10 %
Daniel@0 11 % See also
Daniel@0 12 % GP, GPCOVAR, GPFWD, GPERR
Daniel@0 13 %
Daniel@0 14
Daniel@0 15 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 16
Daniel@0 17 errstring = consist(net, 'gp', x, t);
Daniel@0 18 if ~isempty(errstring);
Daniel@0 19 error(errstring);
Daniel@0 20 end
Daniel@0 21
Daniel@0 22 % Evaluate derivatives with respect to each hyperparameter in turn.
Daniel@0 23 ndata = size(x, 1);
Daniel@0 24 [cov, covf] = gpcovar(net, x);
Daniel@0 25 cninv = inv(cov);
Daniel@0 26 trcninv = trace(cninv);
Daniel@0 27 cninvt = cninv*t;
Daniel@0 28
Daniel@0 29 % Function parameters
Daniel@0 30 switch net.covar_fn
Daniel@0 31
Daniel@0 32 case 'sqexp' % Squared exponential
Daniel@0 33 gfpar = trace(cninv*covf) - cninvt'*covf*cninvt;
Daniel@0 34
Daniel@0 35 case 'ratquad' % Rational quadratic
Daniel@0 36 beta = diag(exp(net.inweights));
Daniel@0 37 gfpar(1) = trace(cninv*covf) - cninvt'*covf*cninvt;
Daniel@0 38 D2 = (x.*x)*beta*ones(net.nin, ndata) - 2*x*beta*x' ...
Daniel@0 39 + ones(ndata, net.nin)*beta*(x.*x)';
Daniel@0 40 E = ones(size(D2));
Daniel@0 41 L = - exp(net.fpar(2)) * covf .* log(E + D2); % d(cn)/d(nu)
Daniel@0 42 gfpar(2) = trace(cninv*L) - cninvt'*L*cninvt;
Daniel@0 43
Daniel@0 44 otherwise
Daniel@0 45 error(['Unknown covariance function ', net.covar_fn]);
Daniel@0 46 end
Daniel@0 47
Daniel@0 48 % Bias derivative
Daniel@0 49 ndata = size(x, 1);
Daniel@0 50 fac = exp(net.bias)*ones(ndata);
Daniel@0 51 gbias = trace(cninv*fac) - cninvt'*fac*cninvt;
Daniel@0 52
Daniel@0 53 % Noise derivative
Daniel@0 54 gnoise = exp(net.noise)*(trcninv - cninvt'*cninvt);
Daniel@0 55
Daniel@0 56 % Input weight derivatives
Daniel@0 57 if strcmp(net.covar_fn, 'ratquad')
Daniel@0 58 F = (exp(net.fpar(2))*E)./(E + D2);
Daniel@0 59 end
Daniel@0 60
Daniel@0 61 nparams = length(net.inweights);
Daniel@0 62 for l = 1 : nparams
Daniel@0 63 vect = x(:, l);
Daniel@0 64 matx = (vect.*vect)*ones(1, ndata) ...
Daniel@0 65 - 2.0*vect*vect' ...
Daniel@0 66 + ones(ndata, 1)*(vect.*vect)';
Daniel@0 67 switch net.covar_fn
Daniel@0 68 case 'sqexp' % Squared exponential
Daniel@0 69 dmat = -0.5*exp(net.inweights(l))*covf.*matx;
Daniel@0 70
Daniel@0 71 case 'ratquad' % Rational quadratic
Daniel@0 72 dmat = - exp(net.inweights(l))*covf.*matx.*F;
Daniel@0 73 otherwise
Daniel@0 74 error(['Unknown covariance function ', net.covar_fn]);
Daniel@0 75 end
Daniel@0 76
Daniel@0 77 gw1(l) = trace(cninv*dmat) - cninvt'*dmat*cninvt;
Daniel@0 78 end
Daniel@0 79
Daniel@0 80 g1 = [gbias, gnoise, gw1, gfpar];
Daniel@0 81 g1 = 0.5*g1;
Daniel@0 82
Daniel@0 83 % Evaluate the prior contribution to the gradient.
Daniel@0 84 if isfield(net, 'pr_mean')
Daniel@0 85 w = gppak(net);
Daniel@0 86 m = repmat(net.pr_mean, size(w));
Daniel@0 87 if size(net.pr_mean) == [1 1]
Daniel@0 88 gprior = w - m;
Daniel@0 89 g2 = gprior/net.pr_var;
Daniel@0 90 else
Daniel@0 91 ngroups = size(net.pr_mean, 1);
Daniel@0 92 gprior = net.index'.*(ones(ngroups, 1)*w - m);
Daniel@0 93 g2 = (1./net.pr_var)'*gprior;
Daniel@0 94 end
Daniel@0 95 else
Daniel@0 96 gprior = 0;
Daniel@0 97 g2 = 0;
Daniel@0 98 end
Daniel@0 99
Daniel@0 100 g = g1 + g2;