mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-27 14:25:16 +02:00
added absolute difference check to gradcheck
This commit is contained in:
parent
40c9790529
commit
914bdc73d8
1 changed files with 8 additions and 11 deletions
|
|
@ -67,12 +67,12 @@ class model(parameterised):
|
||||||
|
|
||||||
# check constraints are okay
|
# check constraints are okay
|
||||||
if isinstance(what, (priors.gamma, priors.log_Gaussian)):
|
if isinstance(what, (priors.gamma, priors.log_Gaussian)):
|
||||||
constrained_positive_indices = [i for i,t in zip(self.constrained_indices, self.constraints) if t.domain=='positive']
|
constrained_positive_indices = [i for i, t in zip(self.constrained_indices, self.constraints) if t.domain == 'positive']
|
||||||
if len(constrained_positive_indices):
|
if len(constrained_positive_indices):
|
||||||
constrained_positive_indices = np.hstack(constrained_positive_indices)
|
constrained_positive_indices = np.hstack(constrained_positive_indices)
|
||||||
else:
|
else:
|
||||||
constrained_positive_indices = np.zeros(shape=(0,))
|
constrained_positive_indices = np.zeros(shape=(0,))
|
||||||
bad_constraints = np.setdiff1d(self.all_constrained_indices(),constrained_positive_indices)
|
bad_constraints = np.setdiff1d(self.all_constrained_indices(), constrained_positive_indices)
|
||||||
assert not np.any(which[:, None] == bad_constraints), "constraint and prior incompatible"
|
assert not np.any(which[:, None] == bad_constraints), "constraint and prior incompatible"
|
||||||
unconst = np.setdiff1d(which, constrained_positive_indices)
|
unconst = np.setdiff1d(which, constrained_positive_indices)
|
||||||
if len(unconst):
|
if len(unconst):
|
||||||
|
|
@ -115,12 +115,12 @@ class model(parameterised):
|
||||||
|
|
||||||
def _transform_gradients(self, g):
|
def _transform_gradients(self, g):
|
||||||
x = self._get_params()
|
x = self._get_params()
|
||||||
for index,constraint in zip(self.constrained_indices, self.constraints):
|
for index, constraint in zip(self.constrained_indices, self.constraints):
|
||||||
g[index] = g[index] * constraint.gradfactor(x[index])
|
g[index] = g[index] * constraint.gradfactor(x[index])
|
||||||
[np.put(g, i, v) for i, v in [(t[0], np.sum(g[t])) for t in self.tied_indices]]
|
[np.put(g, i, v) for i, v in [(t[0], np.sum(g[t])) for t in self.tied_indices]]
|
||||||
if len(self.tied_indices) or len(self.fixed_indices):
|
if len(self.tied_indices) or len(self.fixed_indices):
|
||||||
to_remove = np.hstack((self.fixed_indices+[t[1:] for t in self.tied_indices]))
|
to_remove = np.hstack((self.fixed_indices + [t[1:] for t in self.tied_indices]))
|
||||||
return np.delete(g,to_remove)
|
return np.delete(g, to_remove)
|
||||||
else:
|
else:
|
||||||
return g
|
return g
|
||||||
|
|
||||||
|
|
@ -207,7 +207,7 @@ class model(parameterised):
|
||||||
"""
|
"""
|
||||||
Ensure that any variables which should clearly be positive have been constrained somehow.
|
Ensure that any variables which should clearly be positive have been constrained somehow.
|
||||||
"""
|
"""
|
||||||
positive_strings = ['variance','lengthscale', 'precision', 'kappa']
|
positive_strings = ['variance', 'lengthscale', 'precision', 'kappa']
|
||||||
param_names = self._get_param_names()
|
param_names = self._get_param_names()
|
||||||
currently_constrained = self.all_constrained_indices()
|
currently_constrained = self.all_constrained_indices()
|
||||||
to_make_positive = []
|
to_make_positive = []
|
||||||
|
|
@ -359,10 +359,7 @@ class model(parameterised):
|
||||||
numerical_gradient = (f1 - f2) / (2 * dx)
|
numerical_gradient = (f1 - f2) / (2 * dx)
|
||||||
global_ratio = (f1 - f2) / (2 * np.dot(dx, gradient))
|
global_ratio = (f1 - f2) / (2 * np.dot(dx, gradient))
|
||||||
|
|
||||||
if (np.abs(1. - global_ratio) < tolerance) and not np.isnan(global_ratio):
|
return (np.abs(1. - global_ratio) < tolerance) or (np.abs(gradient - numerical_gradient).mean() - 1) < tolerance
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
else:
|
else:
|
||||||
# check the gradient of each parameter individually, and do some pretty printing
|
# check the gradient of each parameter individually, and do some pretty printing
|
||||||
try:
|
try:
|
||||||
|
|
@ -399,7 +396,7 @@ class model(parameterised):
|
||||||
ratio = (f1 - f2) / (2 * step * gradient)
|
ratio = (f1 - f2) / (2 * step * gradient)
|
||||||
difference = np.abs((f1 - f2) / 2 / step - gradient)
|
difference = np.abs((f1 - f2) / 2 / step - gradient)
|
||||||
|
|
||||||
if (np.abs(ratio - 1) < tolerance):
|
if (np.abs(1. - ratio) < tolerance) or np.abs(difference) < tolerance:
|
||||||
formatted_name = "\033[92m {0} \033[0m".format(names[i])
|
formatted_name = "\033[92m {0} \033[0m".format(names[i])
|
||||||
else:
|
else:
|
||||||
formatted_name = "\033[91m {0} \033[0m".format(names[i])
|
formatted_name = "\033[91m {0} \033[0m".format(names[i])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue