|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Adding emulators\n", |
| 9 | + "\n", |
| 10 | + "In addition to providing a library of core emulators, AutoEmulate is designed to be easily extensible. This tutorial walks you through the steps of adding new emulators to the library. We cover two scenarios: adding new Gaussian Process kernels and adding entirely new models." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "id": "1", |
| 16 | + "metadata": {}, |
| 17 | + "source": [ |
| 18 | + "## 1. Adding Gaussian Process kernels\n", |
| 19 | + "\n", |
| 20 | + "Gaussian Processes (GPs) are primarily defined by their kernel functions, which determine the covariance structure of the data. AutoEmulate includes several built-in GP kernels:\n", |
| 21 | + "- Radial Basis Function (RBF)\n", |
| 22 | + "- Matern 3/2\n", |
| 23 | + "- Matern 5/2\n", |
| 24 | + "- Rational Quadratic (RQ)\n", |
| 25 | + "- Linear\n", |
| 26 | + "\n", |
| 27 | + "You can easily create new kernels by composing any two or more of these existing kernels. For example, you might want to create a kernel that combines the RBF and Linear kernels to capture both smooth variations and linear trends in your data.\n", |
| 28 | + "\n", |
| 29 | + "In AutoEmulate, each kernel is defined by an initialisation function that takes as inputs the number of data input features and the number of output features. Below we define a custom kernel function following this pattern." |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "execution_count": null, |
| 35 | + "id": "2", |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "from autoemulate.emulators.gaussian_process.kernel import rbf_kernel, linear_kernel\n", |
| 40 | + "\n", |
| 41 | + "def rbs_plus_linear_kernel(n_features, n_outputs):\n", |
| 42 | + " \"\"\"\n", |
| 43 | + " Example of a custom kernel function that combines RBF and linear kernels.\n", |
| 44 | + " \"\"\"\n", |
| 45 | + " return rbf_kernel(n_features, n_outputs) + linear_kernel(n_features, n_outputs)" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "markdown", |
| 50 | + "id": "3", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "Once this function has been defined, you can create a new GP emulator class using the `create_gp_subclass` function." |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": null, |
| 59 | + "id": "4", |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "from autoemulate.emulators.gaussian_process.exact import GaussianProcess, create_gp_subclass\n", |
| 64 | + "\n", |
| 65 | + "GaussianProcessRBFandLinear = create_gp_subclass(\n", |
| 66 | + " \"GaussianProcessRBFandLinear\", \n", |
| 67 | + " GaussianProcess, \n", |
| 68 | + " # the custom kernel function goes here\n", |
| 69 | + " covar_module_fn=rbs_plus_linear_kernel,\n", |
| 70 | + ")" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "markdown", |
| 75 | + "id": "5", |
| 76 | + "metadata": {}, |
| 77 | + "source": [ |
| 78 | + "Now we can tell AutoEmulate to use the new GP class by passing it to the `models` argument when initialising an `AutoEmulate` object." |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": null, |
| 84 | + "id": "6", |
| 85 | + "metadata": {}, |
| 86 | + "outputs": [], |
| 87 | + "source": [ |
| 88 | + "from autoemulate import AutoEmulate\n", |
| 89 | + "import torch\n", |
| 90 | + "\n", |
| 91 | + "# create some example data\n", |
| 92 | + "x = torch.linspace(0, 1, 100).unsqueeze(-1)\n", |
| 93 | + "y = torch.sin(2 * 3.14 * x) + 0.1 * torch.randn_like(x)\n", |
| 94 | + "\n", |
| 95 | + "ae = AutoEmulate(x, y, models=[GaussianProcessRBFandLinear])" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": null, |
| 101 | + "id": "7", |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [], |
| 104 | + "source": [ |
| 105 | + "ae.summarise()" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "markdown", |
| 110 | + "id": "8", |
| 111 | + "metadata": {}, |
| 112 | + "source": [ |
| 113 | + "## 2. Adding new models\n", |
| 114 | + "\n", |
| 115 | + "It is also possible to add entirely new models to AutoEmulate. AutoEmulate has a base `Emulator` class that handles most of the general functionality required for training and prediction. To implement a new emulator, one must simply subclass `Emulator` and implement the abstract methods (`_fit`, `_predict` and `is_multioutput`), `get_tune_params` to enable model tuning, as well any model specific functionality and initialisations.\n", |
| 116 | + "\n", |
| 117 | + "Since AutoEmulate supports a variety of models, there are additional `Emulator` subclasses that handle specific functionality for each model type:\n", |
| 118 | + "- `PytorchBackend` for PyTorch models\n", |
| 119 | + "- `SklearnBackend` for scikit-learn models\n", |
| 120 | + "- `GaussianProcess` for exact Gaussian Process implementations\n", |
| 121 | + "- `Ensemble` for ensemble models\n", |
| 122 | + "\n", |
| 123 | + "Subclassing one of these directly has slightly different requirements. For example, when subclassing `PytorchBackend` or `GaussianProcess`, one must implement the `forward` method to define the model's forward pass.\n", |
| 124 | + "\n", |
| 125 | + "There are also some static methods that should be implemented to provide metadata about the model, such as `is_multioutput` and `get_tune_params`.\n", |
| 126 | + "\n", |
| 127 | + "Below demonstrates adding a simple feedforward neural network (FNN) using PyTorch. The new class `SimpleFNN` subclasses `PytorchBackend`, which already handles fitting and prediction." |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "code", |
| 132 | + "execution_count": null, |
| 133 | + "id": "9", |
| 134 | + "metadata": {}, |
| 135 | + "outputs": [], |
| 136 | + "source": [ |
| 137 | + "from autoemulate.core.device import TorchDeviceMixin\n", |
| 138 | + "from autoemulate.emulators.base import PyTorchBackend\n", |
| 139 | + "import torch.nn as nn\n", |
| 140 | + "\n", |
| 141 | + "class SimpleFNN(PyTorchBackend):\n", |
| 142 | + " def __init__(\n", |
| 143 | + " self, \n", |
| 144 | + " x, \n", |
| 145 | + " y,\n", |
| 146 | + " hidden_dim=64,\n", |
| 147 | + " device = None,\n", |
| 148 | + " ):\n", |
| 149 | + " TorchDeviceMixin.__init__(self, device=device)\n", |
| 150 | + " nn.Module.__init__(self)\n", |
| 151 | + " \n", |
| 152 | + " input_dim = x.shape[1]\n", |
| 153 | + " output_dim = y.shape[1] if len(y.shape) > 1 else 1\n", |
| 154 | + " layers = []\n", |
| 155 | + " layers.append(nn.Linear(input_dim, hidden_dim, device=self.device))\n", |
| 156 | + " layers.append(nn.ReLU())\n", |
| 157 | + " layers.append(nn.Linear(hidden_dim, output_dim, device=self.device))\n", |
| 158 | + " self.model = nn.Sequential(*layers)\n", |
| 159 | + " self.optimizer = self.optimizer_cls(self.model.parameters(), lr=self.lr) # type: ignore[call-arg] since all optimizers include lr\n", |
| 160 | + " self.scheduler = None\n", |
| 161 | + " self.to(self.device)\n", |
| 162 | + " \n", |
| 163 | + " def forward(self, x):\n", |
| 164 | + " return self.model(x)\n", |
| 165 | + " \n", |
| 166 | + " @staticmethod\n", |
| 167 | + " def is_multioutput():\n", |
| 168 | + " return True\n", |
| 169 | + " \n", |
| 170 | + " @staticmethod\n", |
| 171 | + " def get_tune_params():\n", |
| 172 | + " return {\n", |
| 173 | + " \"hidden_dim\": [32, 64, 128]\n", |
| 174 | + " }" |
| 175 | + ] |
| 176 | + }, |
| 177 | + { |
| 178 | + "cell_type": "code", |
| 179 | + "execution_count": null, |
| 180 | + "id": "10", |
| 181 | + "metadata": {}, |
| 182 | + "outputs": [], |
| 183 | + "source": [ |
| 184 | + "ae = AutoEmulate(x, y, models=[SimpleFNN])" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "code", |
| 189 | + "execution_count": null, |
| 190 | + "id": "11", |
| 191 | + "metadata": {}, |
| 192 | + "outputs": [], |
| 193 | + "source": [ |
| 194 | + "ae.summarise()" |
| 195 | + ] |
| 196 | + } |
| 197 | + ], |
| 198 | + "metadata": { |
| 199 | + "kernelspec": { |
| 200 | + "display_name": ".venv", |
| 201 | + "language": "python", |
| 202 | + "name": "python3" |
| 203 | + }, |
| 204 | + "language_info": { |
| 205 | + "codemirror_mode": { |
| 206 | + "name": "ipython", |
| 207 | + "version": 3 |
| 208 | + }, |
| 209 | + "file_extension": ".py", |
| 210 | + "mimetype": "text/x-python", |
| 211 | + "name": "python", |
| 212 | + "nbconvert_exporter": "python", |
| 213 | + "pygments_lexer": "ipython3", |
| 214 | + "version": "3.12.11" |
| 215 | + } |
| 216 | + }, |
| 217 | + "nbformat": 4, |
| 218 | + "nbformat_minor": 5 |
| 219 | +} |
0 commit comments