mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-05-11 04:52:37 +02:00
R paramter renamed as W_columns, and Nout renamed as num_outputs
This commit is contained in:
parent
c941da9d3c
commit
81eb22dffd
1 changed files with 34 additions and 29 deletions
|
|
@ -9,24 +9,32 @@ from scipy import weave
|
||||||
|
|
||||||
class Coregionalise(Kernpart):
|
class Coregionalise(Kernpart):
|
||||||
"""
|
"""
|
||||||
Kernel for Intrinsic Corregionalization Models
|
Kernel for Intrinsic Coregionalization Models
|
||||||
|
|
||||||
|
This kernel has the form: K = np.dot(W,W.T) + np.diag(kappa)
|
||||||
|
An intrinsec coregionalization kernel is obtained as the tensor product between a different kernel and the coregionalize kernel.
|
||||||
|
|
||||||
|
:param num_outputs: number of outputs to coregionalize
|
||||||
|
:param W_columns: number of columns of the W matrix (this parameter is ignored if parameter W is not None)
|
||||||
|
:param W: array of shape (num_outputs, W_columns)
|
||||||
|
:param kappa: array of dimensions (num_outputs,)
|
||||||
"""
|
"""
|
||||||
def __init__(self,Nout,R=1, W=None, kappa=None):
|
def __init__(self,num_outputs,W_columns=1, W=None, kappa=None):
|
||||||
self.input_dim = 1
|
self.input_dim = 1
|
||||||
self.name = 'coregion'
|
self.name = 'coregion'
|
||||||
self.Nout = Nout
|
self.num_outputs = num_outputs
|
||||||
self.R = R
|
self.W_columns = W_columns
|
||||||
if W is None:
|
if W is None:
|
||||||
self.W = np.ones((self.Nout,self.R))
|
self.W = np.ones((self.num_outputs,self.W_columns))
|
||||||
else:
|
else:
|
||||||
assert W.shape==(self.Nout,self.R)
|
assert W.shape==(self.num_outputs,self.W_columns)
|
||||||
self.W = W
|
self.W = W
|
||||||
if kappa is None:
|
if kappa is None:
|
||||||
kappa = np.ones(self.Nout)
|
kappa = np.ones(self.num_outputs)
|
||||||
else:
|
else:
|
||||||
assert kappa.shape==(self.Nout,)
|
assert kappa.shape==(self.num_outputs,)
|
||||||
self.kappa = kappa
|
self.kappa = kappa
|
||||||
self.num_params = self.Nout*(self.R + 1)
|
self.num_params = self.num_outputs*(self.W_columns + 1)
|
||||||
self._set_params(np.hstack([self.W.flatten(),self.kappa]))
|
self._set_params(np.hstack([self.W.flatten(),self.kappa]))
|
||||||
|
|
||||||
def _get_params(self):
|
def _get_params(self):
|
||||||
|
|
@ -34,12 +42,12 @@ class Coregionalise(Kernpart):
|
||||||
|
|
||||||
def _set_params(self,x):
|
def _set_params(self,x):
|
||||||
assert x.size == self.num_params
|
assert x.size == self.num_params
|
||||||
self.kappa = x[-self.Nout:]
|
self.kappa = x[-self.num_outputs:]
|
||||||
self.W = x[:-self.Nout].reshape(self.Nout,self.R)
|
self.W = x[:-self.num_outputs].reshape(self.num_outputs,self.W_columns)
|
||||||
self.B = np.dot(self.W,self.W.T) + np.diag(self.kappa)
|
self.B = np.dot(self.W,self.W.T) + np.diag(self.kappa)
|
||||||
|
|
||||||
def _get_param_names(self):
|
def _get_param_names(self):
|
||||||
return sum([['W%i_%i'%(i,j) for j in range(self.R)] for i in range(self.Nout)],[]) + ['kappa_%i'%i for i in range(self.Nout)]
|
return sum([['W%i_%i'%(i,j) for j in range(self.W_columns)] for i in range(self.num_outputs)],[]) + ['kappa_%i'%i for i in range(self.num_outputs)]
|
||||||
|
|
||||||
def K(self,index,index2,target):
|
def K(self,index,index2,target):
|
||||||
index = np.asarray(index,dtype=np.int)
|
index = np.asarray(index,dtype=np.int)
|
||||||
|
|
@ -57,26 +65,26 @@ class Coregionalise(Kernpart):
|
||||||
if index2 is None:
|
if index2 is None:
|
||||||
code="""
|
code="""
|
||||||
for(int i=0;i<N; i++){
|
for(int i=0;i<N; i++){
|
||||||
target[i+i*N] += B[index[i]+Nout*index[i]];
|
target[i+i*N] += B[index[i]+num_outputs*index[i]];
|
||||||
for(int j=0; j<i; j++){
|
for(int j=0; j<i; j++){
|
||||||
target[j+i*N] += B[index[i]+Nout*index[j]];
|
target[j+i*N] += B[index[i]+num_outputs*index[j]];
|
||||||
target[i+j*N] += target[j+i*N];
|
target[i+j*N] += target[j+i*N];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
N,B,Nout = index.size, self.B, self.Nout
|
N,B,num_outputs = index.size, self.B, self.num_outputs
|
||||||
weave.inline(code,['target','index','N','B','Nout'])
|
weave.inline(code,['target','index','N','B','num_outputs'])
|
||||||
else:
|
else:
|
||||||
index2 = np.asarray(index2,dtype=np.int)
|
index2 = np.asarray(index2,dtype=np.int)
|
||||||
code="""
|
code="""
|
||||||
for(int i=0;i<num_inducing; i++){
|
for(int i=0;i<num_inducing; i++){
|
||||||
for(int j=0; j<N; j++){
|
for(int j=0; j<N; j++){
|
||||||
target[i+j*num_inducing] += B[Nout*index[j]+index2[i]];
|
target[i+j*num_inducing] += B[num_outputs*index[j]+index2[i]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
N,num_inducing,B,Nout = index.size,index2.size, self.B, self.Nout
|
N,num_inducing,B,num_outputs = index.size,index2.size, self.B, self.num_outputs
|
||||||
weave.inline(code,['target','index','index2','N','num_inducing','B','Nout'])
|
weave.inline(code,['target','index','index2','N','num_inducing','B','num_outputs'])
|
||||||
|
|
||||||
|
|
||||||
def Kdiag(self,index,target):
|
def Kdiag(self,index,target):
|
||||||
|
|
@ -93,12 +101,12 @@ class Coregionalise(Kernpart):
|
||||||
code="""
|
code="""
|
||||||
for(int i=0; i<num_inducing; i++){
|
for(int i=0; i<num_inducing; i++){
|
||||||
for(int j=0; j<N; j++){
|
for(int j=0; j<N; j++){
|
||||||
dL_dK_small[index[j] + Nout*index2[i]] += dL_dK[i+j*num_inducing];
|
dL_dK_small[index[j] + num_outputs*index2[i]] += dL_dK[i+j*num_inducing];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
N, num_inducing, Nout = index.size, index2.size, self.Nout
|
N, num_inducing, num_outputs = index.size, index2.size, self.num_outputs
|
||||||
weave.inline(code, ['N','num_inducing','Nout','dL_dK','dL_dK_small','index','index2'])
|
weave.inline(code, ['N','num_inducing','num_outputs','dL_dK','dL_dK_small','index','index2'])
|
||||||
|
|
||||||
dkappa = np.diag(dL_dK_small)
|
dkappa = np.diag(dL_dK_small)
|
||||||
dL_dK_small += dL_dK_small.T
|
dL_dK_small += dL_dK_small.T
|
||||||
|
|
@ -115,8 +123,8 @@ class Coregionalise(Kernpart):
|
||||||
ii,jj = ii.T, jj.T
|
ii,jj = ii.T, jj.T
|
||||||
|
|
||||||
dL_dK_small = np.zeros_like(self.B)
|
dL_dK_small = np.zeros_like(self.B)
|
||||||
for i in range(self.Nout):
|
for i in range(self.num_outputs):
|
||||||
for j in range(self.Nout):
|
for j in range(self.num_outputs):
|
||||||
tmp = np.sum(dL_dK[(ii==i)*(jj==j)])
|
tmp = np.sum(dL_dK[(ii==i)*(jj==j)])
|
||||||
dL_dK_small[i,j] = tmp
|
dL_dK_small[i,j] = tmp
|
||||||
|
|
||||||
|
|
@ -128,8 +136,8 @@ class Coregionalise(Kernpart):
|
||||||
|
|
||||||
def dKdiag_dtheta(self,dL_dKdiag,index,target):
|
def dKdiag_dtheta(self,dL_dKdiag,index,target):
|
||||||
index = np.asarray(index,dtype=np.int).flatten()
|
index = np.asarray(index,dtype=np.int).flatten()
|
||||||
dL_dKdiag_small = np.zeros(self.Nout)
|
dL_dKdiag_small = np.zeros(self.num_outputs)
|
||||||
for i in range(self.Nout):
|
for i in range(self.num_outputs):
|
||||||
dL_dKdiag_small[i] += np.sum(dL_dKdiag[index==i])
|
dL_dKdiag_small[i] += np.sum(dL_dKdiag[index==i])
|
||||||
dW = 2.*self.W*dL_dKdiag_small[:,None]
|
dW = 2.*self.W*dL_dKdiag_small[:,None]
|
||||||
dkappa = dL_dKdiag_small
|
dkappa = dL_dKdiag_small
|
||||||
|
|
@ -137,6 +145,3 @@ class Coregionalise(Kernpart):
|
||||||
|
|
||||||
def dK_dX(self,dL_dK,X,X2,target):
|
def dK_dX(self,dL_dK,X,X2,target):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue