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 07-23 19:37:59] ax.utils.notebook.plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.
[INFO 07-23 19:37:59] 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 07-23 19:37:59] 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 07-23 19:37:59] 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 07-23 19:37:59] 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 07-23 19:37:59] 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 07-23 19:37:59] 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 07-23 19:37:59] 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 07-23 19:37:59] 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 07-23 19:37:59] 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 07-23 19:37:59] ax.modelbridge.dispatch_utils: Using Models.BOTORCH_MODULAR since there is at least one ordered parameter and there are no unordered categorical parameters.
[INFO 07-23 19:37:59] 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 07-23 19:37:59] ax.modelbridge.dispatch_utils: calculated num_initialization_trials=12
[INFO 07-23 19:37:59] ax.modelbridge.dispatch_utils: num_completed_initialization_trials=0 num_remaining_initialization_trials=12
[INFO 07-23 19:37:59] 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 07-23 19:37:59] 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)
)
/tmp/tmp.DL1QmpHQMI/Ax-main/ax/modelbridge/cross_validation.py:462: UserWarning: Encountered exception in computing model fit quality: RandomModelBridge does not support prediction. [INFO 07-23 19:38:00] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.005029, 'x2': 0.536797, 'x3': 0.498193, 'x4': 0.128202, 'x5': 0.24232, 'x6': 0.566737} using model Sobol.
[INFO 07-23 19:38:00] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-1.314676, 0.1), 'l2norm': (0.954271, 0.1)}.
/tmp/tmp.DL1QmpHQMI/Ax-main/ax/modelbridge/cross_validation.py:462: UserWarning: Encountered exception in computing model fit quality: RandomModelBridge does not support prediction. [INFO 07-23 19:38:00] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.645062, 'x2': 0.660466, 'x3': 0.007177, 'x4': 0.899662, 'x5': 0.297496, 'x6': 0.854738} using model Sobol.
[INFO 07-23 19:38:00] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (0.036733, 0.1), 'l2norm': (1.412671, 0.1)}.
/tmp/tmp.DL1QmpHQMI/Ax-main/ax/modelbridge/cross_validation.py:462: UserWarning: Encountered exception in computing model fit quality: RandomModelBridge does not support prediction. [INFO 07-23 19:38:00] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.060622, 'x2': 0.488749, 'x3': 0.270709, 'x4': 0.356009, 'x5': 0.179082, 'x6': 0.580092} using model Sobol.
[INFO 07-23 19:38:00] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-1.297832, 0.1), 'l2norm': (0.899584, 0.1)}.
/tmp/tmp.DL1QmpHQMI/Ax-main/ax/modelbridge/cross_validation.py:462: UserWarning: Encountered exception in computing model fit quality: RandomModelBridge does not support prediction. [INFO 07-23 19:38:00] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.55685, 'x2': 0.804798, 'x3': 0.189441, 'x4': 0.989962, 'x5': 0.853408, 'x6': 0.140541} using model Sobol.
[INFO 07-23 19:38:00] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.135698, 0.1), 'l2norm': (1.645901, 0.1)}.
/tmp/tmp.DL1QmpHQMI/Ax-main/ax/modelbridge/cross_validation.py:462: UserWarning: Encountered exception in computing model fit quality: RandomModelBridge does not support prediction. [INFO 07-23 19:38:00] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.635358, 'x2': 0.661891, 'x3': 0.69756, 'x4': 0.971525, 'x5': 0.8347, 'x6': 0.443203} using model Sobol.
[INFO 07-23 19:38:00] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (0.012793, 0.1), 'l2norm': (1.862247, 0.1)}.
/tmp/tmp.DL1QmpHQMI/Ax-main/ax/modelbridge/cross_validation.py:462: UserWarning: Encountered exception in computing model fit quality: RandomModelBridge does not support prediction. [INFO 07-23 19:38:00] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.079007, 'x2': 0.266237, 'x3': 0.240404, 'x4': 0.507038, 'x5': 0.077789, 'x6': 0.899208} using model Sobol.
[INFO 07-23 19:38:00] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (-0.364899, 0.1), 'l2norm': (1.146802, 0.1)}.
/tmp/tmp.DL1QmpHQMI/Ax-main/ax/modelbridge/cross_validation.py:462: UserWarning: Encountered exception in computing model fit quality: RandomModelBridge does not support prediction. [INFO 07-23 19:38:00] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.582654, 'x2': 0.655124, 'x3': 0.98322, 'x4': 0.281674, 'x5': 0.841391, 'x6': 0.104157} using model Sobol.
[INFO 07-23 19:38:00] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.313359, 0.1), 'l2norm': (1.676689, 0.1)}.
/tmp/tmp.DL1QmpHQMI/Ax-main/ax/modelbridge/cross_validation.py:462: UserWarning: Encountered exception in computing model fit quality: RandomModelBridge does not support prediction. [INFO 07-23 19:38:00] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.426719, 'x2': 0.3738, 'x3': 0.616366, 'x4': 0.81264, 'x5': 0.030149, 'x6': 0.307442} using model Sobol.
[INFO 07-23 19:38:00] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-0.190222, 0.1), 'l2norm': (1.252231, 0.1)}.
/tmp/tmp.DL1QmpHQMI/Ax-main/ax/modelbridge/cross_validation.py:462: UserWarning: Encountered exception in computing model fit quality: RandomModelBridge does not support prediction. [INFO 07-23 19:38:00] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.893668, 'x2': 0.138902, 'x3': 0.648711, 'x4': 0.62237, 'x5': 0.441383, 'x6': 0.49616} using model Sobol.
[INFO 07-23 19:38:00] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.004672, 0.1), 'l2norm': (1.410809, 0.1)}.
/tmp/tmp.DL1QmpHQMI/Ax-main/ax/modelbridge/cross_validation.py:462: UserWarning: Encountered exception in computing model fit quality: RandomModelBridge does not support prediction. [INFO 07-23 19:38:00] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.90315, 'x2': 0.070489, 'x3': 0.406913, 'x4': 0.864679, 'x5': 0.156146, 'x6': 0.075863} using model Sobol.
[INFO 07-23 19:38:00] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (0.144427, 0.1), 'l2norm': (1.476797, 0.1)}.
/tmp/tmp.DL1QmpHQMI/Ax-main/ax/modelbridge/cross_validation.py:462: UserWarning: Encountered exception in computing model fit quality: RandomModelBridge does not support prediction. [INFO 07-23 19:38:00] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.120365, 'x2': 0.691714, 'x3': 0.19489, 'x4': 0.534393, 'x5': 0.104493, 'x6': 0.134331} using model Sobol.
[INFO 07-23 19:38:00] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-0.417549, 0.1), 'l2norm': (1.079256, 0.1)}.
/tmp/tmp.DL1QmpHQMI/Ax-main/ax/modelbridge/cross_validation.py:462: UserWarning: Encountered exception in computing model fit quality: RandomModelBridge does not support prediction. [INFO 07-23 19:38:00] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.047774, 'x2': 0.213587, 'x3': 0.856849, 'x4': 0.797774, 'x5': 0.389709, 'x6': 0.100644} using model Sobol.
[INFO 07-23 19:38:00] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.119663, 0.1), 'l2norm': (1.207949, 0.1)}.
[INFO 07-23 19:38:01] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.014492, 'x2': 0.532679, 'x3': 0.347245, 'x4': 0.222015, 'x5': 0.207501, 'x6': 0.556295} using model BoTorch.
[INFO 07-23 19:38:01] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-1.205324, 0.1), 'l2norm': (1.003972, 0.1)}.
[INFO 07-23 19:38:03] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.076827, 'x2': 0.484831, 'x3': 0.49298, 'x4': 0.279434, 'x5': 0.225764, 'x6': 0.590385} using model BoTorch.
[INFO 07-23 19:38:03] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-1.813216, 0.1), 'l2norm': (0.947904, 0.1)}.
[INFO 07-23 19:38:05] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.125147, 'x2': 0.482602, 'x3': 0.562263, 'x4': 0.320569, 'x5': 0.248091, 'x6': 0.597693} using model BoTorch.
[INFO 07-23 19:38:05] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-2.093941, 0.1), 'l2norm': (0.859925, 0.1)}.
[INFO 07-23 19:38:06] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.128409, 'x2': 0.523032, 'x3': 0.654072, 'x4': 0.360235, 'x5': 0.255691, 'x6': 0.620561} using model BoTorch.
[INFO 07-23 19:38:06] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-1.727028, 0.1), 'l2norm': (1.33117, 0.1)}.
[INFO 07-23 19:38:09] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.172432, 'x2': 0.429106, 'x3': 0.544233, 'x4': 0.29239, 'x5': 0.271603, 'x6': 0.577619} using model BoTorch.
[INFO 07-23 19:38:09] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-2.330821, 0.1), 'l2norm': (0.834112, 0.1)}.
[INFO 07-23 19:38:11] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.182029, 'x2': 0.403679, 'x3': 0.527473, 'x4': 0.276148, 'x5': 0.352344, 'x6': 0.578771} using model BoTorch.
[INFO 07-23 19:38:11] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-2.508585, 0.1), 'l2norm': (1.027769, 0.1)}.
[INFO 07-23 19:38:13] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.234979, 'x2': 0.413404, 'x3': 0.519195, 'x4': 0.215853, 'x5': 0.343164, 'x6': 0.594287} using model BoTorch.
[INFO 07-23 19:38:13] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-2.449358, 0.1), 'l2norm': (1.018855, 0.1)}.
[INFO 07-23 19:38:17] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.19953, 'x2': 0.336787, 'x3': 0.566451, 'x4': 0.235734, 'x5': 0.335288, 'x6': 0.579376} using model BoTorch.
[INFO 07-23 19:38:17] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-2.809778, 0.1), 'l2norm': (0.925198, 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 07-23 19:38:17] 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 07-23 19:38:50] 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: 58.15 seconds.