comparison toolboxes/FullBNT-1.0.7/netlab3.3/gpgrad.m @ 0:e9a9cd732c1e tip

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