Skip to content

Instantly share code, notes, and snippets.

@sgbaird
Last active April 29, 2024 14:29
Show Gist options
  • Save sgbaird/e5a9b333154feabcb68a52dde6cbb9b0 to your computer and use it in GitHub Desktop.
Save sgbaird/e5a9b333154feabcb68a52dde6cbb9b0 to your computer and use it in GitHub Desktop.
ax-service-api.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/sgbaird/e5a9b333154feabcb68a52dde6cbb9b0/ax-service-api.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rxX9kBIq1LBv"
},
"source": [
"# Service API Example on Hartmann6\n",
"\n",
"The Ax Service API is designed to allow the user to control scheduling of trials and data computation while having an easy to use interface with Ax.\n",
"\n",
"The user iteratively:\n",
"- Queries Ax for candidates\n",
"- Schedules / deploys them however they choose\n",
"- Computes data and logs to Ax\n",
"- Repeat"
]
},
{
"cell_type": "code",
"source": [
"%pip install ax-platform kaleido"
],
"metadata": {
"id": "ICBj4HJr1NU2"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:43:08.019996Z",
"iopub.status.busy": "2023-08-11T15:43:08.019705Z",
"iopub.status.idle": "2023-08-11T15:43:16.457288Z",
"shell.execute_reply": "2023-08-11T15:43:16.455716Z"
},
"id": "iCf9ww9B1LBx"
},
"outputs": [],
"source": [
"from ax.service.ax_client import AxClient, ObjectiveProperties\n",
"from ax.utils.measurement.synthetic_functions import hartmann6\n",
"from ax.utils.notebook.plotting import render, init_notebook_plotting\n",
"import plotly.io as pio\n",
"\n",
"init_notebook_plotting()\n",
"pio.renderers.default = \"colab\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dyJDvLb11LBz"
},
"source": [
"## 1. Initialize client\n",
"\n",
"Create a client object to interface with Ax APIs. By default this runs locally without storage."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:43:16.524982Z",
"iopub.status.busy": "2023-08-11T15:43:16.524473Z",
"iopub.status.idle": "2023-08-11T15:43:16.528456Z",
"shell.execute_reply": "2023-08-11T15:43:16.527902Z"
},
"id": "UmepG9g11LBz"
},
"outputs": [],
"source": [
"ax_client = AxClient()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4hRrxycN1LB0"
},
"source": [
"## 2. Set up experiment\n",
"An experiment consists of a **search space** (parameters and parameter constraints) and **optimization configuration** (objectives and outcome constraints). Note that:\n",
"- Only `parameters`, and `objectives` arguments are required.\n",
"- Dictionaries in `parameters` have the following required keys: \"name\" - parameter name, \"type\" - parameter type (\"range\", \"choice\" or \"fixed\"), \"bounds\" for range parameters, \"values\" for choice parameters, and \"value\" for fixed parameters.\n",
"- Dictionaries in `parameters` can optionally include \"value_type\" (\"int\", \"float\", \"bool\" or \"str\"), \"log_scale\" flag for range parameters, and \"is_ordered\" flag for choice parameters.\n",
"- `parameter_constraints` should be a list of strings of form \"p1 >= p2\" or \"p1 + p2 <= some_bound\".\n",
"- `outcome_constraints` should be a list of strings of form \"constrained_metric <= some_bound\"."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:43:16.531189Z",
"iopub.status.busy": "2023-08-11T15:43:16.530957Z",
"iopub.status.idle": "2023-08-11T15:43:16.542508Z",
"shell.execute_reply": "2023-08-11T15:43:16.541879Z"
},
"id": "kWObDF0Z1LB0"
},
"outputs": [],
"source": [
"ax_client.create_experiment(\n",
" name=\"hartmann_test_experiment\",\n",
" parameters=[\n",
" {\n",
" \"name\": \"x1\",\n",
" \"type\": \"range\",\n",
" \"bounds\": [0.0, 1.0],\n",
" \"value_type\": \"float\", # Optional, defaults to inference from type of \"bounds\".\n",
" \"log_scale\": False, # Optional, defaults to False.\n",
" },\n",
" {\n",
" \"name\": \"x2\",\n",
" \"type\": \"range\",\n",
" \"bounds\": [0.0, 1.0],\n",
" },\n",
" {\n",
" \"name\": \"x3\",\n",
" \"type\": \"range\",\n",
" \"bounds\": [0.0, 1.0],\n",
" },\n",
" {\n",
" \"name\": \"x4\",\n",
" \"type\": \"range\",\n",
" \"bounds\": [0.0, 1.0],\n",
" },\n",
" {\n",
" \"name\": \"x5\",\n",
" \"type\": \"range\",\n",
" \"bounds\": [0.0, 1.0],\n",
" },\n",
" {\n",
" \"name\": \"x6\",\n",
" \"type\": \"range\",\n",
" \"bounds\": [0.0, 1.0],\n",
" },\n",
" ],\n",
" objectives={\"hartmann6\": ObjectiveProperties(minimize=True)},\n",
" parameter_constraints=[\"x1 + x2 <= 2.0\"], # Optional.\n",
" # outcome_constraints=[\"l2norm <= 1.25\"], # Optional.\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "goL8jnXl1LB1"
},
"source": [
"## 3. Define how to evaluate trials\n",
"When using Ax a service, evaluation of parameterizations suggested by Ax is done either locally or, more commonly, using an external scheduler. Below is a dummy evaluation function that outputs data for two metrics \"hartmann6\" and \"l2norm\". Note that all returned metrics correspond to either the `objectives` set on experiment creation or the metric names mentioned in `outcome_constraints`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:43:16.546746Z",
"iopub.status.busy": "2023-08-11T15:43:16.546556Z",
"iopub.status.idle": "2023-08-11T15:43:16.549991Z",
"shell.execute_reply": "2023-08-11T15:43:16.549570Z"
},
"id": "KuK9I05i1LB2"
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"\n",
"def evaluate(parameters):\n",
" # Assuming you have a dictionary called 'parameters' with keys 'x1', 'x2', ..., 'x6'\n",
" # Iterate over the range [0, 1, 2, 3, 4, 5] and uses 'parameters.get()' to fetch values with keys like 'x1', 'x2', etc.\n",
" x = np.array([parameters.get(f\"x{i+1}\") for i in range(6)])\n",
"\n",
" hartmann6_mean = hartmann6(x)\n",
" l2norm_mean = np.sqrt((x**2).sum())\n",
"\n",
" # In our case, standard error is 0, since we are computing a synthetic function.\n",
" hartmann6_std = 0.0\n",
" l2norm_std = 0.0\n",
" return {\"hartmann6\": (hartmann6_mean, hartmann6_std), \"l2norm\": (l2norm_mean, l2norm_std)}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "GgCJk4Rn1LB3"
},
"source": [
"Result of the evaluation should generally be a mapping of the format: `{metric_name -> (mean, SEM)}`. If there is only one metric in the experiment – the objective – then evaluation function can return a single tuple of mean and SEM, in which case Ax will assume that evaluation corresponds to the objective. _It can also return only the mean as a float, in which case Ax will treat SEM as unknown and use a model that can infer it._\n",
"\n",
"For more details on evaluation function, refer to the \"Trial Evaluation\" section in the Ax docs at [ax.dev](https://ax.dev/)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "H9krvtYe1LB3"
},
"source": [
"## 4. Run optimization loop\n",
"With the experiment set up, we can start the optimization loop.\n",
"\n",
"At each step, the user queries the client for a new trial then submits the evaluation of that trial back to the client.\n",
"\n",
"Note that Ax auto-selects an appropriate optimization algorithm based on the search space. For more advance use cases that require a specific optimization algorithm, pass a `generation_strategy` argument into the `AxClient` constructor. Note that when Bayesian Optimization is used, generating new trials may take a few minutes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:43:16.552975Z",
"iopub.status.busy": "2023-08-11T15:43:16.552788Z",
"iopub.status.idle": "2023-08-11T15:44:22.070668Z",
"shell.execute_reply": "2023-08-11T15:44:22.069593Z"
},
"id": "VL2I_9Ha1LB4"
},
"outputs": [],
"source": [
"for i in range(25):\n",
" parameters, trial_index = ax_client.get_next_trial()\n",
" # Local evaluation here can be replaced with deployment to external system.\n",
" ax_client.complete_trial(trial_index=trial_index, raw_data=evaluate(parameters))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3IOku_rN1LB4"
},
"source": [
"### How many trials can run in parallel?\n",
"By default, Ax restricts number of trials that can run in parallel for some optimization stages, in order to improve the optimization performance and reduce the number of trials that the optimization will require. To check the maximum parallelism for each optimization stage:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:44:22.074757Z",
"iopub.status.busy": "2023-08-11T15:44:22.074428Z",
"iopub.status.idle": "2023-08-11T15:44:22.082737Z",
"shell.execute_reply": "2023-08-11T15:44:22.081784Z"
},
"id": "NXsuV_L11LB4"
},
"outputs": [],
"source": [
"ax_client.get_max_parallelism()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rrwpkd451LB4"
},
"source": [
"The output of this function is a list of tuples of form (number of trials, max parallelism), so the example above means \"the max parallelism is 12 for the first 12 trials and 3 for all subsequent trials.\" This is because the first 12 trials are produced quasi-randomly and can all be evaluated at once, and subsequent trials are produced via Bayesian optimization, which converges on optimal point in fewer trials when parallelism is limited. `MaxParallelismReachedException` indicates that the parallelism limit has been reached –– refer to the 'Service API Exceptions Meaning and Handling' section at the end of the tutorial for handling."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6b5tuHlP1LB4"
},
"source": [
"### How to view all existing trials during optimization?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:44:22.086910Z",
"iopub.status.busy": "2023-08-11T15:44:22.086596Z",
"iopub.status.idle": "2023-08-11T15:44:22.138109Z",
"shell.execute_reply": "2023-08-11T15:44:22.137147Z"
},
"id": "sHJ7QWQB1LB4"
},
"outputs": [],
"source": [
"ax_client.generation_strategy.trials_as_df"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mFIdkFIn1LB5"
},
"source": [
"## 5. Retrieve best parameters\n",
"\n",
"Once it's complete, we can access the best parameters found, as well as the corresponding metric values."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:44:22.141920Z",
"iopub.status.busy": "2023-08-11T15:44:22.141642Z",
"iopub.status.idle": "2023-08-11T15:44:22.482072Z",
"shell.execute_reply": "2023-08-11T15:44:22.481215Z"
},
"id": "WsVYLEad1LB5"
},
"outputs": [],
"source": [
"best_parameters, values = ax_client.get_best_parameters()\n",
"best_parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:44:22.486242Z",
"iopub.status.busy": "2023-08-11T15:44:22.485916Z",
"iopub.status.idle": "2023-08-11T15:44:22.491250Z",
"shell.execute_reply": "2023-08-11T15:44:22.490369Z"
},
"id": "0KgsUNQv1LB5"
},
"outputs": [],
"source": [
"means, covariances = values\n",
"means"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qkX5BPOs1LB5"
},
"source": [
"For comparison, Hartmann6 minimum:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:44:22.495320Z",
"iopub.status.busy": "2023-08-11T15:44:22.494998Z",
"iopub.status.idle": "2023-08-11T15:44:22.499932Z",
"shell.execute_reply": "2023-08-11T15:44:22.499218Z"
},
"id": "5EU55W-J1LB5"
},
"outputs": [],
"source": [
"hartmann6.fmin"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AhTIjK8W1LB5"
},
"source": [
"## 6. Plot the response surface and optimization trace\n",
"Here we arbitrarily select \"x1\" and \"x2\" as the two parameters to plot for both metrics, \"hartmann6\" and \"l2norm\"."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:44:22.503826Z",
"iopub.status.busy": "2023-08-11T15:44:22.503505Z",
"iopub.status.idle": "2023-08-11T15:44:24.037685Z",
"shell.execute_reply": "2023-08-11T15:44:24.036976Z"
},
"id": "wQv7uQHB1LB5"
},
"outputs": [],
"source": [
"render(ax_client.get_contour_plot())"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EhQIp3AB1LB6"
},
"source": [
"We can also retrieve a contour plot for the other metric, \"l2norm\" –– say, we are interested in seeing the response surface for parameters \"x3\" and \"x4\" for this one."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:44:24.044924Z",
"iopub.status.busy": "2023-08-11T15:44:24.044690Z",
"iopub.status.idle": "2023-08-11T15:44:24.661999Z",
"shell.execute_reply": "2023-08-11T15:44:24.661200Z"
},
"id": "zwSPpi8G1LB6"
},
"outputs": [],
"source": [
"render(ax_client.get_contour_plot(param_x=\"x3\", param_y=\"x4\", metric_name=\"l2norm\"))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uF9DZdzO1LB6"
},
"source": [
"Here we plot the optimization trace, showing the progression of finding the point with the optimal objective:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:44:24.670859Z",
"iopub.status.busy": "2023-08-11T15:44:24.670541Z",
"iopub.status.idle": "2023-08-11T15:44:24.747896Z",
"shell.execute_reply": "2023-08-11T15:44:24.747206Z"
},
"id": "dFZU3B7F1LB6"
},
"outputs": [],
"source": [
"render(\n",
" ax_client.get_optimization_trace(objective_optimum=hartmann6.fmin)\n",
") # Objective_optimum is optional."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "edbCmV3I1LB6"
},
"source": [
"## 7. Save / reload optimization to JSON / SQL\n",
"We can serialize the state of optimization to JSON and save it to a `.json` file or save it to the SQL backend. For the former:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:44:24.751211Z",
"iopub.status.busy": "2023-08-11T15:44:24.750995Z",
"iopub.status.idle": "2023-08-11T15:44:24.783923Z",
"shell.execute_reply": "2023-08-11T15:44:24.783318Z"
},
"id": "h3V83UzQ1LB6"
},
"outputs": [],
"source": [
"ax_client.save_to_json_file() # For custom filepath, pass `filepath` argument."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:44:24.787083Z",
"iopub.status.busy": "2023-08-11T15:44:24.786715Z",
"iopub.status.idle": "2023-08-11T15:44:25.043880Z",
"shell.execute_reply": "2023-08-11T15:44:25.043385Z"
},
"id": "BE4-toEj1LB6"
},
"outputs": [],
"source": [
"restored_ax_client = (\n",
" AxClient.load_from_json_file()\n",
") # For custom filepath, pass `filepath` argument."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PEPC7B1X1LB7"
},
"source": [
"To store state of optimization to an SQL backend, first follow [setup instructions](https://ax.dev/docs/storage.html#sql) on Ax website."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DXcGGss71LB7"
},
"source": [
"Having set up the SQL backend, pass `DBSettings` to `AxClient` on instantiation (note that `SQLAlchemy` dependency will have to be installed – for installation, refer to [optional dependencies](https://ax.dev/docs/installation.html#optional-dependencies) on Ax website):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:44:25.047094Z",
"iopub.status.busy": "2023-08-11T15:44:25.046871Z",
"iopub.status.idle": "2023-08-11T15:44:25.109091Z",
"shell.execute_reply": "2023-08-11T15:44:25.108041Z"
},
"id": "D0FcRjM41LB7"
},
"outputs": [],
"source": [
"# NOTE: Requires running `pip install ax-platform[mysql]` and setting up a database per instructions above (out of scope for this tutorial)\n",
"\n",
"# from ax.storage.sqa_store.structs import DBSettings\n",
"\n",
"# # URL is of the form \"dialect+driver://username:password@host:port/database\".\n",
"# db_settings = DBSettings(url=\"sqlite:///foo.db\")\n",
"# # Instead of URL, can provide a `creator function`; can specify custom encoders/decoders if necessary.\n",
"# new_ax = AxClient(db_settings=db_settings)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VNlL71C41LB7"
},
"source": [
"When valid `DBSettings` are passed into `AxClient`, a unique experiment name is a required argument (`name`) to `ax_client.create_experiment`. The **state of the optimization is auto-saved** any time it changes (i.e. a new trial is added or completed, etc).\n",
"\n",
"To reload an optimization state later, instantiate `AxClient` with the same `DBSettings` and use `ax_client.load_experiment_from_database(experiment_name=\"my_experiment\")`."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1OnWPygk1LB7"
},
"source": [
"# Special Cases"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "don-3dGf1LB7"
},
"source": [
"**Evaluation failure**: should any optimization iterations fail during evaluation, `log_trial_failure` will ensure that the same trial is not proposed again."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:44:25.112852Z",
"iopub.status.busy": "2023-08-11T15:44:25.112566Z",
"iopub.status.idle": "2023-08-11T15:44:29.120847Z",
"shell.execute_reply": "2023-08-11T15:44:29.119937Z"
},
"id": "TQ4CVos91LB7"
},
"outputs": [],
"source": [
"_, trial_index = ax_client.get_next_trial()\n",
"ax_client.log_trial_failure(trial_index=trial_index)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kp5Wtq-O1LB8"
},
"source": [
"**Adding custom trials**: should there be need to evaluate a specific parameterization, `attach_trial` will add it to the experiment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-11T15:44:29.124721Z",
"iopub.status.busy": "2023-08-11T15:44:29.124416Z",
"iopub.status.idle": "2023-08-11T15:44:29.130925Z",
"shell.execute_reply": "2023-08-11T15:44:29.130063Z"
},
"id": "W7OyBcNl1LB8"
},
"outputs": [],
"source": [
"ax_client.attach_trial(\n",
" parameters={\"x1\": 0.9, \"x2\": 0.9, \"x3\": 0.9, \"x4\": 0.9, \"x5\": 0.9, \"x6\": 0.9}\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "o1VqiYKa1LB9"
},
"source": [
"**Need to run many trials in parallel**: for optimal results and optimization efficiency, we strongly recommend sequential optimization (generating a few trials, then waiting for them to be completed with evaluation data). However, if your use case needs to dispatch many trials in parallel before they are updated with data and you are running into the *\"All trials for current model have been generated, but not enough data has been observed to fit next model\"* error, instantiate `AxClient` as `AxClient(enforce_sequential_optimization=False)`."
]
}
],
"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.9.17"
},
"colab": {
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment