{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Non-Gaussian Likelihoods\n", "\n", "## Introduction\n", "\n", "This example is the simplest form of using an RBF kernel in an `ApproximateGP` module for classification. This basic model is usable when there is not much training data and no advanced techniques are required.\n", "\n", "In this example, we’re modeling a unit wave with period 1/2 centered with positive values @ x=0. We are going to classify the points as either +1 or -1.\n", "\n", "Variational inference uses the assumption that the posterior distribution factors multiplicatively over the input variables. This makes approximating the distribution via the KL divergence possible to obtain a fast approximation to the posterior. For a good explanation of variational techniques, sections 4-6 of the following may be useful: https://www.cs.princeton.edu/courses/archive/fall11/cos597C/lectures/variational-inference-i.pdf" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import math\n", "import torch\n", "import gpytorch\n", "from matplotlib import pyplot as plt\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set up training data\n", "\n", "In the next cell, we set up the training data for this example. We'll be using 10 regularly spaced points on [0,1] which we evaluate the function on and add Gaussian noise to get the training labels. Labels are unit wave with period 1/2 centered with positive values @ x=0." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "train_x = torch.linspace(0, 1, 10)\n", "train_y = torch.sign(torch.cos(train_x * (4 * math.pi))).add(1).div(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setting up the classification model\n", "\n", "The next cell demonstrates the simplest way to define a classification Gaussian process model in GPyTorch. If you have already done the [GP regression tutorial](../01_Exact_GPs/Simple_GP_Regression.ipynb), you have already seen how GPyTorch model construction differs from other GP packages. In particular, the GP model expects a user to write out a `forward` method in a way analogous to PyTorch models. This gives the user the most possible flexibility.\n", "\n", "Since exact inference is intractable for GP classification, GPyTorch approximates the classification posterior using **variational inference.** We believe that variational inference is ideal for a number of reasons. Firstly, variational inference commonly relies on gradient descent techniques, which take full advantage of PyTorch's autograd. This reduces the amount of code needed to develop complex variational models. Additionally, variational inference can be performed with stochastic gradient decent, which can be extremely scalable for large datasets.\n", "\n", "If you are unfamiliar with variational inference, we recommend the following resources:\n", "- [Variational Inference: A Review for Statisticians](https://arxiv.org/abs/1601.00670) by David M. Blei, Alp Kucukelbir, Jon D. McAuliffe.\n", "- [Scalable Variational Gaussian Process Classification](https://arxiv.org/abs/1411.2005) by James Hensman, Alex Matthews, Zoubin Ghahramani.\n", " \n", "In this example, we're using an `UnwhitenedVariationalStrategy` because we are using the training data as inducing points. In general, you'll probably want to use the standard `VariationalStrategy` class for improved optimization." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from gpytorch.models import ApproximateGP\n", "from gpytorch.variational import CholeskyVariationalDistribution\n", "from gpytorch.variational import UnwhitenedVariationalStrategy\n", "\n", "\n", "class GPClassificationModel(ApproximateGP):\n", " def __init__(self, train_x):\n", " variational_distribution = CholeskyVariationalDistribution(train_x.size(0))\n", " variational_strategy = UnwhitenedVariationalStrategy(\n", " self, train_x, variational_distribution, learn_inducing_locations=False\n", " )\n", " super(GPClassificationModel, self).__init__(variational_strategy)\n", " self.mean_module = gpytorch.means.ConstantMean()\n", " self.covar_module = gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKernel())\n", "\n", " def forward(self, x):\n", " mean_x = self.mean_module(x)\n", " covar_x = self.covar_module(x)\n", " latent_pred = gpytorch.distributions.MultivariateNormal(mean_x, covar_x)\n", " return latent_pred\n", "\n", "\n", "# Initialize model and likelihood\n", "model = GPClassificationModel(train_x)\n", "likelihood = gpytorch.likelihoods.BernoulliLikelihood()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Model modes\n", "\n", "Like most PyTorch modules, the `ApproximateGP` has a `.train()` and `.eval()` mode.\n", "- `.train()` mode is for optimizing variational parameters model hyperameters.\n", "- `.eval()` mode is for computing predictions through the model posterior." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Learn the variational parameters (and other hyperparameters)\n", "\n", "In the next cell, we optimize the variational parameters of our Gaussian process.\n", "In addition, this optimization loop also performs Type-II MLE to train the hyperparameters of the Gaussian process." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Iter 1/50 - Loss: 0.908\n", "Iter 2/50 - Loss: 4.272\n", "Iter 3/50 - Loss: 8.886\n", "Iter 4/50 - Loss: 3.560\n", "Iter 5/50 - Loss: 5.968\n", "Iter 6/50 - Loss: 6.614\n", "Iter 7/50 - Loss: 6.212\n", "Iter 8/50 - Loss: 4.975\n", "Iter 9/50 - Loss: 3.976\n", "Iter 10/50 - Loss: 3.596\n", "Iter 11/50 - Loss: 3.327\n", "Iter 12/50 - Loss: 2.791\n", "Iter 13/50 - Loss: 2.325\n", "Iter 14/50 - Loss: 2.140\n", "Iter 15/50 - Loss: 1.879\n", "Iter 16/50 - Loss: 1.659\n", "Iter 17/50 - Loss: 1.533\n", "Iter 18/50 - Loss: 1.510\n", "Iter 19/50 - Loss: 1.514\n", "Iter 20/50 - Loss: 1.503\n", "Iter 21/50 - Loss: 1.499\n", "Iter 22/50 - Loss: 1.500\n", "Iter 23/50 - Loss: 1.499\n", "Iter 24/50 - Loss: 1.492\n", "Iter 25/50 - Loss: 1.477\n", "Iter 26/50 - Loss: 1.456\n", "Iter 27/50 - Loss: 1.429\n", "Iter 28/50 - Loss: 1.397\n", "Iter 29/50 - Loss: 1.363\n", "Iter 30/50 - Loss: 1.327\n", "Iter 31/50 - Loss: 1.290\n", "Iter 32/50 - Loss: 1.255\n", "Iter 33/50 - Loss: 1.222\n", "Iter 34/50 - Loss: 1.194\n", "Iter 35/50 - Loss: 1.170\n", "Iter 36/50 - Loss: 1.150\n", "Iter 37/50 - Loss: 1.133\n", "Iter 38/50 - Loss: 1.117\n", "Iter 39/50 - Loss: 1.099\n", "Iter 40/50 - Loss: 1.079\n", "Iter 41/50 - Loss: 1.056\n", "Iter 42/50 - Loss: 1.033\n", "Iter 43/50 - Loss: 1.011\n", "Iter 44/50 - Loss: 0.991\n", "Iter 45/50 - Loss: 0.974\n", "Iter 46/50 - Loss: 0.958\n", "Iter 47/50 - Loss: 0.944\n", "Iter 48/50 - Loss: 0.931\n", "Iter 49/50 - Loss: 0.918\n", "Iter 50/50 - Loss: 0.906\n" ] } ], "source": [ "# this is for running the notebook in our testing framework\n", "import os\n", "smoke_test = ('CI' in os.environ)\n", "training_iterations = 2 if smoke_test else 50\n", "\n", "\n", "# Find optimal model hyperparameters\n", "model.train()\n", "likelihood.train()\n", "\n", "# Use the adam optimizer\n", "optimizer = torch.optim.Adam(model.parameters(), lr=0.1)\n", "\n", "# \"Loss\" for GPs - the marginal log likelihood\n", "# num_data refers to the number of training datapoints\n", "mll = gpytorch.mlls.VariationalELBO(likelihood, model, train_y.numel())\n", "\n", "for i in range(training_iterations):\n", " # Zero backpropped gradients from previous iteration\n", " optimizer.zero_grad()\n", " # Get predictive output\n", " output = model(train_x)\n", " # Calc loss and backprop gradients\n", " loss = -mll(output, train_y)\n", " loss.backward()\n", " print('Iter %d/%d - Loss: %.3f' % (i + 1, training_iterations, loss.item()))\n", " optimizer.step()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Make predictions with the model\n", "\n", "In the next cell, we make predictions with the model. To do this, we simply put the model and likelihood in eval mode, and call both modules on the test data.\n", "\n", "In `.eval()` mode, when we call `model()` - we get GP's latent posterior predictions. These will be MultivariateNormal distributions. But since we are performing binary classification, we want to transform these outputs to classification probabilities using our likelihood.\n", "\n", "When we call `likelihood(model())`, we get a `torch.distributions.Bernoulli` distribution, which represents our posterior probability that the data points belong to the positive class.\n", "\n", "```python\n", "f_preds = model(test_x)\n", "y_preds = likelihood(model(test_x))\n", "\n", "f_mean = f_preds.mean\n", "f_samples = f_preds.sample(sample_shape=torch.Size((1000,))\n", "```" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQUAAADBCAYAAADGmKWHAAAAOXRFWHRTb2Z0d2FyZQ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GUYgApAUVU1bPR6h+MLAVgGEHTyfdZiKfsCQhlHCmaLn2RZFGUWx2EACe1DfNaRyO4z7SvtA3MA2TNETjKMUf+D0xKWk6thC8UHLT6n+7AiCwhlTAoHkM3CgPjLmm2vNsM4VFUNq6oa1f7oHNsDawr1lYLsaWOnmcX4ArJntEIAuqodVB6z9/kCgLOKovwRziatQmz9rciYFNzudQBDTgdhRFGUkEuuRbmgxfm604Fspp1+n0Q2sTo9IqwIGZOCvvgJEBc/mW2vNtM4tOO7STg/rDSLcVnbAz8HZ/fCgHmMl+F8/wHm8YVUVZ1TVfW/AUSrHpU1tv5WZEwKWxY/SbYoyjA+7fvXAfwOzu9BDGPUjoNV7Tl7nApOYxZjGECXtn1StvgAXNDe7y4A/+tgfMiLqawLCLl4iYgEMo4UiMhBTApEJGBSICIBkwIRCZgUiEjApEBEAiYFIhL8P4IEZQA8UOJ3AAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "# Go into eval mode\n", "model.eval()\n", "likelihood.eval()\n", "\n", "with torch.no_grad():\n", " # Test x are regularly spaced by 0.01 0,1 inclusive\n", " test_x = torch.linspace(0, 1, 101)\n", " # Get classification predictions\n", " observed_pred = likelihood(model(test_x))\n", "\n", " # Initialize fig and axes for plot\n", " f, ax = plt.subplots(1, 1, figsize=(4, 3))\n", " ax.plot(train_x.numpy(), train_y.numpy(), 'k*')\n", " # Get the predicted labels (probabilites of belonging to the positive class)\n", " # Transform these probabilities to be 0/1 labels\n", " pred_labels = observed_pred.mean.ge(0.5).float()\n", " ax.plot(test_x.numpy(), pred_labels.numpy(), 'b')\n", " ax.set_ylim([-1, 2])\n", " ax.legend(['Observed Data', 'Mean'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Notes on other Non-Gaussian Likeihoods\n", "\n", "The Bernoulli likelihood is special in that we can compute the analytic (approximate) posterior predictive in closed form. That is: $q(\\mathbf y) = E_{q(\\mathbf f)}[ p(y \\mid \\mathbf f) ]$ is a Bernoulli distribution when $q(\\mathbf f)$ is a multivariate Gaussian.\n", "\n", "Most other non-Gaussian likelihoods do not admit an analytic (approximate) posterior predictive. To that end, calling `likelihood(model)` will generally return Monte Carlo samples from the posterior predictive." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Type of output: Bernoulli\n", "Shape of output: torch.Size([101])\n" ] } ], "source": [ "# Analytic marginal\n", "likelihood = gpytorch.likelihoods.BernoulliLikelihood()\n", "observed_pred = likelihood(model(test_x))\n", "print(\n", " f\"Type of output: {observed_pred.__class__.__name__}\\n\"\n", " f\"Shape of output: {observed_pred.batch_shape + observed_pred.event_shape}\"\n", ")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Type of output: Beta\n", "Shape of output: torch.Size([15, 101])\n" ] } ], "source": [ "# Monte Carlo marginal\n", "likelihood = gpytorch.likelihoods.BetaLikelihood()\n", "with gpytorch.settings.num_likelihood_samples(15):\n", " observed_pred = likelihood(model(test_x))\n", "print(\n", " f\"Type of output: {observed_pred.__class__.__name__}\\n\"\n", " f\"Shape of output: {observed_pred.batch_shape + observed_pred.event_shape}\"\n", ")\n", "# There are 15 MC samples for each test datapoint" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See [the Likelihood documentation](http://gpytorch.readthedocs.io/en/stable/likelihoods.html#likelihood) for more details." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.0" } }, "nbformat": 4, "nbformat_minor": 1 }