gradient checker implemented

This commit is contained in:
Max Zwiessele 2013-07-23 13:02:20 +01:00
parent 49f91518e5
commit 01b35b1178

View file

@ -40,24 +40,28 @@ class GradientChecker(Model):
Examples: Examples:
--------- ---------
from GPy.models import GradientChecker
N, M, Q = 10, 5, 3
Sinusoid: Sinusoid:
X = numpy.random.rand(N, Q) X = numpy.random.rand(N, Q)
f = lambda x: numpy.sin(x) grad = GradientChecker(numpy.sin,numpy.cos,X,'x')
df = lambda x: numpy.cos(x) grad.checkgrad(verbose=1)
grad = gc.GradientChecker(f,df,X,'x') grad.randomize()
grad.checkgrad(verbose=1)
Using GPy: Using GPy:
N, M, Q = 10, 5, 3
X, Z = numpy.random.randn(N,Q), numpy.random.randn(M,Q) 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) kern = GPy.kern.linear(Q, ARD=True) + GPy.kern.rbf(Q, ARD=True)
import GPy.models.gradient_checker as gc grad = GradientChecker(kern.K,
grad = gc.GradientChecker(kern.K, lambda x: 2*kern.dK_dX(numpy.ones((1,1)), x),
lambda x: 2*kern.dK_dX(numpy.ones((1,1)), x), x0 = X.copy(),
x0 = X.copy(), names='X')
names='X') grad.checkgrad(verbose=1)
grad.randomize()
grad.checkgrad(verbose=1)
""" """
Model.__init__(self) Model.__init__(self)
if isinstance(x0, (list, tuple)) and names is None: if isinstance(x0, (list, tuple)) and names is None: