wolffd@0
|
1 function [g, gdata, gprior] = gbayes(net, gdata)
|
wolffd@0
|
2 %GBAYES Evaluate gradient of Bayesian error function for network.
|
wolffd@0
|
3 %
|
wolffd@0
|
4 % Description
|
wolffd@0
|
5 % G = GBAYES(NET, GDATA) takes a network data structure NET together
|
wolffd@0
|
6 % the data contribution to the error gradient for a set of inputs and
|
wolffd@0
|
7 % targets. It returns the regularised error gradient using any zero
|
wolffd@0
|
8 % mean Gaussian priors on the weights defined in NET. In addition, if
|
wolffd@0
|
9 % a MASK is defined in NET, then the entries in G that correspond to
|
wolffd@0
|
10 % weights with a 0 in the mask are removed.
|
wolffd@0
|
11 %
|
wolffd@0
|
12 % [G, GDATA, GPRIOR] = GBAYES(NET, GDATA) additionally returns the data
|
wolffd@0
|
13 % and prior components of the error.
|
wolffd@0
|
14 %
|
wolffd@0
|
15 % See also
|
wolffd@0
|
16 % ERRBAYES, GLMGRAD, MLPGRAD, RBFGRAD
|
wolffd@0
|
17 %
|
wolffd@0
|
18
|
wolffd@0
|
19 % Copyright (c) Ian T Nabney (1996-2001)
|
wolffd@0
|
20
|
wolffd@0
|
21 % Evaluate the data contribution to the gradient.
|
wolffd@0
|
22 if (isfield(net, 'mask'))
|
wolffd@0
|
23 gdata = gdata(logical(net.mask));
|
wolffd@0
|
24 end
|
wolffd@0
|
25 if isfield(net, 'beta')
|
wolffd@0
|
26 g1 = gdata*net.beta;
|
wolffd@0
|
27 else
|
wolffd@0
|
28 g1 = gdata;
|
wolffd@0
|
29 end
|
wolffd@0
|
30
|
wolffd@0
|
31 % Evaluate the prior contribution to the gradient.
|
wolffd@0
|
32 if isfield(net, 'alpha')
|
wolffd@0
|
33 w = netpak(net);
|
wolffd@0
|
34 if size(net.alpha) == [1 1]
|
wolffd@0
|
35 gprior = w;
|
wolffd@0
|
36 g2 = net.alpha*gprior;
|
wolffd@0
|
37 else
|
wolffd@0
|
38 if (isfield(net, 'mask'))
|
wolffd@0
|
39 nindx_cols = size(net.index, 2);
|
wolffd@0
|
40 nmask_rows = size(find(net.mask), 1);
|
wolffd@0
|
41 index = reshape(net.index(logical(repmat(net.mask, ...
|
wolffd@0
|
42 1, nindx_cols))), nmask_rows, nindx_cols);
|
wolffd@0
|
43 else
|
wolffd@0
|
44 index = net.index;
|
wolffd@0
|
45 end
|
wolffd@0
|
46
|
wolffd@0
|
47 ngroups = size(net.alpha, 1);
|
wolffd@0
|
48 gprior = index'.*(ones(ngroups, 1)*w);
|
wolffd@0
|
49 g2 = net.alpha'*gprior;
|
wolffd@0
|
50 end
|
wolffd@0
|
51 else
|
wolffd@0
|
52 gprior = 0;
|
wolffd@0
|
53 g2 = 0;
|
wolffd@0
|
54 end
|
wolffd@0
|
55
|
wolffd@0
|
56 g = g1 + g2;
|