From 64e65b846d8b7eafc1abe66d735a4dbf2dfa540c Mon Sep 17 00:00:00 2001 From: Alan Saul Date: Wed, 11 Sep 2013 11:54:47 +0100 Subject: [PATCH] Modified gradient_checker to allow for variable 'f' --- GPy/models/gradient_checker.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/GPy/models/gradient_checker.py b/GPy/models/gradient_checker.py index 5afcd7c4..face9589 100644 --- a/GPy/models/gradient_checker.py +++ b/GPy/models/gradient_checker.py @@ -26,40 +26,40 @@ class GradientChecker(Model): """ :param f: Function to check gradient for :param df: Gradient of function to check - :param x0: + :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 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!!! - + :type x0: [array-like] | array-like | float | int :param 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) - + Examples: --------- from GPy.models import GradientChecker N, M, Q = 10, 5, 3 - + Sinusoid: - + X = numpy.random.rand(N, Q) grad = GradientChecker(numpy.sin,numpy.cos,X,'x') grad.checkgrad(verbose=1) - + 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, + grad = GradientChecker(kern.K, lambda x: 2*kern.dK_dX(numpy.ones((1,1)), x), x0 = X.copy(), - names='X') + names='X') grad.checkgrad(verbose=1) grad.randomize() - grad.checkgrad(verbose=1) + grad.checkgrad(verbose=1) """ Model.__init__(self) if isinstance(x0, (list, tuple)) and names is None: @@ -81,8 +81,8 @@ class GradientChecker(Model): # self._param_names.extend(map(lambda nameshape: ('_'.join(nameshape)).strip('_'), itertools.izip(itertools.repeat(name), itertools.imap(lambda t: '_'.join(map(str, t)), itertools.product(*map(lambda xi: range(xi), shape)))))) self.args = args self.kwargs = kwargs - self.f = f - self.df = df + self._f = f + self._df = df def _get_x(self): if len(self.names) > 1: @@ -90,10 +90,10 @@ class GradientChecker(Model): return [self.__getattribute__(self.names[0])] + list(self.args) def log_likelihood(self): - return float(numpy.sum(self.f(*self._get_x(), **self.kwargs))) + return float(numpy.sum(self._f(*self._get_x(), **self.kwargs))) def _log_likelihood_gradients(self): - return numpy.atleast_1d(self.df(*self._get_x(), **self.kwargs)).flatten() + return numpy.atleast_1d(self._df(*self._get_x(), **self.kwargs)).flatten() def _get_params(self):