1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 from . import HierarchyError
31 from .pickleable import Pickleable
32 from .parentable import Parentable
33
35 """
36 Adds the functionality for an object to be gradcheckable.
37 It is just a thin wrapper of a call to the highest parent for now.
38 TODO: Can be done better, by only changing parameters of the current parameter handle,
39 such that object hierarchy only has to change for those.
40 """
43
44 - def checkgrad(self, verbose=0, step=1e-6, tolerance=1e-3, df_tolerance=1e-12):
45 """
46 Check the gradient of this parameter with respect to the highest parent's
47 objective function.
48 This is a three point estimate of the gradient, wiggling at the parameters
49 with a stepsize step.
50 The check passes if either the ratio or the difference between numerical and
51 analytical gradient is smaller then tolerance.
52
53 :param bool verbose: whether each parameter shall be checked individually.
54 :param float step: the stepsize for the numerical three point gradient estimate.
55 :param float tolerance: the tolerance for the gradient ratio or difference.
56 :param float df_tolerance: the tolerance for df_tolerance
57
58 .. note::
59 The *dF_ratio* indicates the limit of accuracy of numerical gradients.
60 If it is too small, e.g., smaller than 1e-12, the numerical gradients
61 are usually not accurate enough for the tests (shown with blue).
62 """
63
64
65
66
67 if self.has_parent():
68 return self._highest_parent_._checkgrad(self, verbose=verbose, step=step, tolerance=tolerance, df_tolerance=df_tolerance)
69 return self._checkgrad(self, verbose=verbose, step=step, tolerance=tolerance, df_tolerance=df_tolerance)
70
71 - def _checkgrad(self, param, verbose=0, step=1e-6, tolerance=1e-3, df_tolerance=1e-12):
72 """
73 Perform the checkgrad on the model.
74 TODO: this can be done more efficiently, when doing it inside here
75 """
76 raise HierarchyError("This parameter is not in a model with a likelihood, and, therefore, cannot be gradient checked!")
77