A minimal implementation of Gaussian process regression in PyTorch
In search of truth, simplicity is needed. There exist heavy-weighted libraries, but as you know, we need to go bare bone sometimes. Here is a minimal implementation of Gaussian process regression in PyTorch.
The implementation generally follows Algorithm 2.1 in Gaussian Process for Machine Learning (Rassmussen and Williams, 2006).
demo.ipynb
)from gp import GP
# generate data
X = torch.randn(100,1)
y = torch.sin(X * 2 * np.pi /4). + torch.randn(100, 1) * 0.1
grid = torch.linspace(-5, 5, 200)[:,None]
# run GP
gp = GP() # you may specify initial hyperparameters using keyword arguments
gp.fit(X, y)
mu, var = gp.forward(grid)
from gp import GP
# generate data
X = torch.randn(100,1)
f = torch.sin(X * 3 * np.pi / 4)
y = (f > 0.).int() * 2 - 1
grid = torch.linspace(-5, 5, 200)[:,None]
# run GP
gp = BinaryLaplaceGPC() # you may specify initial hyperparameters using keyword arguments
gp.fit(X, y)
mu, var, pi = gp.forward(grid)
$ pytest