diff --git a/GPy/_models/gradient_checker.py b/GPy/_models/gradient_checker.py index 64b8b2fb..dfd0640f 100644 --- a/GPy/_models/gradient_checker.py +++ b/GPy/_models/gradient_checker.py @@ -28,38 +28,37 @@ class GradientChecker(Model): :param df: Gradient of function to check :param x0: Initial guess for inputs x (if it has a shape (a,b) this will be reflected in the parameter names). - Can be a list of arrays, if takes a list of arrays. This list will be passed + Can be a list of arrays, if f takes a list of arrays. This list will be passed to f and df in the same order as given here. - If only one argument, make sure not to pass a list!!! - + If f takes only one argument, make sure not to pass a list for x0!!! :type x0: [array-like] | array-like | float | int - :param names: + :param list names: Names to print, when performing gradcheck. If a list was passed to x0 a list of names with the same length is expected. - :param args: Arguments passed as f(x, *args, **kwargs) and df(x, *args, **kwargs) + :param args kwargs: Arguments passed as f(x, *args, **kwargs) and df(x, *args, **kwargs) Examples: --------- - from GPy.models import GradientChecker - N, M, Q = 10, 5, 3 + from GPy.models import GradientChecker + N, M, Q = 10, 5, 3 - Sinusoid: + Sinusoid: - X = numpy.random.rand(N, Q) - grad = GradientChecker(numpy.sin,numpy.cos,X,'x') - grad.checkgrad(verbose=1) + X = numpy.random.rand(N, Q) + grad = GradientChecker(numpy.sin,numpy.cos,X,'sin_in') + grad.checkgrad(verbose=1) - Using GPy: + Using GPy: - X, Z = numpy.random.randn(N,Q), numpy.random.randn(M,Q) - kern = GPy.kern.linear(Q, ARD=True) + GPy.kern.rbf(Q, ARD=True) - grad = GradientChecker(kern.K, - lambda x: 2*kern.dK_dX(numpy.ones((1,1)), x), - x0 = X.copy(), - names='X') - grad.checkgrad(verbose=1) - grad.randomize() - grad.checkgrad(verbose=1) + X, Z = numpy.random.randn(N,Q), numpy.random.randn(M,Q) + kern = GPy.kern.linear(Q, ARD=True) + GPy.kern.rbf(Q, ARD=True) + grad = GradientChecker(kern.K, + lambda x: kern.dK_dX(numpy.ones((1,1)), x), + x0 = X.copy(), + names=['X_input']) + grad.checkgrad(verbose=1) + grad.randomize() + grad.checkgrad(verbose=1) """ Model.__init__(self) if isinstance(x0, (list, tuple)) and names is None: diff --git a/GPy/kern/kern.py b/GPy/kern/kern.py index f51c3c13..2c56f47a 100644 --- a/GPy/kern/kern.py +++ b/GPy/kern/kern.py @@ -737,15 +737,16 @@ class kern(Parameterized): else: raise NotImplementedError, "Cannot plot a kernel with more than two input dimensions" -from GPy.core.model import Model - +from ..core.model import Model class Kern_check_model(Model): """This is a dummy model class used as a base class for checking that the gradients of a given kernel are implemented correctly. It enables checkgradient() to be called independently on a kernel.""" def __init__(self, kernel=None, dL_dK=None, X=None, X2=None): num_samples = 20 num_samples2 = 10 if kernel==None: + import GPy kernel = GPy.kern.rbf(1) + del GPy if X==None: X = np.random.normal(size=(num_samples, kernel.input_dim)) if dL_dK==None: @@ -760,7 +761,7 @@ class Kern_check_model(Model): self.dL_dK = dL_dK #self.constrained_indices=[] #self.constraints=[] - Model.__init__(self) + super(Kern_check_model, self).__init__() def is_positive_definite(self): v = np.linalg.eig(self.kernel.K(self.X))[0] @@ -863,7 +864,6 @@ def kern_test(kern, X=None, X2=None, output_ind=None, verbose=False, X_positive= if output_ind is not None: assert(output_ind