final touches to gradchecker

This commit is contained in:
Max Zwiessele 2014-02-10 17:00:50 +00:00
parent 32491abaff
commit ed87e6bbd2

View file

@ -447,14 +447,14 @@ class Model(Parameterized):
for i, ind in enumerate(param_list): for i, ind in enumerate(param_list):
xx = x.copy() xx = x.copy()
xx[i] += step xx[ind] += step
f1, g1 = self.objective_and_gradients(xx) f1, g1 = self.objective_and_gradients(xx)
xx[i] -= 2.*step xx[ind] -= 2.*step
f2, g2 = self.objective_and_gradients(xx) f2, g2 = self.objective_and_gradients(xx)
numerical_gradient = (f1 - f2) / (2 * step) numerical_gradient = (f1 - f2) / (2 * step)
ratio = (f1 - f2) / (2 * step * gradient[i]) ratio = (f1 - f2) / (2 * step * gradient[ind])
difference = np.abs((f1 - f2) / 2 / step - gradient[i]) difference = np.abs((f1 - f2) / 2 / step - gradient[ind])
if (np.abs(1. - ratio) < tolerance) or np.abs(difference) < tolerance: if (np.abs(1. - ratio) < tolerance) or np.abs(difference) < tolerance:
formatted_name = "\033[92m {0} \033[0m".format(names[ind]) formatted_name = "\033[92m {0} \033[0m".format(names[ind])
@ -462,7 +462,7 @@ class Model(Parameterized):
formatted_name = "\033[91m {0} \033[0m".format(names[ind]) formatted_name = "\033[91m {0} \033[0m".format(names[ind])
r = '%.6f' % float(ratio) r = '%.6f' % float(ratio)
d = '%.6f' % float(difference) d = '%.6f' % float(difference)
g = '%.6f' % gradient[i] g = '%.6f' % gradient[ind]
ng = '%.6f' % float(numerical_gradient) ng = '%.6f' % float(numerical_gradient)
grad_string = "{0:<{c0}}|{1:^{c1}}|{2:^{c2}}|{3:^{c3}}|{4:^{c4}}".format(formatted_name, r, d, g, ng, c0=cols[0] + 9, c1=cols[1], c2=cols[2], c3=cols[3], c4=cols[4]) grad_string = "{0:<{c0}}|{1:^{c1}}|{2:^{c2}}|{3:^{c3}}|{4:^{c4}}".format(formatted_name, r, d, g, ng, c0=cols[0] + 9, c1=cols[1], c2=cols[2], c3=cols[3], c4=cols[4])
print grad_string print grad_string