wolffd@0
|
1 function g = rbfbkp(net, x, z, n2, deltas)
|
wolffd@0
|
2 %RBFBKP Backpropagate gradient of error function for RBF network.
|
wolffd@0
|
3 %
|
wolffd@0
|
4 % Description
|
wolffd@0
|
5 % G = RBFBKP(NET, X, Z, N2, DELTAS) takes a network data structure NET
|
wolffd@0
|
6 % together with a matrix X of input vectors, a matrix Z of hidden unit
|
wolffd@0
|
7 % activations, a matrix N2 of the squared distances between centres and
|
wolffd@0
|
8 % inputs, and a matrix DELTAS of the gradient of the error function
|
wolffd@0
|
9 % with respect to the values of the output units (i.e. the summed
|
wolffd@0
|
10 % inputs to the output units, before the activation function is
|
wolffd@0
|
11 % applied). The return value is the gradient G of the error function
|
wolffd@0
|
12 % with respect to the network weights. Each row of X corresponds to one
|
wolffd@0
|
13 % input vector.
|
wolffd@0
|
14 %
|
wolffd@0
|
15 % This function is provided so that the common backpropagation
|
wolffd@0
|
16 % algorithm can be used by RBF network models to compute gradients for
|
wolffd@0
|
17 % the output values (in RBFDERIV) as well as standard error functions.
|
wolffd@0
|
18 %
|
wolffd@0
|
19 % See also
|
wolffd@0
|
20 % RBF, RBFGRAD, RBFDERIV
|
wolffd@0
|
21 %
|
wolffd@0
|
22
|
wolffd@0
|
23 % Copyright (c) Ian T Nabney (1996-2001)
|
wolffd@0
|
24
|
wolffd@0
|
25 % Evaluate second-layer gradients.
|
wolffd@0
|
26 gw2 = z'*deltas;
|
wolffd@0
|
27 gb2 = sum(deltas);
|
wolffd@0
|
28
|
wolffd@0
|
29 % Evaluate hidden unit gradients
|
wolffd@0
|
30 delhid = deltas*net.w2';
|
wolffd@0
|
31
|
wolffd@0
|
32 gc = zeros(net.nhidden, net.nin);
|
wolffd@0
|
33 ndata = size(x, 1);
|
wolffd@0
|
34 t1 = ones(ndata, 1);
|
wolffd@0
|
35 t2 = ones(1, net.nin);
|
wolffd@0
|
36 % Switch on activation function type
|
wolffd@0
|
37 switch net.actfn
|
wolffd@0
|
38
|
wolffd@0
|
39 case 'gaussian' % Gaussian
|
wolffd@0
|
40 delhid = (delhid.*z);
|
wolffd@0
|
41 % A loop seems essential, so do it with the shortest index vector
|
wolffd@0
|
42 if (net.nin < net.nhidden)
|
wolffd@0
|
43 for i = 1:net.nin
|
wolffd@0
|
44 gc(:,i) = (sum(((x(:,i)*ones(1, net.nhidden)) - ...
|
wolffd@0
|
45 (ones(ndata, 1)*(net.c(:,i)'))).*delhid, 1)./net.wi)';
|
wolffd@0
|
46 end
|
wolffd@0
|
47 else
|
wolffd@0
|
48 for i = 1:net.nhidden
|
wolffd@0
|
49 gc(i,:) = sum((x - (t1*(net.c(i,:)))./net.wi(i)).*(delhid(:,i)*t2), 1);
|
wolffd@0
|
50 end
|
wolffd@0
|
51 end
|
wolffd@0
|
52 gwi = sum((n2.*delhid)./(2.*(ones(ndata, 1)*(net.wi.^2))), 1);
|
wolffd@0
|
53
|
wolffd@0
|
54 case 'tps' % Thin plate spline activation function
|
wolffd@0
|
55 delhid = delhid.*(1+log(n2+(n2==0)));
|
wolffd@0
|
56 for i = 1:net.nhidden
|
wolffd@0
|
57 gc(i,:) = sum(2.*((t1*(net.c(i,:)) - x)).*(delhid(:,i)*t2), 1);
|
wolffd@0
|
58 end
|
wolffd@0
|
59 % widths are not adjustable in this model
|
wolffd@0
|
60 gwi = [];
|
wolffd@0
|
61 case 'r4logr' % r^4 log r activation function
|
wolffd@0
|
62 delhid = delhid.*(n2.*(1+2.*log(n2+(n2==0))));
|
wolffd@0
|
63 for i = 1:net.nhidden
|
wolffd@0
|
64 gc(i,:) = sum(2.*((t1*(net.c(i,:)) - x)).*(delhid(:,i)*t2), 1);
|
wolffd@0
|
65 end
|
wolffd@0
|
66 % widths are not adjustable in this model
|
wolffd@0
|
67 gwi = [];
|
wolffd@0
|
68 otherwise
|
wolffd@0
|
69 error('Unknown activation function in rbfgrad')
|
wolffd@0
|
70 end
|
wolffd@0
|
71
|
wolffd@0
|
72 g = [gc(:)', gwi, gw2(:)', gb2];
|