mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-06-08 15:05:15 +02:00
chancges to where gradients are computed in laplace
This commit is contained in:
parent
4d00b9db03
commit
ccf5167b75
2 changed files with 11 additions and 22 deletions
|
|
@ -27,7 +27,7 @@ class GP(Model):
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def __init__(self, X, Y, kernel, likelihood, inference_method=None, name='gp', **Y_metadata):
|
def __init__(self, X, Y, kernel, likelihood, inference_method=None, name='gp', Y_metadata=None):
|
||||||
super(GP, self).__init__(name)
|
super(GP, self).__init__(name)
|
||||||
|
|
||||||
assert X.ndim == 2
|
assert X.ndim == 2
|
||||||
|
|
@ -42,10 +42,7 @@ class GP(Model):
|
||||||
assert Y.shape[0] == self.num_data
|
assert Y.shape[0] == self.num_data
|
||||||
_, self.output_dim = self.Y.shape
|
_, self.output_dim = self.Y.shape
|
||||||
|
|
||||||
if Y_metadata is not None:
|
self.Y_metadata = Y_metadata or {}
|
||||||
self.Y_metadata = Y_metadata
|
|
||||||
else:
|
|
||||||
self.Y_metadata = None
|
|
||||||
|
|
||||||
assert isinstance(kernel, kern.Kern)
|
assert isinstance(kernel, kern.Kern)
|
||||||
#assert self.input_dim == kernel.input_dim
|
#assert self.input_dim == kernel.input_dim
|
||||||
|
|
@ -67,8 +64,8 @@ class GP(Model):
|
||||||
self.add_parameter(self.likelihood)
|
self.add_parameter(self.likelihood)
|
||||||
|
|
||||||
def parameters_changed(self):
|
def parameters_changed(self):
|
||||||
self.posterior, self._log_marginal_likelihood, self.grad_dict = self.inference_method.inference(self.kern, self.X, self.likelihood, self.Y, **self.Y_metadata)
|
self.posterior, self._log_marginal_likelihood, self.grad_dict = self.inference_method.inference(self.kern, self.X, self.likelihood, self.Y, self.Y_metadata)
|
||||||
self.likelihood.update_gradients(np.diag(self.grad_dict['dL_dK']), **self.Y_metadata)
|
self.likelihood.update_gradients(self.grad_dict['dL_dthetaL'])
|
||||||
self.kern.update_gradients_full(self.grad_dict['dL_dK'], self.X)
|
self.kern.update_gradients_full(self.grad_dict['dL_dK'], self.X)
|
||||||
|
|
||||||
def log_likelihood(self):
|
def log_likelihood(self):
|
||||||
|
|
@ -99,7 +96,7 @@ class GP(Model):
|
||||||
var = var.reshape(-1, 1)
|
var = var.reshape(-1, 1)
|
||||||
return mu, var
|
return mu, var
|
||||||
|
|
||||||
def predict(self, Xnew, full_cov=False, **likelihood_args):
|
def predict(self, Xnew, full_cov=False, Y_metadata=None):
|
||||||
"""
|
"""
|
||||||
Predict the function(s) at the new point(s) Xnew.
|
Predict the function(s) at the new point(s) Xnew.
|
||||||
|
|
||||||
|
|
@ -123,7 +120,7 @@ class GP(Model):
|
||||||
mu, var = self._raw_predict(Xnew, full_cov=full_cov)
|
mu, var = self._raw_predict(Xnew, full_cov=full_cov)
|
||||||
|
|
||||||
# now push through likelihood
|
# now push through likelihood
|
||||||
mean, var, _025pm, _975pm = self.likelihood.predictive_values(mu, var, full_cov, **likelihood_args)
|
mean, var, _025pm, _975pm = self.likelihood.predictive_values(mu, var, full_cov, Y_metadata)
|
||||||
return mean, var, _025pm, _975pm
|
return mean, var, _025pm, _975pm
|
||||||
|
|
||||||
def posterior_samples_f(self,X,size=10, full_cov=True):
|
def posterior_samples_f(self,X,size=10, full_cov=True):
|
||||||
|
|
@ -147,7 +144,7 @@ class GP(Model):
|
||||||
|
|
||||||
return Ysim
|
return Ysim
|
||||||
|
|
||||||
def posterior_samples(self,X,size=10, full_cov=True,noise_model=None):
|
def posterior_samples(self,X,size=10, full_cov=True, Y_metadata=None):
|
||||||
"""
|
"""
|
||||||
Samples the posterior GP at the points X.
|
Samples the posterior GP at the points X.
|
||||||
|
|
||||||
|
|
@ -162,15 +159,7 @@ class GP(Model):
|
||||||
:returns: Ysim: set of simulations, a Numpy array (N x samples).
|
:returns: Ysim: set of simulations, a Numpy array (N x samples).
|
||||||
"""
|
"""
|
||||||
Ysim = self.posterior_samples_f(X, size, full_cov=full_cov)
|
Ysim = self.posterior_samples_f(X, size, full_cov=full_cov)
|
||||||
if isinstance(self.likelihood, Gaussian):
|
Ysim = self.likelihood.noise_model.samples(Ysim, Y_metadata)
|
||||||
noise_std = np.sqrt(self.likelihood._get_params())
|
|
||||||
Ysim += np.random.normal(0,noise_std,Ysim.shape)
|
|
||||||
elif isinstance(self.likelihood, Gaussian_Mixed_Noise):
|
|
||||||
assert noise_model is not None, "A noise model must be specified."
|
|
||||||
noise_std = np.sqrt(self.likelihood._get_params()[noise_model])
|
|
||||||
Ysim += np.random.normal(0,noise_std,Ysim.shape)
|
|
||||||
else:
|
|
||||||
Ysim = self.likelihood.noise_model.samples(Ysim)
|
|
||||||
|
|
||||||
return Ysim
|
return Ysim
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -37,13 +37,13 @@ class StudentT(Likelihood):
|
||||||
def parameters_changed(self):
|
def parameters_changed(self):
|
||||||
self.variance = (self.v / float(self.v - 2)) * self.sigma2
|
self.variance = (self.v / float(self.v - 2)) * self.sigma2
|
||||||
|
|
||||||
def update_gradients(self, partial):
|
def update_gradients(self, grads):
|
||||||
"""
|
"""
|
||||||
Pull out the gradients, be careful as the order must match the order
|
Pull out the gradients, be careful as the order must match the order
|
||||||
in which the parameters are added
|
in which the parameters are added
|
||||||
"""
|
"""
|
||||||
self.sigma2.gradient = partial[0]
|
self.sigma2.gradient = grads[0]
|
||||||
self.v.gradient = partial[1]
|
self.v.gradient = grads[1]
|
||||||
|
|
||||||
def pdf_link(self, link_f, y, extra_data=None):
|
def pdf_link(self, link_f, y, extra_data=None):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue