{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "e7b5e13a-c059-441d-8d4f-fff080d52054", "metadata": {}, "source": [ "# Hyperparameters\n", "\n", "Consider the Booth test function (below). This function varies differently in different directions, and these directions are somewhat skewed with respect to the inputs. Our agent will automatically fit the right hyperparameters to account for this." ] }, { "cell_type": "code", "execution_count": null, "id": "22438de8", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib as mpl\n", "from matplotlib import pyplot as plt\n", "from blop.utils import functions\n", "\n", "x1 = x2 = np.linspace(-10, 10, 256)\n", "X1, X2 = np.meshgrid(x1, x2)\n", "\n", "F = functions.booth(X1, X2)\n", "\n", "plt.pcolormesh(x1, x2, F, norm=mpl.colors.LogNorm(), shading=\"auto\")\n", "plt.colorbar()\n", "plt.xlabel(\"x1\")\n", "plt.ylabel(\"x2\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "7a88c7bd", "metadata": {}, "source": [ "The optimization goes faster if our model understands how the function changes as we change the inputs in different ways. The way it picks up on this is by starting from a general model that could describe a lot of functions, and making it specific to this one by choosing the right hyperparameters. Our Bayesian agent is very good at this, and only needs a few samples to figure out what the function looks like:" ] }, { "cell_type": "code", "execution_count": null, "id": "7e9c949e", "metadata": {}, "outputs": [], "source": [ "def digestion(df):\n", " for index, entry in df.iterrows():\n", " df.loc[index, \"booth\"] = functions.booth(entry.x1, entry.x2)\n", "\n", " return df" ] }, { "cell_type": "code", "execution_count": null, "id": "071a829f-a390-40dc-9d5b-ae75702e119e", "metadata": { "tags": [] }, "outputs": [], "source": [ "from blop.utils import prepare_re_env\n", "\n", "%run -i $prepare_re_env.__file__ --db-type=temp\n", "\n", "from blop import DOF, Objective, Agent\n", "\n", "dofs = [\n", " DOF(name=\"x1\", search_domain=(-6, 6)),\n", " DOF(name=\"x2\", search_domain=(-6, 6)),\n", "]\n", "\n", "objectives = [\n", " Objective(name=\"booth\", target=\"min\"),\n", "]\n", "\n", "\n", "agent = Agent(\n", " dofs=dofs,\n", " objectives=objectives,\n", " digestion=digestion,\n", " db=db,\n", ")\n", "\n", "RE(agent.learn(acqf=\"qr\", n=16))\n", "\n", "agent.plot_objectives()" ] }, { "attachments": {}, "cell_type": "markdown", "id": "9ab3be01", "metadata": {}, "source": [ "In addition to modeling the fitness of the task, the agent models the probability that an input will be feasible:" ] }, { "cell_type": "code", "execution_count": null, "id": "bc53bf67", "metadata": { "tags": [] }, "outputs": [], "source": [ "agent.plot_acquisition(acqf=\"qei\")" ] }, { "cell_type": "code", "execution_count": null, "id": "ebc65169", "metadata": {}, "outputs": [], "source": [ "RE(agent.learn(\"qei\", n=4, iterations=4))\n", "agent.plot_objectives()" ] }, { "cell_type": "code", "execution_count": null, "id": "b70eaf9b", "metadata": {}, "outputs": [], "source": [ "agent.best" ] } ], "metadata": { "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.10.0" }, "vscode": { "interpreter": { "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e" } } }, "nbformat": 4, "nbformat_minor": 5 }