oh huge bug in checkgrad global

This commit is contained in:
Max Zwiessele 2014-03-11 16:22:45 +00:00
parent 10608a4565
commit 85a471e0f6

View file

@ -253,7 +253,7 @@ class Model(Parameterized):
sgd.run()
self.optimization_runs.append(sgd)
def _checkgrad(self, target_param=None, verbose=False, step=1e-6, tolerance=1e-3):
def _checkgrad(self, target_param=None, verbose=False, step=1e-6, tolerance=1e-3, _debug=False):
"""
Check the gradient of the ,odel by comparing to a numerical
estimate. If the verbose flag is passed, invividual
@ -303,7 +303,7 @@ class Model(Parameterized):
global_ratio = (f1 - f2) / np.where(denominator==0., 1e-32, denominator)
gloabl_diff = (f1 - f2) - denominator
return (np.abs(1. - global_ratio) < tolerance) or (np.abs(gloabl_diff) < tolerance)
return (np.abs(1. - global_ratio) < tolerance) or (np.abs(gloabl_diff) == 0)
else:
# check the gradient of each parameter individually, and do some pretty printing
try:
@ -349,6 +349,8 @@ class Model(Parameterized):
xx[xind] -= 2.*step
f2 = self.objective_function(xx)
numerical_gradient = (f1 - f2) / (2 * step)
if _debug:
self.gradient[xind] = numerical_gradient
if np.all(gradient[xind]==0): ratio = (f1-f2) == gradient[xind]
else: ratio = (f1 - f2) / (2 * step * gradient[xind])
difference = np.abs((f1 - f2) / 2 / step - gradient[xind])