Gaussian Process
Gaussian Process
Hao Yan
June 2, 2024
Contents
1 Gaussian Process 2
1.1 Gaussian Process Prior: . . . . . . . . . . . . . . . . . . . . . 2
1.2 Likelihood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Posterior Distribution: . . . . . . . . . . . . . . . . . . . . . . 2
2 Tutorial on GPytorch.models.exact_gp 2
2.1 Pytorch Implementation - ExactGP Class . . . . . . . . . . . 2
2.2 Pytorch Implementation - ExactGP . . . . . . . . . . . . . . . 3
2.3 Pytorch Implementation - Predictionstrategy . . . . . . . . . . 3
3 Examlpe in Gpytorch 4
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.2 Data Preparation - Create Synthetic Data . . . . . . . . . . . 4
3.3 Data Preparation - Visualize Synthetic Data . . . . . . . . . . 4
3.4 Model Definition - Define the GP Model . . . . . . . . . . . . 5
3.5 Model Training - Train the GP Model . . . . . . . . . . . . . 5
3.6 Model Training - Training Results . . . . . . . . . . . . . . . 6
3.7 Model Parameters . . . . . . . . . . . . . . . . . . . . . . . . 7
3.8 Likelihood Parameters . . . . . . . . . . . . . . . . . . . . . . 7
3.9 Predictions - Make Predictions with the GP Model . . . . . . 7
3.10 Predictions - Visualize Predictions . . . . . . . . . . . . . . . 8
3.11 Summary - Key Points . . . . . . . . . . . . . . . . . . . . . . 8
3.12 Summary - Further Reading . . . . . . . . . . . . . . . . . . . 9
Area
Keywords
1
1 Gaussian Process
1.1 Gaussian Process Prior:
f (x) ∼ GP m(x), k x, x′
where m(x) is the mean function and k (x, x′ ) is the covariance function.
1.2 Likelihood
Given training data X = {xi }ni=1 and corresponding targets y = {yi }ni=1 , we
assume:
y = f (X) + ϵ, ϵ ∼ N 0, σn2 I
2 Tutorial on GPytorch.models.exact_gp
2.1 Pytorch Implementation - ExactGP Class
• File: gpytorch/models/exact_gp.py
• __init__
def __init__(self, train_inputs, train_targets, likelihood):
...
self.train_inputs = tuple(tri.unsqueeze(-1) if tri.ndimension() == 1 else t
self.train_targets = train_targets
self.likelihood = likelihood
self.prediction_strategy = None
2
• forward: Defines the mean and covariance functions for the GP prior.
3
3 Examlpe in Gpytorch
3.1 Introduction
• Gaussian Process Regression (GPR) is a non-parametric method.
# Training data
train_x = torch.linspace(0, 1, 100)
train_y = torch.sin(train_x * (2 * torch.pi)) + torch.randn(train_x.size()) * 0.2
# Testing data
test_x = torch.linspace(0, 1, 51)
test_y = torch.sin(test_x * (2 * torch.pi))
4
3.4 Model Definition - Define the GP Model
• Subclass ‘gpytorch.models.ExactGP‘ to define our model.
class GPRegressionModel(gpytorch.models.ExactGP):
def __init__(self, train_x, train_y, likelihood):
super(GPRegressionModel, self).__init__(train_x, train_y, likelihood)
self.mean_module = gpytorch.means.ConstantMean()
self.covar_module = gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKe
5
model.train()
likelihood.train()
plt.plot(loss_values)
plt.title("Training Loss Over Iterations")
plt.xlabel("Iteration")
plt.ylabel("Loss")
plt.show()
6
3.7 Model Parameters
3.8 Likelihood Parameters
3.9 Predictions - Make Predictions with the GP Model
• Use the trained model to make predictions on test data.
model.eval()
likelihood.eval()
# Make predictions
with torch.no_grad(), gpytorch.settings.fast_pred_var():
observed_pred = likelihood(model(test_x))
7
3.10 Predictions - Visualize Predictions
• Plot the training data, true function, and predictions with uncertainty.
8
3.12 Summary - Further Reading
• GPyTorch Documentation