mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-08 11:32:39 +02:00
non essential tidying in stationary
This commit is contained in:
parent
7ae9d03c45
commit
7aad39e70e
1 changed files with 10 additions and 14 deletions
|
|
@ -74,16 +74,10 @@ class Stationary(Kern):
|
||||||
r = self._scaled_dist(X, X2)
|
r = self._scaled_dist(X, X2)
|
||||||
return self.K_of_r(r)
|
return self.K_of_r(r)
|
||||||
|
|
||||||
#@Cache_this(limit=5, ignore_args=(0,))
|
|
||||||
def _dist(self, X, X2):
|
|
||||||
if X2 is None:
|
|
||||||
X2 = X
|
|
||||||
return X[:, None, :] - X2[None, :, :]
|
|
||||||
|
|
||||||
#@Cache_this(limit=5, ignore_args=(0,))
|
#@Cache_this(limit=5, ignore_args=(0,))
|
||||||
def _unscaled_dist(self, X, X2=None):
|
def _unscaled_dist(self, X, X2=None):
|
||||||
"""
|
"""
|
||||||
Compute the square distance between each row of X and X2, or between
|
Compute the Euclidean distance between each row of X and X2, or between
|
||||||
each pair of rows of X if X2 is None.
|
each pair of rows of X if X2 is None.
|
||||||
"""
|
"""
|
||||||
if X2 is None:
|
if X2 is None:
|
||||||
|
|
@ -99,7 +93,7 @@ class Stationary(Kern):
|
||||||
"""
|
"""
|
||||||
Efficiently compute the scaled distance, r.
|
Efficiently compute the scaled distance, r.
|
||||||
|
|
||||||
r = \sum_{q=1}^Q (x_q - x'q)^2/l_q^2
|
r = \sqrt( \sum_{q=1}^Q (x_q - x'q)^2/l_q^2 )
|
||||||
|
|
||||||
Note that if thre is only one lengthscale, l comes outside the sum. In
|
Note that if thre is only one lengthscale, l comes outside the sum. In
|
||||||
this case we compute the unscaled distance first (in a separate
|
this case we compute the unscaled distance first (in a separate
|
||||||
|
|
@ -129,10 +123,10 @@ class Stationary(Kern):
|
||||||
dL_dr = self.dK_dr(r) * dL_dK
|
dL_dr = self.dK_dr(r) * dL_dK
|
||||||
|
|
||||||
if self.ARD:
|
if self.ARD:
|
||||||
#rinv = self._inv_dist(X, X2)
|
#rinv = self._inv_dis# this is rather high memory? Should we loop instead?t(X, X2)
|
||||||
#x_xl3 = np.square(self._dist(X, X2)) # TODO: this is rather high memory? Should we loop instead?
|
#d = X[:, None, :] - X2[None, :, :]
|
||||||
|
#x_xl3 = np.square(d)
|
||||||
#self.lengthscale.gradient = -((dL_dr*rinv)[:,:,None]*x_xl3).sum(0).sum(0)/self.lengthscale**3
|
#self.lengthscale.gradient = -((dL_dr*rinv)[:,:,None]*x_xl3).sum(0).sum(0)/self.lengthscale**3
|
||||||
self.lengthscale.gradient = np.zeros(self.input_dim)
|
|
||||||
tmp = dL_dr*self._inv_dist(X, X2)
|
tmp = dL_dr*self._inv_dist(X, X2)
|
||||||
if X2 is None: X2 = X
|
if X2 is None: X2 = X
|
||||||
[np.copyto(self.lengthscale.gradient[q:q+1], -np.sum(tmp * np.square(X[:,q:q+1] - X2[:,q:q+1].T))/self.lengthscale[q]**3) for q in xrange(self.input_dim)]
|
[np.copyto(self.lengthscale.gradient[q:q+1], -np.sum(tmp * np.square(X[:,q:q+1] - X2[:,q:q+1].T))/self.lengthscale[q]**3) for q in xrange(self.input_dim)]
|
||||||
|
|
@ -162,7 +156,9 @@ class Stationary(Kern):
|
||||||
r = self._scaled_dist(X, X2)
|
r = self._scaled_dist(X, X2)
|
||||||
invdist = self._inv_dist(X, X2)
|
invdist = self._inv_dist(X, X2)
|
||||||
dL_dr = self.dK_dr(r) * dL_dK
|
dL_dr = self.dK_dr(r) * dL_dK
|
||||||
#The high-memory numpy way: ret = np.sum((invdist*dL_dr)[:,:,None]*self._dist(X, X2),1)/self.lengthscale**2
|
#The high-memory numpy way:
|
||||||
|
#d = X[:, None, :] - X2[None, :, :]
|
||||||
|
#ret = np.sum((invdist*dL_dr)[:,:,None]*d,1)/self.lengthscale**2
|
||||||
#if X2 is None:
|
#if X2 is None:
|
||||||
#ret *= 2.
|
#ret *= 2.
|
||||||
|
|
||||||
|
|
@ -245,7 +241,7 @@ class Matern52(Stationary):
|
||||||
|
|
||||||
.. math::
|
.. math::
|
||||||
|
|
||||||
k(r) = \sigma^2 (1 + \sqrt{5} r + \\frac53 r^2) \exp(- \sqrt{5} r) \ \ \ \ \ \\text{ where } r = \sqrt{\sum_{i=1}^input_dim \\frac{(x_i-y_i)^2}{\ell_i^2} }
|
k(r) = \sigma^2 (1 + \sqrt{5} r + \\frac53 r^2) \exp(- \sqrt{5} r)
|
||||||
"""
|
"""
|
||||||
def __init__(self, input_dim, variance=1., lengthscale=None, ARD=False, name='Mat52'):
|
def __init__(self, input_dim, variance=1., lengthscale=None, ARD=False, name='Mat52'):
|
||||||
super(Matern52, self).__init__(input_dim, variance, lengthscale, ARD, name)
|
super(Matern52, self).__init__(input_dim, variance, lengthscale, ARD, name)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue