bugfix: slicing was still in stationary somehow

This commit is contained in:
Max Zwiessele 2014-04-16 12:19:40 +01:00
parent a1664f50ba
commit 5fb9ce9c53
3 changed files with 9 additions and 6 deletions

View file

@ -25,7 +25,7 @@ class Kern(Parameterized):
is the number of dimensions to work on. Make sure to give the
tight dimensionality of inputs.
You moset likely want this to be the integer telling the number of
You most likely want this to be the integer telling the number of
input dimensions of the kernel.
If this is not an integer (!) we will work on the whole input matrix X,
and not check whether dimensions match or not (!).
@ -44,7 +44,7 @@ class Kern(Parameterized):
super(Kern, self).__init__(name=name, *a, **kw)
try:
self.input_dim = int(input_dim)
self.active_dims = active_dims if active_dims is not None else slice(0, input_dim, 1)
self.active_dims = active_dims# if active_dims is not None else slice(0, input_dim, 1)
except TypeError:
# input_dim is something else then an integer
self.input_dim = input_dim
@ -231,7 +231,9 @@ class CombinationKernel(Kern):
def get_input_dim_active_dims(self, kernels, extra_dims = None):
#active_dims = reduce(np.union1d, (np.r_[x.active_dims] for x in kernels), np.array([], dtype=int))
#active_dims = np.array(np.concatenate((active_dims, extra_dims if extra_dims is not None else [])), dtype=int)
input_dim = [k.input_dim for k in kernels]
input_dim = np.array([k.input_dim for k in kernels])
if np.all(input_dim[0]==input_dim):
input_dim = input_dim[0]
active_dims = None
return input_dim, active_dims

View file

@ -139,7 +139,7 @@ class Stationary(Kern):
#self.lengthscale.gradient = -((dL_dr*rinv)[:,:,None]*x_xl3).sum(0).sum(0)/self.lengthscale**3
tmp = dL_dr*self._inv_dist(X, X2)
if X2 is None: X2 = X
self.lengthscale.gradient = np.array([np.einsum('ij,ij,...', tmp, np.square(self._slice_X(X)[:,q:q+1] - self._slice_X(X2)[:,q:q+1].T), -1./self.lengthscale[q]**3) for q in xrange(self.input_dim)])
self.lengthscale.gradient = np.array([np.einsum('ij,ij,...', tmp, np.square(X[:,q:q+1] - X2[:,q:q+1].T), -1./self.lengthscale[q]**3) for q in xrange(self.input_dim)])
else:
r = self._scaled_dist(X, X2)
self.lengthscale.gradient = -np.sum(dL_dr*r)/self.lengthscale

View file

@ -314,13 +314,14 @@ class KernelTestsMiscellaneous(unittest.TestCase):
self.sumkern.randomize()
def test_active_dims(self):
#self.assertEqual(self.sumkern.input_dim, 10)
#self.assertEqual(list(self.sumkern.active_dims), [0,1,2,3,7,9])
# test the automatic dim detection expression for slices:
start, stop = 0, 277
for i in range(start,stop,7):
for j in range(1,4):
GPy.kern.Kern(int(np.round((i+1)/j)), slice(0, i+1, j), "testkern")
# test the ability to have only one dim
sk = GPy.kern.RBF(2) + GPy.kern.Matern32(2)
self.assertEqual(sk.input_dim, 2)
def test_which_parts(self):
self.assertTrue(np.allclose(self.sumkern.K(self.X, which_parts=[self.linear, self.matern]), self.linear.K(self.X)+self.matern.K(self.X)))