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 03-10 16:27:40] 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 03-10 16:27:41] 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 03-10 16:27:41] 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 03-10 16:27:41] 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 03-10 16:27:41] 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 03-10 16:27:41] 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 03-10 16:27:41] 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 03-10 16:27:41] 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 03-10 16:27:41] 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 03-10 16:27:41] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters. [INFO 03-10 16:27:41] 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 03-10 16:27:41] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.352827, 'x2': 0.093344, 'x3': 0.789495, 'x4': 0.302249, 'x5': 0.567184, 'x6': 0.042165}. [INFO 03-10 16:27:41] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.074469, 0.1), 'l2norm': (0.965222, 0.1)}. [INFO 03-10 16:27:41] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.803432, 'x2': 0.587732, 'x3': 0.835058, 'x4': 0.148863, 'x5': 0.408635, 'x6': 0.31984}. [INFO 03-10 16:27:41] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (0.003362, 0.1), 'l2norm': (1.476881, 0.1)}. [INFO 03-10 16:27:41] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.118921, 'x2': 0.111163, 'x3': 0.821954, 'x4': 0.093103, 'x5': 0.606185, 'x6': 0.592445}. [INFO 03-10 16:27:41] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-0.484018, 0.1), 'l2norm': (1.19009, 0.1)}. [INFO 03-10 16:27:41] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.203261, 'x2': 0.94172, 'x3': 0.400442, 'x4': 0.935692, 'x5': 0.90146, 'x6': 0.114014}. [INFO 03-10 16:27:41] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.17052, 0.1), 'l2norm': (1.638921, 0.1)}. [INFO 03-10 16:27:41] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.214219, 'x2': 0.040968, 'x3': 0.469627, 'x4': 0.991675, 'x5': 0.168038, 'x6': 0.70334}. [INFO 03-10 16:27:41] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (-0.062464, 0.1), 'l2norm': (1.26809, 0.1)}. [INFO 03-10 16:27:41] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.774712, 'x2': 0.545728, 'x3': 0.047743, 'x4': 0.224079, 'x5': 0.023244, 'x6': 0.695692}. [INFO 03-10 16:27:41] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (-0.287718, 0.1), 'l2norm': (1.073833, 0.1)}. [INFO 03-10 16:27:41] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.044977, 'x2': 0.750382, 'x3': 0.167445, 'x4': 0.223084, 'x5': 0.251982, 'x6': 0.419585}. [INFO 03-10 16:27:41] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.592992, 0.1), 'l2norm': (1.076144, 0.1)}. [INFO 03-10 16:27:41] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.771967, 'x2': 0.34697, 'x3': 0.073377, 'x4': 0.872811, 'x5': 0.747369, 'x6': 0.113389}. [INFO 03-10 16:27:41] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (0.093333, 0.1), 'l2norm': (1.476428, 0.1)}. [INFO 03-10 16:27:41] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.289988, 'x2': 0.219711, 'x3': 0.545033, 'x4': 0.858802, 'x5': 0.057485, 'x6': 0.464024}. [INFO 03-10 16:27:41] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (0.034017, 0.1), 'l2norm': (1.386916, 0.1)}. [INFO 03-10 16:27:41] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.732902, 'x2': 0.460136, 'x3': 0.881933, 'x4': 0.170002, 'x5': 0.879989, 'x6': 0.359563}. [INFO 03-10 16:27:41] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (0.075196, 0.1), 'l2norm': (1.688414, 0.1)}. [INFO 03-10 16:27:41] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.524376, 'x2': 0.419511, 'x3': 0.584469, 'x4': 0.636029, 'x5': 0.9243, 'x6': 0.507133}. [INFO 03-10 16:27:41] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-0.018645, 0.1), 'l2norm': (1.615719, 0.1)}. [INFO 03-10 16:27:41] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.095478, 'x2': 0.457029, 'x3': 0.375215, 'x4': 0.505558, 'x5': 0.494655, 'x6': 0.97058}. [INFO 03-10 16:27:41] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.414982, 0.1), 'l2norm': (1.376974, 0.1)}. [INFO 03-10 16:27:53] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.037015, 'x2': 0.683719, 'x3': 0.221445, 'x4': 0.216437, 'x5': 0.292616, 'x6': 0.569796}. [INFO 03-10 16:27:53] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-0.79344, 0.1), 'l2norm': (0.996906, 0.1)}. [INFO 03-10 16:28:14] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.0079, 'x2': 0.595758, 'x3': 0.310029, 'x4': 0.15409, 'x5': 0.343866, 'x6': 0.63448}. [INFO 03-10 16:28:14] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-1.113635, 0.1), 'l2norm': (1.002588, 0.1)}. [INFO 03-10 16:28:35] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.0, 'x2': 0.509852, 'x3': 0.371336, 'x4': 0.074012, 'x5': 0.351524, 'x6': 0.65107}. [INFO 03-10 16:28:35] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-1.155341, 0.1), 'l2norm': (0.927094, 0.1)}. [INFO 03-10 16:28:40] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.0, 'x2': 0.606613, 'x3': 0.378398, 'x4': 0.066405, 'x5': 0.434774, 'x6': 0.717555}. [INFO 03-10 16:28:40] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-0.610078, 0.1), 'l2norm': (1.128758, 0.1)}. [INFO 03-10 16:28:43] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.0, 'x2': 0.464997, 'x3': 0.351049, 'x4': 0.133769, 'x5': 0.314758, 'x6': 0.62072}. [INFO 03-10 16:28:43] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-1.49733, 0.1), 'l2norm': (0.965486, 0.1)}. [INFO 03-10 16:28:45] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.0, 'x2': 0.414049, 'x3': 0.338492, 'x4': 0.163752, 'x5': 0.271699, 'x6': 0.589462}. [INFO 03-10 16:28:45] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-1.86004, 0.1), 'l2norm': (0.623424, 0.1)}. [INFO 03-10 16:28:46] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.0, 'x2': 0.354845, 'x3': 0.322451, 'x4': 0.18523, 'x5': 0.219635, 'x6': 0.559953}. [INFO 03-10 16:28:46] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-1.829957, 0.1), 'l2norm': (0.795035, 0.1)}. [INFO 03-10 16:28:47] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.0, 'x2': 0.381759, 'x3': 0.409561, 'x4': 0.20806, 'x5': 0.23497, 'x6': 0.529831}. [INFO 03-10 16:28:47] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-1.760434, 0.1), 'l2norm': (1.020363, 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 03-10 16:28:47] 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: 1 minutes, 27.87 seconds.