2012-11-29 16:39:20 +00:00
|
|
|
# Copyright (c) 2012, GPy authors (see AUTHORS.txt).
|
|
|
|
|
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
|
|
|
|
|
|
|
|
|
|
2012-11-29 16:31:48 +00:00
|
|
|
import numpy as np
|
2013-04-10 15:50:31 +01:00
|
|
|
from scipy import weave
|
2014-02-19 15:00:48 +00:00
|
|
|
from kern import Kern
|
2014-02-19 17:37:18 +00:00
|
|
|
from ...util.linalg import tdot
|
|
|
|
|
from ...util.misc import fast_array_equal, param_to_array
|
|
|
|
|
from ...core.parameterization import Param
|
|
|
|
|
from ...core.parameterization.transformations import Logexp
|
2014-02-24 14:55:16 +00:00
|
|
|
from stationary import Stationary
|
2012-11-29 16:31:48 +00:00
|
|
|
|
2014-02-24 14:55:16 +00:00
|
|
|
class RBF(Stationary):
|
2012-11-29 16:31:48 +00:00
|
|
|
"""
|
2013-01-18 17:43:32 +00:00
|
|
|
Radial Basis Function kernel, aka squared-exponential, exponentiated quadratic or Gaussian kernel:
|
2012-12-05 19:19:15 -08:00
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
2014-02-24 14:55:16 +00:00
|
|
|
k(r) = \sigma^2 \exp \\bigg(- \\frac{1}{2} r^2 \\bigg)
|
2012-12-05 19:19:15 -08:00
|
|
|
|
2012-11-29 16:31:48 +00:00
|
|
|
"""
|
|
|
|
|
|
2014-02-24 14:55:16 +00:00
|
|
|
def __init__(self, input_dim, variance=1., lengthscale=None, ARD=False, name='RBF'):
|
|
|
|
|
super(RBF, self).__init__(input_dim, variance, lengthscale, ARD, name)
|
|
|
|
|
self.weave_options = {}
|
2014-02-19 15:00:48 +00:00
|
|
|
|
2014-02-24 14:55:16 +00:00
|
|
|
def K_of_r(self, r):
|
|
|
|
|
return self.variance * np.exp(-0.5 * r**2)
|
2014-01-24 12:13:55 +00:00
|
|
|
|
2014-02-24 14:55:16 +00:00
|
|
|
def dK_dr(self, r):
|
|
|
|
|
return -r*self.K_of_r(r)
|
2014-01-24 12:13:55 +00:00
|
|
|
|
2014-02-24 14:55:16 +00:00
|
|
|
#---------------------------------------#
|
|
|
|
|
# PSI statistics #
|
|
|
|
|
#---------------------------------------#
|
2014-01-24 12:13:55 +00:00
|
|
|
|
2013-10-17 14:38:43 +01:00
|
|
|
def parameters_changed(self):
|
2013-10-22 13:38:29 +01:00
|
|
|
# reset cached results
|
2013-11-03 13:58:15 +00:00
|
|
|
self._Z, self._mu, self._S = np.empty(shape=(3, 1)) # cached versions of Z,mu,S
|
2012-11-29 16:31:48 +00:00
|
|
|
|
|
|
|
|
|
2014-02-24 19:31:13 +00:00
|
|
|
def psi0(self, Z, variational_posterior):
|
|
|
|
|
return self.Kdiag(variational_posterior.mean)
|
2014-01-24 14:06:16 +00:00
|
|
|
|
2014-02-24 19:31:13 +00:00
|
|
|
def psi1(self, Z, variational_posterior):
|
|
|
|
|
mu = variational_posterior.mean
|
|
|
|
|
S = variational_posterior.variance
|
2014-01-24 14:06:16 +00:00
|
|
|
self._psi_computations(Z, mu, S)
|
2014-02-20 14:04:16 +00:00
|
|
|
return self._psi1
|
2014-01-24 14:06:16 +00:00
|
|
|
|
2014-02-24 19:31:13 +00:00
|
|
|
def psi2(self, Z, variational_posterior):
|
|
|
|
|
mu = variational_posterior.mean
|
|
|
|
|
S = variational_posterior.variance
|
2014-01-24 14:06:16 +00:00
|
|
|
self._psi_computations(Z, mu, S)
|
2014-02-20 14:04:16 +00:00
|
|
|
return self._psi2
|
2014-01-24 14:06:16 +00:00
|
|
|
|
2014-02-24 19:31:13 +00:00
|
|
|
def update_gradients_variational(self, dL_dKmm, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variational_posterior):
|
2014-02-24 14:55:16 +00:00
|
|
|
#contributions from Kmm
|
|
|
|
|
sself.update_gradients_full(dL_dKmm, Z)
|
|
|
|
|
|
2014-02-24 19:31:13 +00:00
|
|
|
mu = variational_posterior.mean
|
|
|
|
|
S = variational_posterior.variance
|
2014-01-24 12:13:55 +00:00
|
|
|
self._psi_computations(Z, mu, S)
|
2014-02-24 14:55:16 +00:00
|
|
|
l2 = self.lengthscale **2
|
2014-01-24 12:13:55 +00:00
|
|
|
|
|
|
|
|
#contributions from psi0:
|
2014-02-24 14:55:16 +00:00
|
|
|
self.variance.gradient += np.sum(dL_dpsi0)
|
2014-01-24 12:13:55 +00:00
|
|
|
|
|
|
|
|
#from psi1
|
|
|
|
|
self.variance.gradient += np.sum(dL_dpsi1 * self._psi1 / self.variance)
|
|
|
|
|
d_length = self._psi1[:,:,None] * ((self._psi1_dist_sq - 1.)/(self.lengthscale*self._psi1_denom) +1./self.lengthscale)
|
|
|
|
|
dpsi1_dlength = d_length * dL_dpsi1[:, :, None]
|
|
|
|
|
if not self.ARD:
|
2014-02-24 14:55:16 +00:00
|
|
|
self.lengthscale.gradient += dpsi1_dlength.sum()
|
2014-01-24 12:13:55 +00:00
|
|
|
else:
|
2014-02-24 14:55:16 +00:00
|
|
|
self.lengthscale.gradient += dpsi1_dlength.sum(0).sum(0)
|
2014-01-24 12:13:55 +00:00
|
|
|
|
|
|
|
|
#from psi2
|
|
|
|
|
d_var = 2.*self._psi2 / self.variance
|
2014-02-24 14:55:16 +00:00
|
|
|
d_length = 2.*self._psi2[:, :, :, None] * (self._psi2_Zdist_sq * self._psi2_denom + self._psi2_mudist_sq + S[:, None, None, :] / l2) / (self.lengthscale * self._psi2_denom)
|
2014-01-24 12:13:55 +00:00
|
|
|
|
|
|
|
|
self.variance.gradient += np.sum(dL_dpsi2 * d_var)
|
|
|
|
|
dpsi2_dlength = d_length * dL_dpsi2[:, :, :, None]
|
|
|
|
|
if not self.ARD:
|
|
|
|
|
self.lengthscale.gradient += dpsi2_dlength.sum()
|
|
|
|
|
else:
|
|
|
|
|
self.lengthscale.gradient += dpsi2_dlength.sum(0).sum(0).sum(0)
|
|
|
|
|
|
2014-02-24 19:31:13 +00:00
|
|
|
def gradients_Z_variational(self, dL_dKmm, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variational_posterior):
|
|
|
|
|
mu = variational_posterior.mean
|
|
|
|
|
S = variational_posterior.variance
|
2014-02-20 14:04:16 +00:00
|
|
|
self._psi_computations(Z, mu, S)
|
2014-02-24 14:55:16 +00:00
|
|
|
l2 = self.lengthscale **2
|
2014-02-20 14:04:16 +00:00
|
|
|
|
|
|
|
|
#psi1
|
2014-02-24 14:55:16 +00:00
|
|
|
denominator = (l2 * (self._psi1_denom))
|
2014-02-20 14:04:16 +00:00
|
|
|
dpsi1_dZ = -self._psi1[:, :, None] * ((self._psi1_dist / denominator))
|
|
|
|
|
grad = np.sum(dL_dpsi1[:, :, None] * dpsi1_dZ, 0)
|
|
|
|
|
|
|
|
|
|
#psi2
|
2014-02-24 14:55:16 +00:00
|
|
|
term1 = self._psi2_Zdist / l2 # num_inducing, num_inducing, input_dim
|
|
|
|
|
term2 = self._psi2_mudist / self._psi2_denom / l2 # N, num_inducing, num_inducing, input_dim
|
2014-02-20 14:04:16 +00:00
|
|
|
dZ = self._psi2[:, :, :, None] * (term1[None] + term2)
|
2014-02-21 12:29:28 +00:00
|
|
|
grad += 2*(dL_dpsi2[:, :, :, None] * dZ).sum(0).sum(0)
|
2014-02-20 14:04:16 +00:00
|
|
|
|
2014-02-20 17:11:44 +00:00
|
|
|
grad += self.gradients_X(dL_dKmm, Z, None)
|
|
|
|
|
|
2014-02-20 14:04:16 +00:00
|
|
|
return grad
|
|
|
|
|
|
2014-02-24 19:31:13 +00:00
|
|
|
def gradients_q_variational(self, dL_dKmm, dL_dpsi0, dL_dpsi1, dL_dpsi2, Z, variational_posterior):
|
|
|
|
|
mu = variational_posterior.mean
|
|
|
|
|
S = variational_posterior.variance
|
2014-02-20 14:04:16 +00:00
|
|
|
self._psi_computations(Z, mu, S)
|
2014-02-24 14:55:16 +00:00
|
|
|
l2 = self.lengthscale **2
|
2014-02-20 14:04:16 +00:00
|
|
|
#psi1
|
2014-02-24 14:55:16 +00:00
|
|
|
tmp = self._psi1[:, :, None] / l2 / self._psi1_denom
|
2014-02-20 14:04:16 +00:00
|
|
|
grad_mu = np.sum(dL_dpsi1[:, :, None] * tmp * self._psi1_dist, 1)
|
|
|
|
|
grad_S = np.sum(dL_dpsi1[:, :, None] * 0.5 * tmp * (self._psi1_dist_sq - 1), 1)
|
2014-02-21 08:03:44 +00:00
|
|
|
#psi2
|
2014-02-24 14:55:16 +00:00
|
|
|
tmp = self._psi2[:, :, :, None] / l2 / self._psi2_denom
|
2014-02-20 14:04:16 +00:00
|
|
|
grad_mu += -2.*(dL_dpsi2[:, :, :, None] * tmp * self._psi2_mudist).sum(1).sum(1)
|
|
|
|
|
grad_S += (dL_dpsi2[:, :, :, None] * tmp * (2.*self._psi2_mudist_sq - 1)).sum(1).sum(1)
|
2014-02-24 11:45:18 +00:00
|
|
|
|
|
|
|
|
return grad_mu, grad_S
|
2014-02-20 14:04:16 +00:00
|
|
|
|
2014-02-20 17:11:44 +00:00
|
|
|
#---------------------------------------#
|
2013-01-30 16:27:45 +00:00
|
|
|
# Precomputations #
|
|
|
|
|
#---------------------------------------#
|
|
|
|
|
|
2014-01-24 12:13:55 +00:00
|
|
|
def _dL_dlengthscales_via_K(self, dL_dK, X, X2):
|
|
|
|
|
"""
|
|
|
|
|
A helper function for update_gradients_* methods
|
|
|
|
|
|
|
|
|
|
Computes the derivative of the objective L wrt the lengthscales via
|
|
|
|
|
|
|
|
|
|
dL_dl = sum_{i,j}(dL_dK_{ij} dK_dl)
|
|
|
|
|
|
|
|
|
|
assumes self._K_computations has just been called.
|
|
|
|
|
|
|
|
|
|
This is only valid if self.ARD=True
|
|
|
|
|
"""
|
|
|
|
|
target = np.zeros(self.input_dim)
|
|
|
|
|
dvardLdK = self._K_dvar * dL_dK
|
|
|
|
|
var_len3 = self.variance / np.power(self.lengthscale, 3)
|
|
|
|
|
if X2 is None:
|
|
|
|
|
# save computation for the symmetrical case
|
|
|
|
|
dvardLdK = dvardLdK + dvardLdK.T
|
|
|
|
|
code = """
|
|
|
|
|
int q,i,j;
|
|
|
|
|
double tmp;
|
|
|
|
|
for(q=0; q<input_dim; q++){
|
|
|
|
|
tmp = 0;
|
|
|
|
|
for(i=0; i<num_data; i++){
|
|
|
|
|
for(j=0; j<i; j++){
|
|
|
|
|
tmp += (X(i,q)-X(j,q))*(X(i,q)-X(j,q))*dvardLdK(i,j);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
target(q) += var_len3(q)*tmp;
|
|
|
|
|
}
|
|
|
|
|
"""
|
|
|
|
|
num_data, num_inducing, input_dim = X.shape[0], X.shape[0], self.input_dim
|
2014-02-17 12:04:40 +00:00
|
|
|
X, dvardLdK, var_len3 = param_to_array(X, dvardLdK, var_len3)
|
2014-01-24 12:13:55 +00:00
|
|
|
weave.inline(code, arg_names=['num_data', 'num_inducing', 'input_dim', 'X', 'target', 'dvardLdK', 'var_len3'], type_converters=weave.converters.blitz, **self.weave_options)
|
|
|
|
|
else:
|
|
|
|
|
code = """
|
|
|
|
|
int q,i,j;
|
|
|
|
|
double tmp;
|
|
|
|
|
for(q=0; q<input_dim; q++){
|
|
|
|
|
tmp = 0;
|
|
|
|
|
for(i=0; i<num_data; i++){
|
|
|
|
|
for(j=0; j<num_inducing; j++){
|
|
|
|
|
tmp += (X(i,q)-X2(j,q))*(X(i,q)-X2(j,q))*dvardLdK(i,j);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
target(q) += var_len3(q)*tmp;
|
|
|
|
|
}
|
|
|
|
|
"""
|
|
|
|
|
num_data, num_inducing, input_dim = X.shape[0], X2.shape[0], self.input_dim
|
2014-02-17 12:04:40 +00:00
|
|
|
X, X2, dvardLdK, var_len3 = param_to_array(X, X2, dvardLdK, var_len3)
|
2014-01-24 12:13:55 +00:00
|
|
|
weave.inline(code, arg_names=['num_data', 'num_inducing', 'input_dim', 'X', 'X2', 'target', 'dvardLdK', 'var_len3'], type_converters=weave.converters.blitz, **self.weave_options)
|
2014-01-24 14:06:16 +00:00
|
|
|
return target
|
2014-01-24 12:13:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-05 15:21:57 +01:00
|
|
|
def _psi_computations(self, Z, mu, S):
|
|
|
|
|
# here are the "statistics" for psi1 and psi2
|
2013-09-03 10:05:42 +01:00
|
|
|
Z_changed = not fast_array_equal(Z, self._Z)
|
|
|
|
|
if Z_changed:
|
2013-07-18 15:39:58 +01:00
|
|
|
# Z has changed, compute Z specific stuff
|
|
|
|
|
self._psi2_Zhat = 0.5 * (Z[:, None, :] + Z[None, :, :]) # M,M,Q
|
|
|
|
|
self._psi2_Zdist = 0.5 * (Z[:, None, :] - Z[None, :, :]) # M,M,Q
|
|
|
|
|
self._psi2_Zdist_sq = np.square(self._psi2_Zdist / self.lengthscale) # M,M,Q
|
2012-11-30 15:49:20 +00:00
|
|
|
|
2013-09-03 10:05:42 +01:00
|
|
|
if Z_changed or not fast_array_equal(mu, self._mu) or not fast_array_equal(S, self._S):
|
2013-07-18 15:39:58 +01:00
|
|
|
# something's changed. recompute EVERYTHING
|
2014-02-24 14:55:16 +00:00
|
|
|
l2 = self.lengthscale **2
|
2013-07-18 15:39:58 +01:00
|
|
|
|
|
|
|
|
# psi1
|
2014-02-24 14:55:16 +00:00
|
|
|
self._psi1_denom = S[:, None, :] / l2 + 1.
|
2013-07-18 15:39:58 +01:00
|
|
|
self._psi1_dist = Z[None, :, :] - mu[:, None, :]
|
2014-02-24 14:55:16 +00:00
|
|
|
self._psi1_dist_sq = np.square(self._psi1_dist) / l2 / self._psi1_denom
|
2013-07-18 15:39:58 +01:00
|
|
|
self._psi1_exponent = -0.5 * np.sum(self._psi1_dist_sq + np.log(self._psi1_denom), -1)
|
|
|
|
|
self._psi1 = self.variance * np.exp(self._psi1_exponent)
|
|
|
|
|
|
|
|
|
|
# psi2
|
2014-02-24 14:55:16 +00:00
|
|
|
self._psi2_denom = 2.*S[:, None, None, :] / l2 + 1. # N,M,M,Q
|
2013-07-18 15:39:58 +01:00
|
|
|
self._psi2_mudist, self._psi2_mudist_sq, self._psi2_exponent, _ = self.weave_psi2(mu, self._psi2_Zhat)
|
|
|
|
|
# self._psi2_mudist = mu[:,None,None,:]-self._psi2_Zhat #N,M,M,Q
|
2014-02-24 14:55:16 +00:00
|
|
|
# self._psi2_mudist_sq = np.square(self._psi2_mudist)/(l2*self._psi2_denom)
|
2013-07-18 15:39:58 +01:00
|
|
|
# self._psi2_exponent = np.sum(-self._psi2_Zdist_sq -self._psi2_mudist_sq -0.5*np.log(self._psi2_denom),-1) #N,M,M,Q
|
|
|
|
|
self._psi2 = np.square(self.variance) * np.exp(self._psi2_exponent) # N,M,M,Q
|
|
|
|
|
|
|
|
|
|
# store matrices for caching
|
|
|
|
|
self._Z, self._mu, self._S = Z, mu, S
|
|
|
|
|
|
|
|
|
|
def weave_psi2(self, mu, Zhat):
|
|
|
|
|
N, input_dim = mu.shape
|
2013-06-05 15:29:45 +01:00
|
|
|
num_inducing = Zhat.shape[0]
|
2013-04-10 16:12:09 +01:00
|
|
|
|
2013-07-18 15:39:58 +01:00
|
|
|
mudist = np.empty((N, num_inducing, num_inducing, input_dim))
|
|
|
|
|
mudist_sq = np.empty((N, num_inducing, num_inducing, input_dim))
|
|
|
|
|
psi2_exponent = np.zeros((N, num_inducing, num_inducing))
|
|
|
|
|
psi2 = np.empty((N, num_inducing, num_inducing))
|
2013-04-10 16:12:09 +01:00
|
|
|
|
|
|
|
|
psi2_Zdist_sq = self._psi2_Zdist_sq
|
2013-06-05 15:21:57 +01:00
|
|
|
_psi2_denom = self._psi2_denom.squeeze().reshape(N, self.input_dim)
|
|
|
|
|
half_log_psi2_denom = 0.5 * np.log(self._psi2_denom).squeeze().reshape(N, self.input_dim)
|
2014-02-24 14:55:16 +00:00
|
|
|
variance_sq = np.float64(np.square(self.variance))
|
2013-04-10 15:50:31 +01:00
|
|
|
if self.ARD:
|
2014-02-24 14:55:16 +00:00
|
|
|
lengthscale2 = self.lengthscale **2
|
2013-04-10 15:50:31 +01:00
|
|
|
else:
|
2014-02-24 14:55:16 +00:00
|
|
|
lengthscale2 = np.ones(input_dim) * self.lengthscale2**2
|
2013-04-10 15:50:31 +01:00
|
|
|
code = """
|
|
|
|
|
double tmp;
|
2013-04-10 20:02:22 +01:00
|
|
|
|
2013-04-10 16:50:02 +01:00
|
|
|
#pragma omp parallel for private(tmp)
|
2013-04-10 15:50:31 +01:00
|
|
|
for (int n=0; n<N; n++){
|
2013-06-05 15:29:45 +01:00
|
|
|
for (int m=0; m<num_inducing; m++){
|
2013-04-10 15:50:31 +01:00
|
|
|
for (int mm=0; mm<(m+1); mm++){
|
2013-06-05 11:17:15 +01:00
|
|
|
for (int q=0; q<input_dim; q++){
|
2013-04-10 16:12:09 +01:00
|
|
|
//compute mudist
|
|
|
|
|
tmp = mu(n,q) - Zhat(m,mm,q);
|
|
|
|
|
mudist(n,m,mm,q) = tmp;
|
|
|
|
|
mudist(n,mm,m,q) = tmp;
|
|
|
|
|
|
|
|
|
|
//now mudist_sq
|
|
|
|
|
tmp = tmp*tmp/lengthscale2(q)/_psi2_denom(n,q);
|
2013-04-10 15:50:31 +01:00
|
|
|
mudist_sq(n,m,mm,q) = tmp;
|
|
|
|
|
mudist_sq(n,mm,m,q) = tmp;
|
2013-04-10 16:12:09 +01:00
|
|
|
|
|
|
|
|
//now psi2_exponent
|
2013-04-12 15:02:56 +01:00
|
|
|
tmp = -psi2_Zdist_sq(m,mm,q) - tmp - half_log_psi2_denom(n,q);
|
2013-04-10 15:50:31 +01:00
|
|
|
psi2_exponent(n,mm,m) += tmp;
|
|
|
|
|
if (m !=mm){
|
|
|
|
|
psi2_exponent(n,m,mm) += tmp;
|
|
|
|
|
}
|
2013-04-10 16:12:09 +01:00
|
|
|
//psi2 would be computed like this, but np is faster
|
2013-04-10 15:50:31 +01:00
|
|
|
//tmp = variance_sq*exp(psi2_exponent(n,m,mm));
|
|
|
|
|
//psi2(n,m,mm) = tmp;
|
|
|
|
|
//psi2(n,mm,m) = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-10 16:50:02 +01:00
|
|
|
|
2013-04-10 15:50:31 +01:00
|
|
|
"""
|
|
|
|
|
|
2013-04-10 16:50:02 +01:00
|
|
|
support_code = """
|
|
|
|
|
#include <omp.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
"""
|
2014-02-20 14:04:16 +00:00
|
|
|
mu = param_to_array(mu)
|
2013-04-10 20:02:22 +01:00
|
|
|
weave.inline(code, support_code=support_code, libraries=['gomp'],
|
2013-07-18 15:39:58 +01:00
|
|
|
arg_names=['N', 'num_inducing', 'input_dim', 'mu', 'Zhat', 'mudist_sq', 'mudist', 'lengthscale2', '_psi2_denom', 'psi2_Zdist_sq', 'psi2_exponent', 'half_log_psi2_denom', 'psi2', 'variance_sq'],
|
2013-06-05 15:21:57 +01:00
|
|
|
type_converters=weave.converters.blitz, **self.weave_options)
|
2013-04-10 20:02:22 +01:00
|
|
|
|
2013-06-05 15:21:57 +01:00
|
|
|
return mudist, mudist_sq, psi2_exponent, psi2
|
2014-02-24 14:47:43 +00:00
|
|
|
|
|
|
|
|
def input_sensitivity(self):
|
|
|
|
|
if self.ARD: return 1./self.lengthscale
|
2014-02-24 14:50:23 +00:00
|
|
|
else: return (1./self.lengthscale).repeat(self.input_dim)
|