mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-15 06:52:39 +02:00
gradient checker comments and import updates
This commit is contained in:
parent
77a0d61bf6
commit
4be3f4482d
2 changed files with 25 additions and 25 deletions
|
|
@ -28,38 +28,37 @@ class GradientChecker(Model):
|
||||||
:param df: Gradient of function to check
|
: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).
|
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.
|
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
|
: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
|
Names to print, when performing gradcheck. If a list was passed to x0
|
||||||
a list of names with the same length is expected.
|
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:
|
Examples:
|
||||||
---------
|
---------
|
||||||
from GPy.models import GradientChecker
|
from GPy.models import GradientChecker
|
||||||
N, M, Q = 10, 5, 3
|
N, M, Q = 10, 5, 3
|
||||||
|
|
||||||
Sinusoid:
|
Sinusoid:
|
||||||
|
|
||||||
X = numpy.random.rand(N, Q)
|
X = numpy.random.rand(N, Q)
|
||||||
grad = GradientChecker(numpy.sin,numpy.cos,X,'x')
|
grad = GradientChecker(numpy.sin,numpy.cos,X,'sin_in')
|
||||||
grad.checkgrad(verbose=1)
|
grad.checkgrad(verbose=1)
|
||||||
|
|
||||||
Using GPy:
|
Using GPy:
|
||||||
|
|
||||||
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)
|
||||||
grad = GradientChecker(kern.K,
|
grad = GradientChecker(kern.K,
|
||||||
lambda x: 2*kern.dK_dX(numpy.ones((1,1)), x),
|
lambda x: kern.dK_dX(numpy.ones((1,1)), x),
|
||||||
x0 = X.copy(),
|
x0 = X.copy(),
|
||||||
names='X')
|
names=['X_input'])
|
||||||
grad.checkgrad(verbose=1)
|
grad.checkgrad(verbose=1)
|
||||||
grad.randomize()
|
grad.randomize()
|
||||||
grad.checkgrad(verbose=1)
|
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:
|
||||||
|
|
|
||||||
|
|
@ -737,15 +737,16 @@ class kern(Parameterized):
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError, "Cannot plot a kernel with more than two input dimensions"
|
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):
|
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."""
|
"""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):
|
def __init__(self, kernel=None, dL_dK=None, X=None, X2=None):
|
||||||
num_samples = 20
|
num_samples = 20
|
||||||
num_samples2 = 10
|
num_samples2 = 10
|
||||||
if kernel==None:
|
if kernel==None:
|
||||||
|
import GPy
|
||||||
kernel = GPy.kern.rbf(1)
|
kernel = GPy.kern.rbf(1)
|
||||||
|
del GPy
|
||||||
if X==None:
|
if X==None:
|
||||||
X = np.random.normal(size=(num_samples, kernel.input_dim))
|
X = np.random.normal(size=(num_samples, kernel.input_dim))
|
||||||
if dL_dK==None:
|
if dL_dK==None:
|
||||||
|
|
@ -760,7 +761,7 @@ class Kern_check_model(Model):
|
||||||
self.dL_dK = dL_dK
|
self.dL_dK = dL_dK
|
||||||
#self.constrained_indices=[]
|
#self.constrained_indices=[]
|
||||||
#self.constraints=[]
|
#self.constraints=[]
|
||||||
Model.__init__(self)
|
super(Kern_check_model, self).__init__()
|
||||||
|
|
||||||
def is_positive_definite(self):
|
def is_positive_definite(self):
|
||||||
v = np.linalg.eig(self.kernel.K(self.X))[0]
|
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:
|
if output_ind is not None:
|
||||||
assert(output_ind<kern.input_dim)
|
assert(output_ind<kern.input_dim)
|
||||||
X[:, output_ind] = np.random.randint(low=0,high=kern.parts[0].output_dim, size=X.shape[0])
|
X[:, output_ind] = np.random.randint(low=0,high=kern.parts[0].output_dim, size=X.shape[0])
|
||||||
import ipdb; ipdb.set_trace()
|
|
||||||
if X2==None:
|
if X2==None:
|
||||||
X2 = np.random.randn(20, kern.input_dim)
|
X2 = np.random.randn(20, kern.input_dim)
|
||||||
if X_positive:
|
if X_positive:
|
||||||
|
|
@ -964,3 +964,4 @@ def kern_test(kern, X=None, X2=None, output_ind=None, verbose=False, X_positive=
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return pass_checks
|
return pass_checks
|
||||||
|
del Model
|
||||||
Loading…
Add table
Add a link
Reference in a new issue