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 11-10 20:32:10] 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 11-10 20:32:10] 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 11-10 20:32:10] 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 11-10 20:32:10] 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 11-10 20:32:10] 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 11-10 20:32:10] 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 11-10 20:32:10] 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 11-10 20:32:10] 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 11-10 20:32:10] 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 11-10 20:32:10] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters. [INFO 11-10 20:32:10] 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 11-10 20:32:10] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.941206, 'x2': 0.128345, 'x3': 0.193663, 'x4': 0.59304, 'x5': 0.67774, 'x6': 0.632615}. [INFO 11-10 20:32:10] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.013391, 0.1), 'l2norm': (1.666149, 0.1)}. [INFO 11-10 20:32:10] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.350923, 'x2': 0.817723, 'x3': 0.212011, 'x4': 0.210169, 'x5': 0.206005, 'x6': 0.954653}. [INFO 11-10 20:32:10] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-0.295311, 0.1), 'l2norm': (1.445695, 0.1)}. [INFO 11-10 20:32:10] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.109802, 'x2': 0.062789, 'x3': 0.490375, 'x4': 0.975718, 'x5': 0.572008, 'x6': 0.285686}. [INFO 11-10 20:32:10] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-0.17209, 0.1), 'l2norm': (1.164104, 0.1)}. [INFO 11-10 20:32:10] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.231465, 'x2': 0.964593, 'x3': 0.155377, 'x4': 0.409109, 'x5': 0.756466, 'x6': 0.710501}. [INFO 11-10 20:32:10] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (0.168145, 0.1), 'l2norm': (1.646876, 0.1)}. [INFO 11-10 20:32:10] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.00778, 'x2': 0.237625, 'x3': 0.791061, 'x4': 0.711802, 'x5': 0.241862, 'x6': 0.661306}. [INFO 11-10 20:32:10] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (-0.558998, 0.1), 'l2norm': (1.260731, 0.1)}. [INFO 11-10 20:32:10] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.7766, 'x2': 0.34743, 'x3': 0.823272, 'x4': 0.807713, 'x5': 0.912812, 'x6': 0.628868}. [INFO 11-10 20:32:10] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (0.092051, 0.1), 'l2norm': (1.774278, 0.1)}. [INFO 11-10 20:32:10] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.610301, 'x2': 0.818059, 'x3': 0.683816, 'x4': 0.793503, 'x5': 0.971427, 'x6': 0.025934}. [INFO 11-10 20:32:10] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.900724, 0.1), 'l2norm': (1.598032, 0.1)}. [INFO 11-10 20:32:10] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.612836, 'x2': 0.173494, 'x3': 0.39489, 'x4': 0.838601, 'x5': 0.485488, 'x6': 0.826801}. [INFO 11-10 20:32:10] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (0.166554, 0.1), 'l2norm': (1.424632, 0.1)}. [INFO 11-10 20:32:10] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.616064, 'x2': 0.886116, 'x3': 0.251193, 'x4': 0.667437, 'x5': 0.838638, 'x6': 0.668467}. [INFO 11-10 20:32:10] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.066591, 0.1), 'l2norm': (1.673057, 0.1)}. [INFO 11-10 20:32:10] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.419323, 'x2': 0.084307, 'x3': 0.412628, 'x4': 0.870432, 'x5': 0.671433, 'x6': 0.937125}. [INFO 11-10 20:32:10] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (-0.00288, 0.1), 'l2norm': (1.549686, 0.1)}. [INFO 11-10 20:32:10] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.321102, 'x2': 0.720067, 'x3': 0.578888, 'x4': 0.226596, 'x5': 0.488705, 'x6': 0.936341}. [INFO 11-10 20:32:10] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-0.336436, 0.1), 'l2norm': (1.371364, 0.1)}. [INFO 11-10 20:32:10] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.865786, 'x2': 0.308924, 'x3': 0.419366, 'x4': 0.472768, 'x5': 0.062104, 'x6': 0.08089}. [INFO 11-10 20:32:10] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.038426, 0.1), 'l2norm': (1.095405, 0.1)}. [INFO 11-10 20:32:26] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.0, 'x2': 0.195257, 'x3': 0.743973, 'x4': 0.765187, 'x5': 0.316779, 'x6': 0.508665}. [INFO 11-10 20:32:26] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-0.225562, 0.1), 'l2norm': (1.271639, 0.1)}. [INFO 11-10 20:32:40] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.0, 'x2': 0.269234, 'x3': 0.790091, 'x4': 0.667303, 'x5': 0.06273, 'x6': 0.706032}. [INFO 11-10 20:32:40] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-0.508339, 0.1), 'l2norm': (1.427037, 0.1)}. [INFO 11-10 20:32:54] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.0, 'x2': 0.217392, 'x3': 0.727826, 'x4': 0.625424, 'x5': 0.429193, 'x6': 0.709425}. [INFO 11-10 20:32:54] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-0.650362, 0.1), 'l2norm': (1.183348, 0.1)}. [INFO 11-10 20:33:09] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.0, 'x2': 0.249143, 'x3': 0.860397, 'x4': 0.61219, 'x5': 0.37464, 'x6': 0.76641}. [INFO 11-10 20:33:09] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-0.844568, 0.1), 'l2norm': (1.481249, 0.1)}. [INFO 11-10 20:33:37] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.0, 'x2': 0.240716, 'x3': 0.654132, 'x4': 0.593032, 'x5': 0.339848, 'x6': 0.790009}. [INFO 11-10 20:33:37] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-1.005465, 0.1), 'l2norm': (1.290837, 0.1)}. [INFO 11-10 20:34:03] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.0, 'x2': 0.224115, 'x3': 0.534221, 'x4': 0.620932, 'x5': 0.363411, 'x6': 0.791665}. [INFO 11-10 20:34:03] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-0.774214, 0.1), 'l2norm': (1.35333, 0.1)}. [INFO 11-10 20:34:27] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.0, 'x2': 0.168341, 'x3': 0.611198, 'x4': 0.470563, 'x5': 0.361918, 'x6': 0.739598}. [INFO 11-10 20:34:27] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-1.672589, 0.1), 'l2norm': (1.278163, 0.1)}. [INFO 11-10 20:35:04] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.02098, 'x2': 0.181006, 'x3': 0.620755, 'x4': 0.461433, 'x5': 0.349005, 'x6': 0.591645}. [INFO 11-10 20:35:04] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-2.021935, 0.1), 'l2norm': (1.056877, 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 11-10 20:35:05] 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: 3 minutes, 17.05 seconds.