This tutorial illustrates the core visualization utilities available in Ax.
import numpy as np
from ax.service.ax_client import AxClient
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.utils.measurement.synthetic_functions import hartmann6
from ax.utils.notebook.plotting import render, init_notebook_plotting
init_notebook_plotting()
[INFO 04-26 20:16:12] ax.utils.notebook.plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.
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
],
objective_name="hartmann6",
minimize=True,
outcome_constraints=["l2norm <= 1.25"]
)
[INFO 04-26 20:16: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 6 decimal points. [INFO 04-26 20:16:12] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x1. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict. [INFO 04-26 20:16:12] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x2. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict. [INFO 04-26 20:16:12] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x3. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict. [INFO 04-26 20:16:12] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x4. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict. [INFO 04-26 20:16:12] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x5. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict. [INFO 04-26 20:16:12] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x6. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict. [INFO 04-26 20:16:12] 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 04-26 20:16:12] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters. [INFO 04-26 20:16:12] ax.modelbridge.dispatch_utils: Using Bayesian Optimization generation strategy: GenerationStrategy(name='Sobol+GPEI', steps=[Sobol for 12 trials, GPEI 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 04-26 20:16:12] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.647986, 'x2': 0.940565, 'x3': 0.044812, 'x4': 0.575433, 'x5': 0.637394, 'x6': 0.412333}. [INFO 04-26 20:16:12] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.04526, 0.1), 'l2norm': (1.381985, 0.1)}. [INFO 04-26 20:16:12] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.567233, 'x2': 0.821178, 'x3': 0.977733, 'x4': 0.199389, 'x5': 0.180126, 'x6': 0.868955}. [INFO 04-26 20:16:12] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-0.238117, 0.1), 'l2norm': (1.656251, 0.1)}. [INFO 04-26 20:16:12] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.030914, 'x2': 0.442223, 'x3': 0.032014, 'x4': 0.751952, 'x5': 0.529942, 'x6': 0.162994}. [INFO 04-26 20:16:12] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-0.172203, 0.1), 'l2norm': (0.921497, 0.1)}. [INFO 04-26 20:16:12] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.893565, 'x2': 0.879475, 'x3': 0.362525, 'x4': 0.865657, 'x5': 0.690319, 'x6': 0.10035}. [INFO 04-26 20:16:12] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.173078, 0.1), 'l2norm': (1.674019, 0.1)}. [INFO 04-26 20:16:12] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.725341, 'x2': 0.354519, 'x3': 0.346764, 'x4': 0.049017, 'x5': 0.922025, 'x6': 0.225903}. [INFO 04-26 20:16:12] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (0.191891, 0.1), 'l2norm': (1.229723, 0.1)}. [INFO 04-26 20:16:12] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.635386, 'x2': 0.479884, 'x3': 0.430837, 'x4': 0.817347, 'x5': 0.316434, 'x6': 0.137054}. [INFO 04-26 20:16:12] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (-0.409751, 0.1), 'l2norm': (1.198406, 0.1)}. [INFO 04-26 20:16:12] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.360357, 'x2': 0.127097, 'x3': 0.981938, 'x4': 0.475173, 'x5': 0.716454, 'x6': 0.980746}. [INFO 04-26 20:16:12] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.023049, 0.1), 'l2norm': (1.792053, 0.1)}. [INFO 04-26 20:16:12] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.603229, 'x2': 0.63944, 'x3': 0.192713, 'x4': 0.065143, 'x5': 0.470419, 'x6': 0.061607}. [INFO 04-26 20:16:12] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-0.180625, 0.1), 'l2norm': (1.117865, 0.1)}. [INFO 04-26 20:16:12] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.809156, 'x2': 0.189099, 'x3': 0.794207, 'x4': 0.773438, 'x5': 0.378103, 'x6': 0.268962}. [INFO 04-26 20:16:12] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.074947, 0.1), 'l2norm': (1.325708, 0.1)}. [INFO 04-26 20:16:12] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.225076, 'x2': 0.0129, 'x3': 0.802195, 'x4': 0.345076, 'x5': 0.688158, 'x6': 0.461337}. [INFO 04-26 20:16:12] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (-0.205796, 0.1), 'l2norm': (1.121066, 0.1)}. [INFO 04-26 20:16:12] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.143256, 'x2': 0.67861, 'x3': 0.774216, 'x4': 0.926749, 'x5': 0.741571, 'x6': 0.727287}. [INFO 04-26 20:16:12] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-0.00976, 0.1), 'l2norm': (1.844292, 0.1)}. [INFO 04-26 20:16:12] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.07977, 'x2': 0.49046, 'x3': 0.474706, 'x4': 0.731792, 'x5': 0.419593, 'x6': 0.904151}. [INFO 04-26 20:16:12] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.255761, 0.1), 'l2norm': (1.39581, 0.1)}. [INFO 04-26 20:16:31] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.514753, 'x2': 0.470081, 'x3': 0.353008, 'x4': 0.774696, 'x5': 0.300075, 'x6': 0.133009}. [INFO 04-26 20:16:31] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-0.24639, 0.1), 'l2norm': (1.073419, 0.1)}. [INFO 04-26 20:16:45] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.648421, 'x2': 0.473822, 'x3': 0.368169, 'x4': 0.71098, 'x5': 0.216919, 'x6': 0.130905}. [INFO 04-26 20:16:45] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-0.3002, 0.1), 'l2norm': (1.203156, 0.1)}. [INFO 04-26 20:17:04] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.59857, 'x2': 0.454225, 'x3': 0.383377, 'x4': 0.957917, 'x5': 0.250776, 'x6': 0.117958}. [INFO 04-26 20:17:04] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-0.160542, 0.1), 'l2norm': (1.236019, 0.1)}. [INFO 04-26 20:17:26] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.537644, 'x2': 0.532768, 'x3': 0.461756, 'x4': 0.637048, 'x5': 0.354229, 'x6': 0.096646}. [INFO 04-26 20:17:26] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-0.608008, 0.1), 'l2norm': (1.122255, 0.1)}. [INFO 04-26 20:17:59] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.485691, 'x2': 0.50462, 'x3': 0.515797, 'x4': 0.592812, 'x5': 0.356857, 'x6': 0.131914}. [INFO 04-26 20:17:59] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-0.733421, 0.1), 'l2norm': (1.158977, 0.1)}. [INFO 04-26 20:18:24] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.467467, 'x2': 0.573445, 'x3': 0.534, 'x4': 0.534673, 'x5': 0.337681, 'x6': 0.095688}. [INFO 04-26 20:18:24] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-1.230438, 0.1), 'l2norm': (1.311675, 0.1)}. [INFO 04-26 20:19:21] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.423334, 'x2': 0.588478, 'x3': 0.520482, 'x4': 0.490528, 'x5': 0.320158, 'x6': 0.19463}. [INFO 04-26 20:19:21] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-1.21523, 0.1), 'l2norm': (1.161382, 0.1)}. [INFO 04-26 20:20:01] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.397725, 'x2': 0.593562, 'x3': 0.536781, 'x4': 0.468968, 'x5': 0.400463, 'x6': 0.126085}. [INFO 04-26 20:20:01] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-1.383516, 0.1), 'l2norm': (1.121911, 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 04-26 20:20:01] 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))
Total runtime of script: 4 minutes, 8.08 seconds.