wolffd@0
|
1 function h = hesschek(net, x, t)
|
wolffd@0
|
2 %HESSCHEK Use central differences to confirm correct evaluation of Hessian matrix.
|
wolffd@0
|
3 %
|
wolffd@0
|
4 % Description
|
wolffd@0
|
5 %
|
wolffd@0
|
6 % HESSCHEK(NET, X, T) takes a network data structure NET, together with
|
wolffd@0
|
7 % input and target data matrices X and T, and compares the evaluation
|
wolffd@0
|
8 % of the Hessian matrix using the function NETHESS and using central
|
wolffd@0
|
9 % differences with the function NETERR.
|
wolffd@0
|
10 %
|
wolffd@0
|
11 % The optional return value H is the Hessian computed using NETHESS.
|
wolffd@0
|
12 %
|
wolffd@0
|
13 % See also
|
wolffd@0
|
14 % NETHESS, NETERR
|
wolffd@0
|
15 %
|
wolffd@0
|
16
|
wolffd@0
|
17 % Copyright (c) Ian T Nabney (1996-2001)
|
wolffd@0
|
18
|
wolffd@0
|
19 w0 = netpak(net);
|
wolffd@0
|
20 nwts = length(w0);
|
wolffd@0
|
21 h = nethess(w0, net, x, t);
|
wolffd@0
|
22
|
wolffd@0
|
23 w = w0;
|
wolffd@0
|
24 hcent = zeros(nwts, nwts);
|
wolffd@0
|
25 h1 = 0.0; h2 = 0.0; h3 = 0.0; h4 = 0.0;
|
wolffd@0
|
26 epsilon = 1.0e-4;
|
wolffd@0
|
27 fprintf(1, 'Checking Hessian ...\n\n');
|
wolffd@0
|
28 for k = 1:nwts;
|
wolffd@0
|
29 for l = 1:nwts;
|
wolffd@0
|
30 if(l == k)
|
wolffd@0
|
31 w(k) = w0(k) + 2.0*epsilon;
|
wolffd@0
|
32 h1 = neterr(w, net, x, t);
|
wolffd@0
|
33 w(k) = w0(k) - 2.0*epsilon;
|
wolffd@0
|
34 h2 = neterr(w, net, x, t);
|
wolffd@0
|
35 w(k) = w0(k);
|
wolffd@0
|
36 h3 = neterr(w, net, x, t);
|
wolffd@0
|
37 hcent(k, k) = (h1 + h2 - 2.0*h3)/(4.0*epsilon^2);
|
wolffd@0
|
38 else
|
wolffd@0
|
39 w(k) = w0(k) + epsilon;
|
wolffd@0
|
40 w(l) = w0(l) + epsilon;
|
wolffd@0
|
41 h1 = neterr(w, net, x, t);
|
wolffd@0
|
42 w(k) = w0(k) - epsilon;
|
wolffd@0
|
43 w(l) = w0(l) - epsilon;
|
wolffd@0
|
44 h2 = neterr(w, net, x, t);
|
wolffd@0
|
45 w(k) = w0(k) + epsilon;
|
wolffd@0
|
46 w(l) = w0(l) - epsilon;
|
wolffd@0
|
47 h3 = neterr(w, net, x, t);
|
wolffd@0
|
48 w(k) = w0(k) - epsilon;
|
wolffd@0
|
49 w(l) = w0(l) + epsilon;
|
wolffd@0
|
50 h4 = neterr(w, net, x, t);
|
wolffd@0
|
51 hcent(k, l) = (h1 + h2 - h3 - h4)/(4.0*epsilon^2);
|
wolffd@0
|
52 w(k) = w0(k);
|
wolffd@0
|
53 w(l) = w0(l);
|
wolffd@0
|
54 end
|
wolffd@0
|
55 end
|
wolffd@0
|
56 end
|
wolffd@0
|
57
|
wolffd@0
|
58 fprintf(1, ' analytical numerical delta\n\n');
|
wolffd@0
|
59 temp = [h(:), hcent(:), (h(:) - hcent(:))];
|
wolffd@0
|
60 fprintf(1, '%12.6f %12.6f %12.6f\n', temp'); |