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 12-09 18:38:25] ax.utils.notebook.plotting: Injecting Plotly library into cell. Do not overwrite or delete cell. [INFO 12-09 18:38:25] 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 12-09 18:38:26] 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 12-09 18:38:26] 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 12-09 18:38:26] 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 12-09 18:38:26] 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 12-09 18:38:26] 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 12-09 18:38:26] 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 12-09 18:38:26] 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 12-09 18:38:26] 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 12-09 18:38:26] ax.modelbridge.dispatch_utils: Using Models.BOTORCH_MODULAR since there are more ordered parameters than there are categories for the unordered categorical parameters. [INFO 12-09 18:38:26] 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 12-09 18:38:26] ax.modelbridge.dispatch_utils: calculated num_initialization_trials=12 [INFO 12-09 18:38:26] ax.modelbridge.dispatch_utils: num_completed_initialization_trials=0 num_remaining_initialization_trials=12 [INFO 12-09 18:38:26] 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 12-09 18:38:26] 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 12-09 18:38:26] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.717836, 'x2': 0.867509, 'x3': 0.411843, 'x4': 0.557453, 'x5': 0.930346, 'x6': 0.487641}. [INFO 12-09 18:38:26] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.026916, 0.1), 'l2norm': (1.669672, 0.1)}. [INFO 12-09 18:38:26] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.364309, 'x2': 0.48863, 'x3': 0.479126, 'x4': 0.708059, 'x5': 0.833789, 'x6': 0.258907}. [INFO 12-09 18:38:26] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-0.428, 0.1), 'l2norm': (1.481896, 0.1)}. [INFO 12-09 18:38:26] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.881429, 'x2': 0.640834, 'x3': 0.583128, 'x4': 0.379465, 'x5': 0.394699, 'x6': 0.083646}. [INFO 12-09 18:38:26] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-0.035703, 0.1), 'l2norm': (1.407078, 0.1)}. [INFO 12-09 18:38:26] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.22264, 'x2': 0.546985, 'x3': 0.938589, 'x4': 0.811344, 'x5': 0.799448, 'x6': 0.3494}. [INFO 12-09 18:38:26] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.157674, 0.1), 'l2norm': (1.601964, 0.1)}. [INFO 12-09 18:38:26] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.264638, 'x2': 0.787066, 'x3': 0.569831, 'x4': 0.4533, 'x5': 0.135156, 'x6': 0.178554}. [INFO 12-09 18:38:26] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (-1.446214, 0.1), 'l2norm': (1.153204, 0.1)}. [INFO 12-09 18:38:26] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.963348, 'x2': 0.129555, 'x3': 0.196904, 'x4': 0.302237, 'x5': 0.831323, 'x6': 0.860621}. [INFO 12-09 18:38:26] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (0.037397, 0.1), 'l2norm': (1.583259, 0.1)}. [INFO 12-09 18:38:26] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.724607, 'x2': 0.829436, 'x3': 0.814764, 'x4': 0.874378, 'x5': 0.484756, 'x6': 0.200474}. [INFO 12-09 18:38:26] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.13446, 0.1), 'l2norm': (1.634034, 0.1)}. [INFO 12-09 18:38:26] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.560857, 'x2': 0.444827, 'x3': 0.482693, 'x4': 0.193291, 'x5': 0.153528, 'x6': 0.630369}. [INFO 12-09 18:38:26] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-1.113004, 0.1), 'l2norm': (1.123313, 0.1)}. [INFO 12-09 18:38:26] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.6662, 'x2': 0.966202, 'x3': 0.300971, 'x4': 0.321722, 'x5': 0.42957, 'x6': 0.909311}. [INFO 12-09 18:38:26] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.227831, 0.1), 'l2norm': (1.708066, 0.1)}. [INFO 12-09 18:38:26] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.218835, 'x2': 0.060056, 'x3': 0.419158, 'x4': 0.823246, 'x5': 0.631888, 'x6': 0.78678}. [INFO 12-09 18:38:26] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (-0.14057, 0.1), 'l2norm': (1.285344, 0.1)}. [INFO 12-09 18:38:26] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.700078, 'x2': 0.638961, 'x3': 0.642547, 'x4': 0.728778, 'x5': 0.786412, 'x6': 0.768077}. [INFO 12-09 18:38:26] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-0.083595, 0.1), 'l2norm': (1.72349, 0.1)}. [INFO 12-09 18:38:26] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.438328, 'x2': 0.132198, 'x3': 0.392784, 'x4': 0.340328, 'x5': 0.979764, 'x6': 0.811956}. [INFO 12-09 18:38:26] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.103516, 0.1), 'l2norm': (1.556576, 0.1)}. [INFO 12-09 18:38:31] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.301999, 'x2': 0.692386, 'x3': 0.546606, 'x4': 0.380032, 'x5': 0.111498, 'x6': 0.272188}. [INFO 12-09 18:38:31] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-0.959199, 0.1), 'l2norm': (1.079562, 0.1)}. [INFO 12-09 18:38:38] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.21781, 'x2': 0.819848, 'x3': 0.550377, 'x4': 0.451543, 'x5': 0.105043, 'x6': 0.112515}. [INFO 12-09 18:38:38] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-1.306366, 0.1), 'l2norm': (1.167894, 0.1)}. [INFO 12-09 18:38:44] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.251789, 'x2': 0.855182, 'x3': 0.590875, 'x4': 0.517109, 'x5': 0.14953, 'x6': 0.186671}. [INFO 12-09 18:38:44] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-1.36519, 0.1), 'l2norm': (1.191117, 0.1)}. [INFO 12-09 18:38:52] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.282151, 'x2': 0.798814, 'x3': 0.595753, 'x4': 0.482203, 'x5': 0.179543, 'x6': 0.098228}. [INFO 12-09 18:38:52] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-2.001797, 0.1), 'l2norm': (1.187921, 0.1)}. [INFO 12-09 18:38:59] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.300518, 'x2': 0.747963, 'x3': 0.593445, 'x4': 0.446647, 'x5': 0.211808, 'x6': 0.061964}. [INFO 12-09 18:38:59] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-1.92168, 0.1), 'l2norm': (0.995027, 0.1)}. [INFO 12-09 18:39:04] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.320803, 'x2': 0.808672, 'x3': 0.626271, 'x4': 0.512085, 'x5': 0.189916, 'x6': 0.043484}. [INFO 12-09 18:39:04] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-2.674456, 0.1), 'l2norm': (1.305926, 0.1)}. [INFO 12-09 18:39:19] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.355405, 'x2': 0.791758, 'x3': 0.601559, 'x4': 0.502884, 'x5': 0.134105, 'x6': 0.003507}. [INFO 12-09 18:39:19] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-2.760813, 0.1), 'l2norm': (1.159587, 0.1)}. [INFO 12-09 18:39:28] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.360559, 'x2': 0.81132, 'x3': 0.639645, 'x4': 0.534901, 'x5': 0.155799, 'x6': 0.0}. [INFO 12-09 18:39:28] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-2.954836, 0.1), 'l2norm': (1.256104, 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 12-09 18:39:28] 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 12-09 18:39:59] 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, 41.25 seconds.