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 09-28 16:20:51] 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 09-28 16:20:51] 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 09-28 16:20:51] 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 09-28 16:20:51] 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 09-28 16:20:51] 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 09-28 16:20:51] 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 09-28 16:20:51] 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 09-28 16:20:51] 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 09-28 16:20:51] 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 09-28 16:20:51] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters. [INFO 09-28 16:20:51] 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))
/home/runner/work/Ax/Ax/ax/core/observation.py:274: FutureWarning: In a future version of pandas, a length 1 tuple will be returned when iterating over a groupby with a grouper equal to a list of length 1. Don't supply a list with a single grouper to avoid this warning. [INFO 09-28 16:20:51] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.454946, 'x2': 0.603001, 'x3': 0.063674, 'x4': 0.424514, 'x5': 0.130818, 'x6': 0.075103}. [INFO 09-28 16:20:51] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-1.177602, 0.1), 'l2norm': (0.86887, 0.1)}. [INFO 09-28 16:20:51] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.721239, 'x2': 0.428901, 'x3': 0.875858, 'x4': 0.549744, 'x5': 0.212166, 'x6': 0.852913}. [INFO 09-28 16:20:51] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-0.915197, 0.1), 'l2norm': (1.691416, 0.1)}. [INFO 09-28 16:20:51] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.623067, 'x2': 0.971697, 'x3': 0.616247, 'x4': 0.757515, 'x5': 0.558287, 'x6': 0.175032}. [INFO 09-28 16:20:51] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-0.724632, 0.1), 'l2norm': (1.539999, 0.1)}. [INFO 09-28 16:20:51] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.264243, 'x2': 0.168695, 'x3': 0.538047, 'x4': 0.003598, 'x5': 0.344249, 'x6': 0.217604}. [INFO 09-28 16:20:51] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.432082, 0.1), 'l2norm': (0.782227, 0.1)}. [INFO 09-28 16:20:51] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.187313, 'x2': 0.485827, 'x3': 0.430016, 'x4': 0.711261, 'x5': 0.733871, 'x6': 0.243443}. [INFO 09-28 16:20:51] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (-0.257618, 0.1), 'l2norm': (1.313912, 0.1)}. [INFO 09-28 16:20:51] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.390678, 'x2': 0.960958, 'x3': 0.120741, 'x4': 0.745606, 'x5': 0.439906, 'x6': 0.780262}. [INFO 09-28 16:20:51] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (-0.083186, 0.1), 'l2norm': (1.468524, 0.1)}. [INFO 09-28 16:20:51] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.735676, 'x2': 0.449008, 'x3': 0.309312, 'x4': 0.992304, 'x5': 0.667596, 'x6': 0.751031}. [INFO 09-28 16:20:51] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.063632, 0.1), 'l2norm': (1.601553, 0.1)}. [INFO 09-28 16:20:51] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.17627, 'x2': 0.259648, 'x3': 0.39841, 'x4': 0.104464, 'x5': 0.688635, 'x6': 0.714765}. [INFO 09-28 16:20:51] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-0.670639, 0.1), 'l2norm': (0.975103, 0.1)}. [INFO 09-28 16:20:51] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.548878, 'x2': 0.988486, 'x3': 0.942464, 'x4': 0.311807, 'x5': 0.244395, 'x6': 0.531932}. [INFO 09-28 16:20:51] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.024953, 0.1), 'l2norm': (1.511371, 0.1)}. [INFO 09-28 16:20:51] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.046721, 'x2': 0.284299, 'x3': 0.069976, 'x4': 0.68202, 'x5': 0.442875, 'x6': 0.67003}. [INFO 09-28 16:20:51] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (-0.484645, 0.1), 'l2norm': (1.112495, 0.1)}. [INFO 09-28 16:20:51] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.799768, 'x2': 0.165811, 'x3': 0.726288, 'x4': 0.835844, 'x5': 0.294569, 'x6': 0.051389}. [INFO 09-28 16:20:51] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (0.046002, 0.1), 'l2norm': (1.371904, 0.1)}. [INFO 09-28 16:20:51] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.612971, 'x2': 0.680151, 'x3': 0.920277, 'x4': 0.419911, 'x5': 0.030181, 'x6': 0.318475}. [INFO 09-28 16:20:51] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.22676, 0.1), 'l2norm': (1.147252, 0.1)}. [INFO 09-28 16:21:02] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.453214, 'x2': 0.662361, 'x3': 0.0, 'x4': 0.35427, 'x5': 0.098175, 'x6': 0.0}. [INFO 09-28 16:21:02] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-1.149865, 0.1), 'l2norm': (0.959162, 0.1)}. [INFO 09-28 16:21:15] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.410542, 'x2': 0.53153, 'x3': 0.0, 'x4': 0.346654, 'x5': 0.022487, 'x6': 0.080508}. [INFO 09-28 16:21:15] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-0.57885, 0.1), 'l2norm': (0.724437, 0.1)}. [INFO 09-28 16:21:37] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.47768, 'x2': 0.687741, 'x3': 0.054868, 'x4': 0.42443, 'x5': 0.179838, 'x6': 0.017929}. [INFO 09-28 16:21:37] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-1.524291, 0.1), 'l2norm': (0.997778, 0.1)}. [INFO 09-28 16:21:57] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.498703, 'x2': 0.745856, 'x3': 0.095896, 'x4': 0.429057, 'x5': 0.232983, 'x6': 0.0}. [INFO 09-28 16:21:57] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-1.896443, 0.1), 'l2norm': (1.052623, 0.1)}. [INFO 09-28 16:22:25] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.498377, 'x2': 0.809206, 'x3': 0.136792, 'x4': 0.437591, 'x5': 0.271773, 'x6': 0.0}. [INFO 09-28 16:22:25] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-2.094882, 0.1), 'l2norm': (1.134289, 0.1)}. [INFO 09-28 16:22:51] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.564269, 'x2': 0.810752, 'x3': 0.162578, 'x4': 0.385179, 'x5': 0.303267, 'x6': 0.0}. [INFO 09-28 16:22:51] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-1.357871, 0.1), 'l2norm': (1.029446, 0.1)}. [INFO 09-28 16:23:07] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.476256, 'x2': 0.848307, 'x3': 0.142714, 'x4': 0.478473, 'x5': 0.303303, 'x6': 0.0}. [INFO 09-28 16:23:07] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-2.556004, 0.1), 'l2norm': (1.048117, 0.1)}. [INFO 09-28 16:23:09] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.447768, 'x2': 0.8895, 'x3': 0.13735, 'x4': 0.519652, 'x5': 0.320775, 'x6': 0.0}. [INFO 09-28 16:23:09] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-2.8551, 0.1), 'l2norm': (1.061184, 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 09-28 16:23:09] 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: 2 minutes, 37.92 seconds.