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-15 17:21:35] 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-15 17:21:36] 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-15 17:21:36] 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-15 17:21:36] 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-15 17:21:36] 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-15 17:21:36] 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-15 17:21:36] 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-15 17:21:36] 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-15 17:21:36] 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-15 17:21:36] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters. [INFO 09-15 17:21:36] 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 09-15 17:21:36] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.412969, 'x2': 0.328518, 'x3': 0.765503, 'x4': 0.090356, 'x5': 0.084397, 'x6': 0.229964}. [INFO 09-15 17:21:36] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.254166, 0.1), 'l2norm': (1.062691, 0.1)}. [INFO 09-15 17:21:36] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.400361, 'x2': 0.694227, 'x3': 0.263258, 'x4': 0.456069, 'x5': 0.694066, 'x6': 0.551439}. [INFO 09-15 17:21:36] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-0.216456, 0.1), 'l2norm': (1.377596, 0.1)}. [INFO 09-15 17:21:36] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.527782, 'x2': 0.281631, 'x3': 0.459004, 'x4': 0.85833, 'x5': 0.382644, 'x6': 0.843758}. [INFO 09-15 17:21:36] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-0.093704, 0.1), 'l2norm': (1.565636, 0.1)}. [INFO 09-15 17:21:36] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.964993, 'x2': 0.606123, 'x3': 0.724707, 'x4': 0.674225, 'x5': 0.850825, 'x6': 0.760082}. [INFO 09-15 17:21:36] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.032231, 0.1), 'l2norm': (1.856033, 0.1)}. [INFO 09-15 17:21:36] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.855739, 'x2': 0.521387, 'x3': 0.202258, 'x4': 0.114507, 'x5': 0.392379, 'x6': 0.866037}. [INFO 09-15 17:21:36] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (-0.327491, 0.1), 'l2norm': (1.269839, 0.1)}. [INFO 09-15 17:21:36] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.23947, 'x2': 0.446653, 'x3': 0.181172, 'x4': 0.457178, 'x5': 0.601999, 'x6': 0.869284}. [INFO 09-15 17:21:36] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (-0.292095, 0.1), 'l2norm': (1.205225, 0.1)}. [INFO 09-15 17:21:36] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.852697, 'x2': 0.887604, 'x3': 0.303298, 'x4': 0.77791, 'x5': 0.70816, 'x6': 0.200014}. [INFO 09-15 17:21:36] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (0.003432, 0.1), 'l2norm': (1.520647, 0.1)}. [INFO 09-15 17:21:36] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.137982, 'x2': 0.164091, 'x3': 0.994962, 'x4': 0.183641, 'x5': 0.758688, 'x6': 0.336802}. [INFO 09-15 17:21:36] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-0.13732, 0.1), 'l2norm': (1.360396, 0.1)}. [INFO 09-15 17:21:36] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.998569, 'x2': 0.069484, 'x3': 0.86861, 'x4': 0.020381, 'x5': 0.419802, 'x6': 0.195323}. [INFO 09-15 17:21:36] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.086671, 0.1), 'l2norm': (1.346619, 0.1)}. [INFO 09-15 17:21:36] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.873837, 'x2': 0.508701, 'x3': 0.475132, 'x4': 0.096994, 'x5': 0.492009, 'x6': 0.077229}. [INFO 09-15 17:21:36] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (0.099158, 0.1), 'l2norm': (1.226503, 0.1)}. [INFO 09-15 17:21:36] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.425132, 'x2': 0.459248, 'x3': 0.799464, 'x4': 0.641969, 'x5': 0.985835, 'x6': 0.552843}. [INFO 09-15 17:21:36] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (0.001006, 0.1), 'l2norm': (1.707721, 0.1)}. [INFO 09-15 17:21:36] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.662244, 'x2': 0.670021, 'x3': 0.000235, 'x4': 0.809495, 'x5': 0.215972, 'x6': 0.31156}. [INFO 09-15 17:21:36] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.01377, 0.1), 'l2norm': (1.357761, 0.1)}. [INFO 09-15 17:21:48] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.332177, 'x2': 0.432272, 'x3': 0.145211, 'x4': 0.289302, 'x5': 0.50205, 'x6': 0.86715}. [INFO 09-15 17:21:48] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-0.933392, 0.1), 'l2norm': (1.213185, 0.1)}. [INFO 09-15 17:22:44] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.337652, 'x2': 0.442638, 'x3': 0.213407, 'x4': 0.258784, 'x5': 0.410148, 'x6': 0.808832}. [INFO 09-15 17:22:44] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-1.360083, 0.1), 'l2norm': (1.081447, 0.1)}. [INFO 09-15 17:23:13] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.317832, 'x2': 0.429257, 'x3': 0.210136, 'x4': 0.19964, 'x5': 0.374641, 'x6': 0.772252}. [INFO 09-15 17:23:13] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-1.580677, 0.1), 'l2norm': (1.173073, 0.1)}. [INFO 09-15 17:23:15] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.319015, 'x2': 0.438675, 'x3': 0.21249, 'x4': 0.18008, 'x5': 0.315566, 'x6': 0.792599}. [INFO 09-15 17:23:15] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-1.700773, 0.1), 'l2norm': (0.988241, 0.1)}. [INFO 09-15 17:23:23] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.279883, 'x2': 0.416912, 'x3': 0.257569, 'x4': 0.132701, 'x5': 0.30657, 'x6': 0.773901}. [INFO 09-15 17:23:23] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-1.805066, 0.1), 'l2norm': (1.05792, 0.1)}. [INFO 09-15 17:23:25] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.288451, 'x2': 0.351195, 'x3': 0.202783, 'x4': 0.130344, 'x5': 0.24475, 'x6': 0.782968}. [INFO 09-15 17:23:25] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-1.7629, 0.1), 'l2norm': (0.930239, 0.1)}. [INFO 09-15 17:23:27] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.255876, 'x2': 0.490113, 'x3': 0.202269, 'x4': 0.103008, 'x5': 0.237842, 'x6': 0.777957}. [INFO 09-15 17:23:27] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-1.147839, 0.1), 'l2norm': (1.084243, 0.1)}. [INFO 09-15 17:23:29] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.326781, 'x2': 0.369115, 'x3': 0.27679, 'x4': 0.151255, 'x5': 0.304935, 'x6': 0.792773}. [INFO 09-15 17:23:29] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-1.681368, 0.1), 'l2norm': (0.941332, 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-15 17:23:29] 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, 21.79 seconds.