{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Visualizations\n", "\n", "This tutorial illustrates the core visualization utilities avaialable in Ax." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "[INFO 05-01 15:02:14] ipy_plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.\n" ] } ], "source": [ "import numpy as np\n", "from ax import (\n", " Arm,\n", " ComparisonOp,\n", " RangeParameter,\n", " ParameterType,\n", " SearchSpace, \n", " SimpleExperiment, \n", " OutcomeConstraint, \n", ")\n", "\n", "from ax.metrics.l2norm import L2NormMetric\n", "from ax.modelbridge.cross_validation import cross_validate\n", "from ax.modelbridge.factory import Models\n", "from ax.plot.contour import interact_contour, plot_contour\n", "from ax.plot.diagnostic import interact_cross_validation\n", "from ax.plot.scatter import(\n", " interact_fitted,\n", " plot_objective_vs_constraints,\n", " tile_fitted,\n", ")\n", "from ax.plot.slice import plot_slice\n", "from ax.utils.measurement.synthetic_functions import hartmann6\n", "from ax.utils.notebook.plotting import render, init_notebook_plotting\n", "\n", "init_notebook_plotting()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Create experiment and run optimization\n", "\n", "The vizualizations require an experiment object and a model fit on the evaluated data. The routine below is a copy of the Developer API tutorial, so the explanation here is omitted. Retrieving the experiment and model objects for each API paradigm is shown in the respective tutorials" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1a. Define search space and evaluation function" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "noise_sd = 0.1\n", "param_names = [f\"x{i+1}\" for i in range(6)] # x1, x2, ..., x6\n", "\n", "def noisy_hartmann_evaluation_function(parameterization):\n", " x = np.array([parameterization.get(p_name) for p_name in param_names])\n", " noise1, noise2 = np.random.normal(0, noise_sd, 2)\n", "\n", " return {\n", " \"hartmann6\": (hartmann6(x) + noise1, noise_sd),\n", " \"l2norm\": (np.sqrt((x ** 2).sum()) + noise2, noise_sd)\n", " }\n", "\n", "hartmann_search_space = SearchSpace(\n", " parameters=[\n", " RangeParameter(\n", " name=p_name, parameter_type=ParameterType.FLOAT, lower=0.0, upper=1.0\n", " )\n", " for p_name in param_names\n", " ]\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1b. Create Experiment" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "exp = SimpleExperiment(\n", " name=\"test_branin\",\n", " search_space=hartmann_search_space,\n", " evaluation_function=noisy_hartmann_evaluation_function,\n", " objective_name=\"hartmann6\",\n", " minimize=True,\n", " outcome_constraints=[\n", " OutcomeConstraint(\n", " metric=L2NormMetric(\n", " name=\"l2norm\", param_names=param_names, noise_sd=0.2\n", " ),\n", " op=ComparisonOp.LEQ,\n", " bound=1.25,\n", " relative=False,\n", " )\n", " ],\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1c. Run the optimization and fit a GP on all data\n", "\n", "After doing (`N_BATCHES=15`) rounds of optimization, fit final GP using all data to feed into the plots." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "N_RANDOM = 5\n", "BATCH_SIZE = 1\n", "N_BATCHES = 15\n", "\n", "sobol = Models.SOBOL(exp.search_space)\n", "exp.new_batch_trial(generator_run=sobol.gen(N_RANDOM))\n", "\n", "for i in range(N_BATCHES):\n", " intermediate_gp = Models.GPEI(experiment=exp, data=exp.eval())\n", " exp.new_trial(generator_run=intermediate_gp.gen(BATCH_SIZE))\n", "\n", "model = Models.GPEI(experiment=exp, data=exp.eval())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Contour plots\n", "\n", "The plot below shows the response surface for `hartmann6` metric as a function of the `x1`, `x2` parameters.\n", "\n", "The other parameters are fixed in the middle of their respective ranges, which in this example is 0.5 for all of them." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "render(plot_contour(model=model, param_x=\"x1\", param_y=\"x2\", metric_name='hartmann6'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2a. Interactive contour plot\n", "\n", "The plot below allows toggling between different pairs of parameters to view the contours." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "render(interact_contour(model=model, metric_name='hartmann6'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Tradeoff plots\n", "This plot illustrates the tradeoffs achievable for 2 different metrics. The plot takes the x-axis metric as input (usually the objective) and allows toggling among all other metrics for the y-axis.\n", "\n", "This is useful to get a sense of the pareto frontier (i.e. what is the best objective value achievable for different bounds on the constraint)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "render(plot_objective_vs_constraints(model, 'hartmann6', rel=False))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Cross-validation plots\n", "\n", "CV plots are useful to check how well the model predictions calibrate against the actual measurements. If all points are close to the dashed line, then the model is a good predictor of the real data." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "cv_results = cross_validate(model)\n", "render(interact_cross_validation(cv_results))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Slice plots\n", "\n", "Slice plots show the metric outcome as a function of one parameter while fixing the others. They serve a similar function as contour plots." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "render(plot_slice(model, \"x2\", \"hartmann6\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. Tile plots\n", "\n", "Tile plots are useful for viewing the effect of each arm." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "render(interact_fitted(model, rel=False))" ] } ], "metadata": { "kernelspec": { "display_name": "python3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }