{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Kernel Design\n", "\n", "It's easy to make new kernels in GPflow. To demonstrate, we'll have a look at the Brownian motion kernel, whose function is \n", "$$\n", "k(x, x') = \\sigma^2 \\text{min}(x, x')\n", "$$\n", "where $\\sigma^2$ is a variance parameter. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import gpflow\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "plt.style.use('ggplot')\n", "import tensorflow as tf\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To make this new kernel class, we inherit form the base class `gpflow.kernels.Kernel` and implement the three functions below. Note that depending on the kernel to be implemented other classes can be more adequate. For example, it is preferable to inherit from the base class `gpflow.kernels.Stationary` if the kernel to be implemented is stationnary.\n", "\n", "#### `__init__`\n", "The constructor takes no argument in this simple case (though it could, if that was convenient). It *must* call the constructor of the super class with appropriate arguments. In this case, the input_dim is always 1 (Brownian motion is only defined in 1D) and we'll assume the active_dims are [0], for simplicity.\n", "\n", "We've added a parameter to the kernel using the Param class. Using this class lets the parameter be used in computing the kernel function, and it will automatically be recognised for optimization (or MCMC). Here, the variance parameter is initialized at 1, and constrained to be positive. \n", "\n", "#### `K`\n", "This is where you implement the kernel function itself. This takes two arguments, X and X2. By convention, we make the second argument optional (defaults to None). \n", "\n", "Inside K, all the computation must be done with tensorflow -- here we've used `tf.minimum`. When GPflow executes the `K` function, X and X2 will be tensorflow tensors, and the `params_as_tensors` decorator turns the parameters such as self.variance into tensorflow tensors as well.\n", "\n", "#### `Kdiag`\n", "This convenience function allows GPflow to save memory at predict time. It's simply the diagonal of the `K` function, in the case where X2 is None. It must return a 1D vector, so we use tensorflow's reshape command.\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | class | \n", "prior | \n", "transform | \n", "trainable | \n", "shape | \n", "fixed_shape | \n", "value | \n", "
---|---|---|---|---|---|---|---|
Brownian/variance | \n", "Parameter | \n", "None | \n", "+ve | \n", "True | \n", "() | \n", "True | \n", "1.0 | \n", "