This tutorial illustrates the core visualization utilities available in Ax.
import numpy as np
from ax.modelbridge.cross_validation import cross_validate
from ax.plot.contour import interact_contour
from ax.plot.diagnostic import interact_cross_validation
from ax.plot.scatter import interact_fitted, plot_objective_vs_constraints, tile_fitted
from ax.plot.slice import plot_slice
from ax.service.ax_client import AxClient, ObjectiveProperties
from ax.utils.measurement.synthetic_functions import hartmann6
from ax.utils.notebook.plotting import init_notebook_plotting, render
init_notebook_plotting()
[INFO 05-02 22:18:26] ax.utils.notebook.plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.
[INFO 05-02 22:18:26] ax.utils.notebook.plotting: Please see (https://ax.dev/tutorials/visualizations.html#Fix-for-plots-that-are-not-rendering) if visualizations are not rendering.
The vizualizations require an experiment object and a model fit on the evaluated data. The routine below is a copy of the Service API tutorial, so the explanation here is omitted. Retrieving the experiment and model objects for each API paradigm is shown in the respective tutorials
noise_sd = 0.1
param_names = [f"x{i+1}" for i in range(6)] # x1, x2, ..., x6
def noisy_hartmann_evaluation_function(parameterization):
x = np.array([parameterization.get(p_name) for p_name in param_names])
noise1, noise2 = np.random.normal(0, noise_sd, 2)
return {
"hartmann6": (hartmann6(x) + noise1, noise_sd),
"l2norm": (np.sqrt((x**2).sum()) + noise2, noise_sd),
}
ax_client = AxClient()
ax_client.create_experiment(
name="test_visualizations",
parameters=[
{
"name": p_name,
"type": "range",
"bounds": [0.0, 1.0],
}
for p_name in param_names
],
objectives={"hartmann6": ObjectiveProperties(minimize=True)},
outcome_constraints=["l2norm <= 1.25"],
)
[INFO 05-02 22:18:27] 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 6 decimal points.
[INFO 05-02 22:18:27] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x1. If that is not the expected value type, you can explicitly specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 05-02 22:18:27] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x2. If that is not the expected value type, you can explicitly specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 05-02 22:18:27] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x3. If that is not the expected value type, you can explicitly specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 05-02 22:18:27] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x4. If that is not the expected value type, you can explicitly specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 05-02 22:18:27] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x5. If that is not the expected value type, you can explicitly specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 05-02 22:18:27] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x6. If that is not the expected value type, you can explicitly specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 05-02 22:18:27] ax.service.utils.instantiation: Created search space: SearchSpace(parameters=[RangeParameter(name='x1', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x2', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x3', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x4', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x5', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x6', parameter_type=FLOAT, range=[0.0, 1.0])], parameter_constraints=[]).
[INFO 05-02 22:18:27] ax.modelbridge.dispatch_utils: Using Models.BOTORCH_MODULAR since there is at least one ordered parameter and there are no unordered categorical parameters.
[INFO 05-02 22:18:27] ax.modelbridge.dispatch_utils: Calculating the number of remaining initialization trials based on num_initialization_trials=None max_initialization_trials=None num_tunable_parameters=6 num_trials=None use_batch_trials=False
[INFO 05-02 22:18:27] ax.modelbridge.dispatch_utils: calculated num_initialization_trials=12
[INFO 05-02 22:18:27] ax.modelbridge.dispatch_utils: num_completed_initialization_trials=0 num_remaining_initialization_trials=12
[INFO 05-02 22:18:27] ax.modelbridge.dispatch_utils: `verbose`, `disable_progbar`, and `jit_compile` are not yet supported when using `choose_generation_strategy` with ModularBoTorchModel, dropping these arguments.
[INFO 05-02 22:18:27] ax.modelbridge.dispatch_utils: Using Bayesian Optimization generation strategy: GenerationStrategy(name='Sobol+BoTorch', steps=[Sobol for 12 trials, BoTorch for subsequent trials]). Iterations after 12 will take longer to generate due to model-fitting.
for i in range(20):
parameters, trial_index = ax_client.get_next_trial()
# Local evaluation here can be replaced with deployment to external system.
ax_client.complete_trial(
trial_index=trial_index, raw_data=noisy_hartmann_evaluation_function(parameters)
)
[INFO 05-02 22:18:27] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.072748, 'x2': 0.501659, 'x3': 0.25993, 'x4': 0.946573, 'x5': 0.074083, 'x6': 0.093909} using model Sobol.
[INFO 05-02 22:18:27] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.062507, 0.1), 'l2norm': (1.245922, 0.1)}.
[INFO 05-02 22:18:27] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.870331, 'x2': 0.884144, 'x3': 0.32905, 'x4': 0.348655, 'x5': 0.018309, 'x6': 0.382576} using model Sobol.
[INFO 05-02 22:18:27] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (0.035316, 0.1), 'l2norm': (1.254974, 0.1)}.
[INFO 05-02 22:18:27] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.874784, 'x2': 0.074181, 'x3': 0.363169, 'x4': 0.851343, 'x5': 0.002129, 'x6': 0.835925} using model Sobol.
[INFO 05-02 22:18:27] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (0.006915, 0.1), 'l2norm': (1.633009, 0.1)}.
[INFO 05-02 22:18:27] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.047925, 'x2': 0.825871, 'x3': 0.222742, 'x4': 0.743798, 'x5': 0.032947, 'x6': 0.548446} using model Sobol.
[INFO 05-02 22:18:27] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.258536, 0.1), 'l2norm': (1.386114, 0.1)}.
[INFO 05-02 22:18:27] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.345831, 'x2': 0.294621, 'x3': 0.845616, 'x4': 0.460682, 'x5': 0.517062, 'x6': 0.002476} using model Sobol.
[INFO 05-02 22:18:27] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (-0.157252, 0.1), 'l2norm': (1.156689, 0.1)}.
[INFO 05-02 22:18:27] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.032923, 'x2': 0.060313, 'x3': 0.553259, 'x4': 0.368801, 'x5': 0.286852, 'x6': 0.811723} using model Sobol.
[INFO 05-02 22:18:27] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (-2.218646, 0.1), 'l2norm': (1.196299, 0.1)}.
[INFO 05-02 22:18:27] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.242648, 'x2': 0.505711, 'x3': 0.55175, 'x4': 0.433789, 'x5': 0.506815, 'x6': 0.760031} using model Sobol.
[INFO 05-02 22:18:27] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.830963, 0.1), 'l2norm': (1.252556, 0.1)}.
[INFO 05-02 22:18:27] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.084274, 'x2': 0.207129, 'x3': 0.928566, 'x4': 0.614309, 'x5': 0.942062, 'x6': 0.876934} using model Sobol.
[INFO 05-02 22:18:27] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-0.02264, 0.1), 'l2norm': (1.636417, 0.1)}.
[INFO 05-02 22:18:27] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.474176, 'x2': 0.283934, 'x3': 0.86632, 'x4': 0.303013, 'x5': 0.562885, 'x6': 0.537905} using model Sobol.
[INFO 05-02 22:18:27] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.23663, 0.1), 'l2norm': (1.33125, 0.1)}.
[INFO 05-02 22:18:27] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.731912, 'x2': 0.444271, 'x3': 0.061336, 'x4': 0.449137, 'x5': 0.022246, 'x6': 0.337693} using model Sobol.
[INFO 05-02 22:18:27] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (0.068903, 0.1), 'l2norm': (0.923901, 0.1)}.
[INFO 05-02 22:18:27] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.114644, 'x2': 0.984508, 'x3': 0.583411, 'x4': 0.436938, 'x5': 0.697563, 'x6': 0.787443} using model Sobol.
[INFO 05-02 22:18:27] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-0.13166, 0.1), 'l2norm': (1.578783, 0.1)}.
[INFO 05-02 22:18:27] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.187027, 'x2': 0.521519, 'x3': 0.982817, 'x4': 0.693045, 'x5': 0.448665, 'x6': 0.519258} using model Sobol.
[INFO 05-02 22:18:27] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.147945, 0.1), 'l2norm': (1.493089, 0.1)}.
[INFO 05-02 22:18:35] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.098513, 'x2': 0.202258, 'x3': 0.507267, 'x4': 0.322569, 'x5': 0.298028, 'x6': 0.75312} using model BoTorch.
[INFO 05-02 22:18:35] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-2.934783, 0.1), 'l2norm': (1.010958, 0.1)}.
[INFO 05-02 22:18:40] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.089935, 'x2': 0.215298, 'x3': 0.445568, 'x4': 0.253093, 'x5': 0.268339, 'x6': 0.739325} using model BoTorch.
[INFO 05-02 22:18:40] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-3.147859, 0.1), 'l2norm': (0.879601, 0.1)}.
[INFO 05-02 22:18:43] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.095335, 'x2': 0.251265, 'x3': 0.499367, 'x4': 0.225639, 'x5': 0.214923, 'x6': 0.776927} using model BoTorch.
[INFO 05-02 22:18:43] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-2.250654, 0.1), 'l2norm': (1.056715, 0.1)}.
[INFO 05-02 22:18:47] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.090054, 'x2': 0.208798, 'x3': 0.401803, 'x4': 0.255464, 'x5': 0.302799, 'x6': 0.71478} using model BoTorch.
[INFO 05-02 22:18:47] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-2.842875, 0.1), 'l2norm': (0.980735, 0.1)}.
[INFO 05-02 22:18:50] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.1292, 'x2': 0.219514, 'x3': 0.448855, 'x4': 0.319039, 'x5': 0.271014, 'x6': 0.69052} using model BoTorch.
[INFO 05-02 22:18:50] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-2.924677, 0.1), 'l2norm': (0.687589, 0.1)}.
[INFO 05-02 22:18:55] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.057607, 'x2': 0.271694, 'x3': 0.444438, 'x4': 0.295688, 'x5': 0.280567, 'x6': 0.776034} using model BoTorch.
[INFO 05-02 22:18:55] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-2.346059, 0.1), 'l2norm': (0.992336, 0.1)}.
[INFO 05-02 22:18:57] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.098117, 'x2': 0.169062, 'x3': 0.460488, 'x4': 0.254112, 'x5': 0.267578, 'x6': 0.727593} using model BoTorch.
[INFO 05-02 22:18:57] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-3.097835, 0.1), 'l2norm': (0.959907, 0.1)}.
[INFO 05-02 22:19:00] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.161962, 'x2': 0.176271, 'x3': 0.437014, 'x4': 0.262446, 'x5': 0.274465, 'x6': 0.779175} using model BoTorch.
[INFO 05-02 22:19:00] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-2.868746, 0.1), 'l2norm': (1.040622, 0.1)}.
The plot below shows the response surface for hartmann6
metric as a function of the x1
, x2
parameters.
The other parameters are fixed in the middle of their respective ranges, which in this example is 0.5 for all of them.
# this could alternately be done with `ax.plot.contour.plot_contour`
render(ax_client.get_contour_plot(param_x="x1", param_y="x2", metric_name="hartmann6"))
[INFO 05-02 22:19:00] ax.service.ax_client: Retrieving contour plot with parameter 'x1' on X-axis and 'x2' on Y-axis, for metric 'hartmann6'. Remaining parameters are affixed to the middle of their range.
The plot below allows toggling between different pairs of parameters to view the contours.
model = ax_client.generation_strategy.model
render(interact_contour(model=model, metric_name="hartmann6"))
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.
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)
render(plot_objective_vs_constraints(model, "hartmann6", rel=False))
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.
cv_results = cross_validate(model)
render(interact_cross_validation(cv_results))
Slice plots show the metric outcome as a function of one parameter while fixing the others. They serve a similar function as contour plots.
render(plot_slice(model, "x2", "hartmann6"))
Tile plots are useful for viewing the effect of each arm.
render(interact_fitted(model, rel=False))
In certain environments like Google Colab or remote setups, plots may not render. If this is the case, we recommend using the below workaround which overrides the default renderer in plotly. The below cell changes the renderer to "jupyterlab" for this tutorial, but you can find the right renderer for your use case by calling pio.renderers
import plotly.io as pio
pio.renderers.default = "jupyterlab"
render(ax_client.get_contour_plot(param_x="x1", param_y="x2", metric_name="hartmann6"))
[INFO 05-02 22:19:33] ax.service.ax_client: Retrieving contour plot with parameter 'x1' on X-axis and 'x2' on Y-axis, for metric 'hartmann6'. Remaining parameters are affixed to the middle of their range.
Total runtime of script: 1 minutes, 16.27 seconds.