Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add posterior_predictive to GP API and predict
  • Loading branch information
sgreenbury committed Jul 21, 2025
commit 5b5b091e932ca16a4bc0843e5fa06a13a8a918ce
26 changes: 21 additions & 5 deletions autoemulate/experimental/emulators/gaussian_process/exact.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# import logging

import gpytorch
import torch
from gpytorch import ExactMarginalLogLikelihood
Expand Down Expand Up @@ -59,7 +57,8 @@ def __init__( # noqa: PLR0913 allow too many arguments since all currently requ
y: TensorLike,
likelihood_cls: type[MultitaskGaussianLikelihood] = MultitaskGaussianLikelihood,
mean_module_fn: MeanModuleFn = constant_mean,
covar_module_fn: CovarModuleFn = rbf,
covar_module_fn: CovarModuleFn = rbf_plus_constant,
posterior_predictive: bool = False,
epochs: int = 50,
activation: type[nn.Module] = nn.ReLU,
lr: float = 2e-1,
Expand All @@ -84,6 +83,10 @@ def __init__( # noqa: PLR0913 allow too many arguments since all currently requ
Function to create the mean module.
covar_module_fn: CovarModuleFn, default=rbf
Function to create the covariance module.
posterior_predictive: bool, default=False
If True, the model will return the posterior predictive distribution that
by default includes observation noise (both global and task-specific). If
False, it will return the posterior distribution over the modelled function.
epochs: int, default=50
Number of training epochs.
activation: type[nn.Module], default=nn.ReLU
Expand Down Expand Up @@ -135,6 +138,7 @@ def __init__( # noqa: PLR0913 allow too many arguments since all currently requ
self.optimizer = self.optimizer_cls(self.parameters(), lr=self.lr) # type: ignore[call-arg] since all optimizers include lr
self.scheduler_setup(kwargs)
self.early_stopping = early_stopping
self.posterior_predictive = posterior_predictive
self.to(self.device)

@staticmethod
Expand Down Expand Up @@ -199,8 +203,14 @@ def _fit(self, x: TensorLike, y: TensorLike):
def _predict(self, x: TensorLike, with_grad: bool) -> GaussianProcessLike:
with torch.set_grad_enabled(with_grad):
self.eval()
self.likelihood.eval()
x = x.to(self.device)
return self(x)
output = self(x)
if not self.posterior_predictive:
return output
output = self.likelihood(output)
assert isinstance(output, GaussianProcessLike)
return output

@staticmethod
def get_tune_config():
Expand Down Expand Up @@ -249,7 +259,8 @@ def __init__( # noqa: PLR0913 allow too many arguments since all currently requ
y: TensorLike,
likelihood_cls: type[MultitaskGaussianLikelihood] = MultitaskGaussianLikelihood,
mean_module_fn: MeanModuleFn = constant_mean,
covar_module_fn: CovarModuleFn = rbf,
covar_module_fn: CovarModuleFn = rbf_plus_constant,
posterior_predictive: bool = False,
epochs: int = 50,
activation: type[nn.Module] = nn.ReLU,
lr: float = 2e-1,
Expand All @@ -275,6 +286,10 @@ def __init__( # noqa: PLR0913 allow too many arguments since all currently requ
Function to create the mean module.
covar_module_fn: CovarModuleFn, default=rbf
Function to create the covariance module.
posterior_predictive: bool, default=False
If True, the model will return the posterior predictive distribution that
by default includes observation noise (both global and task-specific). If
False, it will return the posterior distribution over the modelled function.
epochs: int, default=50
Number of training epochs.
activation: type[nn.Module], default=nn.ReLU
Expand Down Expand Up @@ -335,6 +350,7 @@ def __init__( # noqa: PLR0913 allow too many arguments since all currently requ
self.optimizer = self.optimizer_cls(self.parameters(), lr=self.lr) # type: ignore[call-arg] since all optimizers include lr
self.scheduler_setup(kwargs)
self.early_stopping = early_stopping
self.posterior_predictive = posterior_predictive
self.to(self.device)

def forward(self, x):
Expand Down