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.
The user iteratively:
from ax.service.ax_client import AxClient
from ax.utils.measurement.synthetic_functions import hartmann6
from ax.utils.notebook.plotting import render, init_notebook_plotting
init_notebook_plotting()
[INFO 09-24 14:46:38] ipy_plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.
Create a client object to interface with Ax APIs. By default this runs locally without storage.
ax = AxClient()
[INFO 09-24 14:46:38] ax.service.ax_client: Starting optimization with verbose logging. To disable logging, set the `verbose_logging` argument to `False`. Note that float values in the logs are rounded to 2 decimal points.
An experiment consists of a search space (parameters and parameter constraints) and optimization configuration (objective name, minimization setting, and outcome constraints). Note that:
name
, parameters
, and objective_name
arguments are required.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.parameters
can optionally include "value_type" ("int", "float", "bool" or "str"), "log_scale" flag for range parameters, and "is_ordered" flag for choice parameters.parameter_constraints
should be a list of strings of form "p1 >= p2" or "p1 + p2 <= some_bound".outcome_constraints
should be a list of strings of form "constrained_metric <= some_bound".ax.create_experiment(
name="hartmann_test_experiment",
parameters=[
{
"name": "x1",
"type": "range",
"bounds": [0.0, 1.0],
"value_type": "float", # Optional, defaults to inference from type of "bounds".
"log_scale": False, # Optional, defaults to False.
},
{
"name": "x2",
"type": "range",
"bounds": [0.0, 1.0],
},
{
"name": "x3",
"type": "range",
"bounds": [0.0, 1.0],
},
{
"name": "x4",
"type": "range",
"bounds": [0.0, 1.0],
},
{
"name": "x5",
"type": "range",
"bounds": [0.0, 1.0],
},
{
"name": "x6",
"type": "range",
"bounds": [0.0, 1.0],
},
],
objective_name="hartmann6",
minimize=True, # Optional, defaults to False.
parameter_constraints=["x1 + x2 <= 2.0"], # Optional.
outcome_constraints=["l2norm <= 1.25"], # Optional.
)
[INFO 09-24 14:46:38] ax.service.utils.dispatch: Using Bayesian Optimization generation strategy: GenerationStrategy(name='Sobol+GPEI', steps=[Sobol for 6 arms, GPEI for subsequent arms], generated 0 arm(s) so far). Iterations after 6 will take longer to generate due to model-fitting.
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 objective_name
set on experiment creation or the metric names mentioned in outcome_constraints
.
import numpy as np
def evaluate(parameters):
x = np.array([parameters.get(f"x{i+1}") for i in range(6)])
# In our case, standard error is 0, since we are computing a synthetic function.
return {"hartmann6": (hartmann6(x), 0.0), "l2norm": (np.sqrt((x ** 2).sum()), 0.0)}
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.
For more details on evaluation function, refer to the "Trial Evaluation" section in the Ax docs at ax.dev
With the experiment set up, we can start the optimization loop.
At each step, the user queries the client for a new trial then submits the evaluation of that trial back to the client.
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.
for i in range(25):
parameters, trial_index = ax.get_next_trial()
# Local evaluation here can be replaced with deployment to external system.
ax.complete_trial(trial_index=trial_index, raw_data=evaluate(parameters))
[INFO 09-24 14:46:38] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.04, 'x2': 0.45, 'x3': 0.49, 'x4': 0.94, 'x5': 0.04, 'x6': 0.64}. [INFO 09-24 14:46:38] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.05, 0.0), 'l2norm': (1.32, 0.0)}. [INFO 09-24 14:46:38] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.29, 'x2': 0.6, 'x3': 0.17, 'x4': 0.49, 'x5': 0.48, 'x6': 0.11}. [INFO 09-24 14:46:38] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-1.16, 0.0), 'l2norm': (0.98, 0.0)}. [INFO 09-24 14:46:38] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.85, 'x2': 0.12, 'x3': 0.87, 'x4': 0.71, 'x5': 0.64, 'x6': 0.93}. [INFO 09-24 14:46:38] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-0.05, 0.0), 'l2norm': (1.81, 0.0)}. [INFO 09-24 14:46:38] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.89, 'x2': 0.73, 'x3': 0.27, 'x4': 0.08, 'x5': 0.29, 'x6': 0.8}. [INFO 09-24 14:46:38] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.14, 0.0), 'l2norm': (1.46, 0.0)}. [INFO 09-24 14:46:38] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.47, 'x2': 0.25, 'x3': 0.69, 'x4': 0.86, 'x5': 0.57, 'x6': 0.23}. [INFO 09-24 14:46:38] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (-0.04, 0.0), 'l2norm': (1.37, 0.0)}. [INFO 09-24 14:46:38] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.21, 'x2': 0.83, 'x3': 0.89, 'x4': 0.31, 'x5': 0.88, 'x6': 0.52}. [INFO 09-24 14:46:38] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (-0.06, 0.0), 'l2norm': (1.63, 0.0)}. [INFO 09-24 14:46:48] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.31, 'x2': 0.63, 'x3': 0.05, 'x4': 0.45, 'x5': 0.47, 'x6': 0.06}. [INFO 09-24 14:46:48] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-1.33, 0.0), 'l2norm': (0.96, 0.0)}. [INFO 09-24 14:46:58] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.23, 'x2': 0.72, 'x3': 0.02, 'x4': 0.45, 'x5': 0.46, 'x6': 0.01}. [INFO 09-24 14:46:58] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-1.2, 0.0), 'l2norm': (0.99, 0.0)}. [INFO 09-24 14:47:07] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.29, 'x2': 0.53, 'x3': 0.02, 'x4': 0.37, 'x5': 0.5, 'x6': 0.02}. [INFO 09-24 14:47:07] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.6, 0.0), 'l2norm': (0.87, 0.0)}. [INFO 09-24 14:47:16] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.35, 'x2': 0.67, 'x3': 0.05, 'x4': 0.5, 'x5': 0.44, 'x6': 0.1}. [INFO 09-24 14:47:16] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (-1.87, 0.0), 'l2norm': (1.01, 0.0)}. [INFO 09-24 14:47:26] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.4, 'x2': 0.73, 'x3': 0.02, 'x4': 0.55, 'x5': 0.41, 'x6': 0.12}. [INFO 09-24 14:47:26] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-2.32, 0.0), 'l2norm': (1.09, 0.0)}. [INFO 09-24 14:47:38] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.45, 'x2': 0.81, 'x3': 0.0, 'x4': 0.6, 'x5': 0.39, 'x6': 0.15}. [INFO 09-24 14:47:38] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-2.42, 0.0), 'l2norm': (1.18, 0.0)}. [INFO 09-24 14:47:50] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.47, 'x2': 0.76, 'x3': 0.02, 'x4': 0.56, 'x5': 0.3, 'x6': 0.12}. [INFO 09-24 14:47:50] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-2.32, 0.0), 'l2norm': (1.11, 0.0)}. [INFO 09-24 14:48:04] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.43, 'x2': 0.8, 'x3': 0.0, 'x4': 0.53, 'x5': 0.39, 'x6': 0.2}. [INFO 09-24 14:48:04] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-2.0, 0.0), 'l2norm': (1.14, 0.0)}. [INFO 09-24 14:48:18] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.44, 'x2': 0.78, 'x3': 0.0, 'x4': 0.64, 'x5': 0.4, 'x6': 0.09}. [INFO 09-24 14:48:18] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-2.5, 0.0), 'l2norm': (1.18, 0.0)}. [INFO 09-24 14:48:29] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.37, 'x2': 0.77, 'x3': 0.0, 'x4': 0.67, 'x5': 0.34, 'x6': 0.12}. [INFO 09-24 14:48:29] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-2.32, 0.0), 'l2norm': (1.14, 0.0)}. [INFO 09-24 14:48:41] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.46, 'x2': 0.84, 'x3': 0.0, 'x4': 0.6, 'x5': 0.41, 'x6': 0.08}. [INFO 09-24 14:48:41] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-2.77, 0.0), 'l2norm': (1.2, 0.0)}. [INFO 09-24 14:48:59] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.45, 'x2': 0.91, 'x3': 0.0, 'x4': 0.56, 'x5': 0.41, 'x6': 0.06}. [INFO 09-24 14:48:59] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-2.91, 0.0), 'l2norm': (1.23, 0.0)}. [INFO 09-24 14:49:16] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.44, 'x2': 0.93, 'x3': 0.08, 'x4': 0.56, 'x5': 0.39, 'x6': 0.06}. [INFO 09-24 14:49:16] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-2.94, 0.0), 'l2norm': (1.23, 0.0)}. [INFO 09-24 14:49:32] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.49, 'x2': 0.91, 'x3': 0.07, 'x4': 0.52, 'x5': 0.41, 'x6': 0.04}. [INFO 09-24 14:49:32] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-2.62, 0.0), 'l2norm': (1.23, 0.0)}. [INFO 09-24 14:49:35] ax.service.ax_client: Generated new trial 20 with parameters {'x1': 0.66, 'x2': 0.04, 'x3': 0.47, 'x4': 0.21, 'x5': 0.9, 'x6': 0.35}. [INFO 09-24 14:49:35] ax.service.ax_client: Completed trial 20 with data: {'hartmann6': (-0.03, 0.0), 'l2norm': (1.28, 0.0)}. [INFO 09-24 14:49:49] ax.service.ax_client: Generated new trial 21 with parameters {'x1': 0.41, 'x2': 0.94, 'x3': 0.04, 'x4': 0.59, 'x5': 0.37, 'x6': 0.07}. [INFO 09-24 14:49:49] ax.service.ax_client: Completed trial 21 with data: {'hartmann6': (-2.96, 0.0), 'l2norm': (1.24, 0.0)}. [INFO 09-24 14:49:53] ax.service.ax_client: Generated new trial 22 with parameters {'x1': 0.02, 'x2': 0.62, 'x3': 0.06, 'x4': 0.74, 'x5': 0.6, 'x6': 0.63}. [INFO 09-24 14:49:53] ax.service.ax_client: Completed trial 22 with data: {'hartmann6': (-0.03, 0.0), 'l2norm': (1.3, 0.0)}. [INFO 09-24 14:49:56] ax.service.ax_client: Generated new trial 23 with parameters {'x1': 0.91, 'x2': 0.99, 'x3': 0.78, 'x4': 0.17, 'x5': 0.32, 'x6': 0.98}. [INFO 09-24 14:49:56] ax.service.ax_client: Completed trial 23 with data: {'hartmann6': (-0.05, 0.0), 'l2norm': (1.87, 0.0)}. [INFO 09-24 14:50:05] ax.service.ax_client: Generated new trial 24 with parameters {'x1': 0.42, 'x2': 0.92, 'x3': 0.06, 'x4': 0.57, 'x5': 0.41, 'x6': 0.08}. [INFO 09-24 14:50:05] ax.service.ax_client: Completed trial 24 with data: {'hartmann6': (-2.96, 0.0), 'l2norm': (1.24, 0.0)}.
To view all trials in a data frame at any point during optimization:
ax.get_trials_data_frame().sort_values('trial_index')
/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/pandas/core/reshape/merge.py:617: UserWarning: merging between different levels can give an unintended result (2 levels on the left, 1 on the right)
arm_name | hartmann6 | l2norm | trial_index | x1 | x2 | x3 | x4 | x5 | x6 | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 0_0 | -0.0484269 | 1.31858 | 0 | 0.041149 | 0.449423 | 4.941352e-01 | 0.935055 | 0.040936 | 0.644046 |
3 | 1_0 | -1.15946 | 0.976463 | 1 | 0.285768 | 0.600216 | 1.663477e-01 | 0.489105 | 0.483187 | 0.105788 |
15 | 2_0 | -0.052796 | 1.80532 | 2 | 0.852216 | 0.116015 | 8.710344e-01 | 0.705258 | 0.637039 | 0.926026 |
18 | 3_0 | -0.139436 | 1.45983 | 3 | 0.892099 | 0.730785 | 2.660937e-01 | 0.084308 | 0.288732 | 0.799951 |
19 | 4_0 | -0.0416481 | 1.36665 | 4 | 0.466343 | 0.246096 | 6.893221e-01 | 0.859999 | 0.567588 | 0.229698 |
20 | 5_0 | -0.0556183 | 1.63403 | 5 | 0.206099 | 0.834636 | 8.930215e-01 | 0.305682 | 0.877038 | 0.520411 |
21 | 6_0 | -1.32837 | 0.956853 | 6 | 0.306756 | 0.626719 | 5.016168e-02 | 0.448183 | 0.470506 | 0.062703 |
22 | 7_0 | -1.20079 | 0.990633 | 7 | 0.225050 | 0.718886 | 1.995630e-02 | 0.445996 | 0.463194 | 0.006988 |
23 | 8_0 | -0.598285 | 0.865196 | 8 | 0.288064 | 0.527412 | 1.693621e-02 | 0.369213 | 0.500501 | 0.017689 |
24 | 9_0 | -1.86537 | 1.01344 | 9 | 0.349452 | 0.672030 | 4.723547e-02 | 0.497091 | 0.441550 | 0.095014 |
1 | 10_0 | -2.32342 | 1.08563 | 10 | 0.401323 | 0.733901 | 2.049776e-02 | 0.545837 | 0.406674 | 0.123212 |
2 | 11_0 | -2.41751 | 1.17863 | 11 | 0.445116 | 0.810274 | 0.000000e+00 | 0.602813 | 0.386921 | 0.146278 |
4 | 12_0 | -2.31614 | 1.10765 | 12 | 0.471792 | 0.763103 | 1.697528e-02 | 0.561236 | 0.302624 | 0.122960 |
5 | 13_0 | -1.99798 | 1.14318 | 13 | 0.432437 | 0.802938 | 4.435467e-03 | 0.532340 | 0.389808 | 0.199458 |
6 | 14_0 | -2.50425 | 1.17796 | 14 | 0.443628 | 0.779911 | 1.869401e-16 | 0.644795 | 0.397612 | 0.093039 |
7 | 15_0 | -2.32173 | 1.14409 | 15 | 0.373997 | 0.771676 | 1.026428e-16 | 0.666368 | 0.341020 | 0.115092 |
8 | 16_0 | -2.76507 | 1.20058 | 16 | 0.459369 | 0.837357 | 0.000000e+00 | 0.595089 | 0.409753 | 0.084675 |
9 | 17_0 | -2.91444 | 1.22892 | 17 | 0.448618 | 0.907157 | 8.626423e-04 | 0.562055 | 0.407462 | 0.064273 |
10 | 18_0 | -2.94072 | 1.23394 | 18 | 0.441615 | 0.925853 | 7.530165e-02 | 0.558100 | 0.386883 | 0.059623 |
11 | 19_0 | -2.62345 | 1.23357 | 19 | 0.490722 | 0.912290 | 6.540343e-02 | 0.523037 | 0.411300 | 0.040055 |
12 | 20_0 | -0.0286595 | 1.27978 | 20 | 0.662944 | 0.040698 | 4.689431e-01 | 0.205543 | 0.902362 | 0.346805 |
13 | 21_0 | -2.96116 | 1.23918 | 21 | 0.406477 | 0.938609 | 4.138984e-02 | 0.586165 | 0.373635 | 0.066750 |
14 | 22_0 | -0.0318074 | 1.30305 | 22 | 0.020123 | 0.622116 | 5.737688e-02 | 0.738401 | 0.600414 | 0.633616 |
16 | 23_0 | -0.0451738 | 1.87454 | 23 | 0.912273 | 0.989336 | 7.780936e-01 | 0.174573 | 0.322604 | 0.981276 |
17 | 24_0 | -2.95939 | 1.23744 | 24 | 0.415569 | 0.924813 | 6.458696e-02 | 0.567884 | 0.413013 | 0.077715 |
Once it's complete, we can access the best parameters found, as well as the corresponding metric values.
best_parameters, values = ax.get_best_parameters()
best_parameters
{'x1': 0.4064773338609024, 'x2': 0.938608765232727, 'x3': 0.04138983684782125, 'x4': 0.5861650473548686, 'x5': 0.3736346402677701, 'x6': 0.06674968638342735}
means, covariances = values
means
{'hartmann6': -2.9611594090792632, 'l2norm': 1.239180918514228}
For comparison, Hartmann6 minimum:
hartmann6.fmin
-3.32237
Here we arbitrarily select "x1" and "x2" as the two parameters to plot for both metrics, "hartmann6" and "l2norm".
render(ax.get_contour_plot())
[INFO 09-24 14:50:05] ax.service.ax_client: Retrieving contour plot with parameter 'x1' on X-axis and 'x2' on Y-axis, for metric 'hartmann6'. Ramaining parameters are affixed to the middle of their range.
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.
render(ax.get_contour_plot(param_x="x3", param_y="x4", metric_name="l2norm"))
[INFO 09-24 14:50:08] ax.service.ax_client: Retrieving contour plot with parameter 'x3' on X-axis and 'x4' on Y-axis, for metric 'l2norm'. Ramaining parameters are affixed to the middle of their range.
Here we plot the optimization trace, showing the progression of finding the point with the optimal objective:
render(ax.get_optimization_trace(objective_optimum=hartmann6.fmin)) # Objective_optimum is optional.
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:
ax.save_to_json_file() # For custom filepath, pass `filepath` argument.
[INFO 09-24 14:50:12] ax.service.ax_client: Saved JSON-serialized state of optimization to `ax_client_snapshot.json`.
restored_ax_client = AxClient.load_from_json_file() # For custom filepath, pass `filepath` argument.
[INFO 09-24 14:50:12] ax.service.ax_client: Starting optimization with verbose logging. To disable logging, set the `verbose_logging` argument to `False`. Note that float values in the logs are rounded to 2 decimal points.
To store state of optimization to an SQL backend, first follow setup instructions on Ax website.
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 on Ax website):
from ax.storage.sqa_store.structs import DBSettings
# URL is of the form "dialect+driver://username:password@host:port/database".
db_settings = DBSettings(url="postgresql+psycopg2://sarah:c82i94d@ocalhost:5432/foobar")
# Instead of URL, can provide a `creator function`; can specify custom encoders/decoders if necessary.
new_ax = AxClient(db_settings=db_settings)
[INFO 09-24 14:50:12] ax.service.ax_client: Starting optimization with verbose logging. To disable logging, set the `verbose_logging` argument to `False`. Note that float values in the logs are rounded to 2 decimal points.
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).
To reload an optimization state later, instantiate AxClient
with the same DBSettings
and use ax.load_experiment_from_database(experiment_name="my_experiment")
.
Evaluation failure: should any optimization iterations fail during evaluation, log_trial_failure
will ensure that the same trial is not proposed again.
_, trial_index = ax.get_next_trial()
ax.log_trial_failure(trial_index=trial_index)
[INFO 09-24 14:50:16] ax.service.ax_client: Generated new trial 25 with parameters {'x1': 0.06, 'x2': 0.29, 'x3': 0.64, 'x4': 0.79, 'x5': 0.48, 'x6': 0.4}. [INFO 09-24 14:50:16] ax.service.ax_client: Registered failure of trial 25.
Adding custom trials: should there be need to evaluate a specific parameterization, attach_trial
will add it to the experiment.
ax.attach_trial(parameters={"x1": 9.0, "x2": 9.0, "x3": 9.0, "x4": 9.0, "x5": 9.0, "x6": 9.0})
[INFO 09-24 14:50:16] ax.service.ax_client: Attached custom parameterization {'x1': 9.0, 'x2': 9.0, 'x3': 9.0, 'x4': 9.0, 'x5': 9.0, 'x6': 9.0} as trial 26.
({'x1': 9.0, 'x2': 9.0, 'x3': 9.0, 'x4': 9.0, 'x5': 9.0, 'x6': 9.0}, 26)
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)
.
Total runtime of script: 3 minutes, 41.96 seconds.