syntax fix

This commit is contained in:
alessandratosi 2016-04-21 15:45:37 +01:00
parent 5c0c1d4c3d
commit 0e109cd3da
5 changed files with 11 additions and 12 deletions

View file

@ -86,7 +86,7 @@ class Add(CombinationKernel):
return target
def gradients_XX(self, dL_dK, X, X2, cov=True):
if cov==True: # full covarance
if cov: # full covarance
if X2 is None:
target = np.zeros((X.shape[0], X.shape[0], X.shape[1], X.shape[1]))
else:
@ -96,8 +96,7 @@ class Add(CombinationKernel):
target = np.zeros((X.shape[0], X.shape[0], X.shape[1]))
else:
target = np.zeros((X.shape[0], X2.shape[0], X.shape[1]))
[target.__iadd__(p.gradients_XX(dL_dK, X, X2, cov)) for p in self.parts]
[target.__iadd__(p.gradients_XX(dL_dK, X, X2)) for p in self.parts]
return target
def gradients_XX_diag(self, dL_dKdiag, X):

View file

@ -119,14 +119,14 @@ def _slice_gradients_XX(f):
N, M = X.shape[0], X.shape[0]
else:
N, M = X.shape[0], X2.shape[0]
if cov==True: # full covariance
if cov: # full covariance
with _Slice_wrap(self, X, X2, ret_shape=(N, M, X.shape[1], X.shape[1])) as s:
#with _Slice_wrap(self, X, X2, ret_shape=None) as s:
ret = s.handle_return_array(f(self, dL_dK, s.X, s.X2, cov=True))
ret = s.handle_return_array(f(self, dL_dK, s.X, s.X2, cov))
else: # diagonal covariance
with _Slice_wrap(self, X, X2, ret_shape=(N, M, X.shape[1])) as s:
#with _Slice_wrap(self, X, X2, ret_shape=None) as s:
ret = s.handle_return_array(f(self, dL_dK, s.X, s.X2, cov=True))
ret = s.handle_return_array(f(self, dL_dK, s.X, s.X2, cov))
return ret
return wrap

View file

@ -101,7 +101,7 @@ class Linear(Kern):
#return (((X2[None,:, :] * self.variances)) * dL_dK[:, :, None]).sum(1)
return dL_dK.dot(X2)*self.variances #np.einsum('jq,q,ij->iq', X2, self.variances, dL_dK)
def gradients_XX(self, dL_dK, X, X2=None):
def gradients_XX(self, dL_dK, X, X2=None, cov=True):
if X2 is None: dL_dK = (dL_dK+dL_dK.T)/2
if X2 is None:
return 2*np.ones(X.shape)*self.variances

View file

@ -24,7 +24,7 @@ class Static(Kern):
def gradients_X_diag(self, dL_dKdiag, X):
return np.zeros(X.shape)
def gradients_XX(self, dL_dK, X, X2):
def gradients_XX(self, dL_dK, X, X2=None, cov=True):
if X2 is None:
X2 = X
return np.zeros((X.shape[0], X2.shape[0], X.shape[1]), dtype=np.float64)

View file

@ -222,14 +222,14 @@ class Stationary(Kern):
"""
Given the derivative of the objective K(dL_dK), compute the second derivative of K wrt X and X2:
cov = Full: returns the full covariance matrix [QxQ] of the input dimensionfor each pair or vectors
cov = Diag: returns the diagonal of the covariance matrix [QxQ] of the input dimensionfor each pair
cov = True: returns the full covariance matrix [QxQ] of the input dimensionfor each pair or vectors
cov = False: returns the diagonal of the covariance matrix [QxQ] of the input dimensionfor each pair
or vectors (computationally more efficient if the full covariance matrix is not needed)
..math:
\frac{\partial^2 K}{\partial X2 ^2} = - \frac{\partial^2 K}{\partial X\partial X2}
..returns:
dL2_dXdX2: [NxMxQ] in the cov=Diag case, or [NxMxQxQ] in the cov=full case,
dL2_dXdX2: [NxMxQxQ] in the cov=True case, or [NxMxQ] in the cov=False case,
for X [NxQ] and X2[MxQ] (X2 is X if, X2 is None)
Thus, we return the second derivative in X2.
"""
@ -251,7 +251,7 @@ class Stationary(Kern):
# (seems to have a bug: it is subtracted to the first X1 anyway)
tmp1[invdist2==0.] -= self.variance
if cov==True: # full covariance
if cov: # full covariance
grad = np.empty((X.shape[0], X2.shape[0], X2.shape[1], X.shape[1]), dtype=np.float64)
for q in range(self.input_dim):
for r in range(self.input_dim):