First attempt at making coregionalise work with the sparse model

Gradients are failing! have implemented prod_othogonal.dKdiag_dtheta
This commit is contained in:
James Hensman 2013-03-06 15:29:03 +00:00
parent fc34fa3eb9
commit 65f9c7bb76
3 changed files with 82 additions and 16 deletions

View file

@ -46,8 +46,8 @@ class coregionalise(kernpart):
index2 = index
else:
index2 = np.asarray(index2,dtype=np.int)
ii,jj = np.meshgrid(index,index2)
target += self.B[ii,jj].T
ii,jj = np.meshgrid(index2,index)
target += self.B[ii,jj]
def Kdiag(self,index,target):
target += np.diag(self.B)[np.asarray(index,dtype=np.int).flatten()]
@ -58,26 +58,47 @@ class coregionalise(kernpart):
index2 = index
else:
index2 = np.asarray(index2,dtype=np.int)
ii,jj = np.meshgrid(index,index2)
ii,jj = np.meshgrid(index2,index)
PK = np.zeros((self.R,self.R))
dkappa = np.zeros(self.Nout)
partial_small = np.zeros_like(self.B)
for i in range(self.Nout):
for j in range(self.Nout):
partial_small[j,i] = np.sum(partial[(ii==i)*(jj==j)])
#print partial_small
partial_small[i,j] = np.sum(partial[(ii==i)*(jj==j)])
dkappa = np.diag(partial_small)
##target += (((X2[:, None, :] * self.variances)) * partial[:,:, None]).sum(0)
dW = 2.*(self.W[:,None,:]*partial_small[:,:,None]).sum(0)
target += np.hstack([dW.flatten(),dkappa])
def dKdiag_dtheta(self,partial,index,target):
raise NotImplementedError
index = np.asarray(index,dtype=np.int).flatten()
partial_small = np.zeros(self.Nout)
for i in range(self.Nout):
partial_small[i] += np.sum(partial[index==i])
dW = 2.*self.W*partial_small[:,None]
dkappa = partial_small
target += np.hstack([dW.flatten(),dkappa])
def dK_dX(self,partial,X,X2,target):
pass
def dKdiag_dthetai_(self,partial,index,target):
index = np.asarray(index,dtype=np.int)
index2 = index
ii,jj = np.meshgrid(index2,index)
PK = np.zeros((self.R,self.R))
partial_small = np.zeros_like(self.B)
for i in range(self.Nout):
for j in range(self.Nout):
partial_small[j,i] = np.sum(partial[np.diag((ii==i)*(jj==j))])
#print partial_small
dkappa = np.diag(partial_small)
##target += (((X2[:, None, :] * self.variances)) * partial[:,:, None]).sum(0)
partial_small = np.diag(np.diag(partial_small))
#dW = 2.*(self.W[:,None,:]*partial_small[:,:,None]).sum(0)
dW = 2.
target += np.hstack([dW.flatten(),dkappa])

View file

@ -46,14 +46,6 @@ class product_orthogonal(kernpart):
self.k2.K(X[:,self.k1.D:],X2[:,self.k1.D:],target2)
target += target1 * target2
def Kdiag(self,X,target):
"""Compute the diagonal of the covariance matrix associated to X."""
target1 = np.zeros((X.shape[0],))
target2 = np.zeros((X.shape[0],))
self.k1.Kdiag(X[:,0:self.k1.D],target1)
self.k2.Kdiag(X[:,self.k1.D:],target2)
target += target1 * target2
def dK_dtheta(self,partial,X,X2,target):
"""derivative of the covariance matrix with respect to the parameters."""
if X2 is None: X2 = X
@ -70,6 +62,22 @@ class product_orthogonal(kernpart):
target[:self.k1.Nparam] += k1_target
target[self.k1.Nparam:] += k2_target
def Kdiag(self,X,target):
"""Compute the diagonal of the covariance matrix associated to X."""
target1 = np.zeros((X.shape[0],))
target2 = np.zeros((X.shape[0],))
self.k1.Kdiag(X[:,:self.k1.D],target1)
self.k2.Kdiag(X[:,self.k1.D:],target2)
target += target1 * target2
def dKdiag_dtheta(self,partial,X,target):
K1 = np.zeros(X.shape[0])
K2 = np.zeros(X.shape[0])
self.k1.Kdiag(X[:,:self.k1.D],K1)
self.k2.Kdiag(X[:,self.k1.D:],K2)
self.k1.dKdiag_dtheta(partial*K2,X[:,:self.k1.D],target[:self.k1.Nparam])
self.k2.dKdiag_dtheta(partial*K1,X[:,self.k1.D:],target[self.k1.Nparam:])
def dK_dX(self,partial,X,X2,target):
"""derivative of the covariance matrix with respect to X."""
if X2 is None: X2 = X