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-25 21:27:06] 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-25 21:27:06] 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-25 21:27:06] 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-25 21:27:06] 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-25 21:27:06] 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-25 21:27:06] 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-25 21:27:06] 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-25 21:27:06] 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-25 21:27:06] 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-25 21:27:06] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters. [INFO 04-25 21:27:06] 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-25 21:27:06] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.156353, 'x2': 0.828955, 'x3': 0.18221, 'x4': 0.993831, 'x5': 0.278476, 'x6': 0.628853}. [INFO 04-25 21:27:06] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.00661, 0.1), 'l2norm': (1.519872, 0.1)}. [INFO 04-25 21:27:06] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.638525, 'x2': 0.295647, 'x3': 0.056867, 'x4': 0.174938, 'x5': 0.181975, 'x6': 0.433481}. [INFO 04-25 21:27:06] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-0.603912, 0.1), 'l2norm': (0.836981, 0.1)}. [INFO 04-25 21:27:06] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.006821, 'x2': 0.841453, 'x3': 0.119364, 'x4': 0.726945, 'x5': 0.762638, 'x6': 0.210246}. [INFO 04-25 21:27:06] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-0.058331, 0.1), 'l2norm': (1.35503, 0.1)}. [INFO 04-25 21:27:06] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.19718, 'x2': 0.641646, 'x3': 0.99326, 'x4': 0.206919, 'x5': 0.081034, 'x6': 0.066097}. [INFO 04-25 21:27:06] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.285483, 0.1), 'l2norm': (1.142436, 0.1)}. [INFO 04-25 21:27:06] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.454058, 'x2': 0.160317, 'x3': 0.119512, 'x4': 0.299947, 'x5': 0.989933, 'x6': 0.076605}. [INFO 04-25 21:27:06] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (-0.033935, 0.1), 'l2norm': (1.10637, 0.1)}. [INFO 04-25 21:27:06] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.604522, 'x2': 0.925746, 'x3': 0.607682, 'x4': 0.265181, 'x5': 0.568391, 'x6': 0.474896}. [INFO 04-25 21:27:06] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (-0.019129, 0.1), 'l2norm': (1.650064, 0.1)}. [INFO 04-25 21:27:06] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.635463, 'x2': 0.26446, 'x3': 0.618036, 'x4': 0.907142, 'x5': 0.883884, 'x6': 0.555087}. [INFO 04-25 21:27:06] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (0.070479, 0.1), 'l2norm': (1.651056, 0.1)}. [INFO 04-25 21:27:06] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.80227, 'x2': 0.59793, 'x3': 0.773525, 'x4': 0.867527, 'x5': 0.914255, 'x6': 0.357337}. [INFO 04-25 21:27:06] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-0.019961, 0.1), 'l2norm': (2.048878, 0.1)}. [INFO 04-25 21:27:06] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.885401, 'x2': 0.979298, 'x3': 0.52418, 'x4': 0.628868, 'x5': 0.398996, 'x6': 0.143867}. [INFO 04-25 21:27:06] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (0.115659, 0.1), 'l2norm': (1.613013, 0.1)}. [INFO 04-25 21:27:06] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.652378, 'x2': 0.878267, 'x3': 0.698092, 'x4': 0.167317, 'x5': 0.394397, 'x6': 0.190446}. [INFO 04-25 21:27:06] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (-0.039665, 0.1), 'l2norm': (1.253833, 0.1)}. [INFO 04-25 21:27:06] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.925983, 'x2': 0.915254, 'x3': 0.332073, 'x4': 0.883105, 'x5': 0.483516, 'x6': 0.340959}. [INFO 04-25 21:27:07] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-0.004602, 0.1), 'l2norm': (1.678071, 0.1)}. [INFO 04-25 21:27:07] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.171178, 'x2': 0.467583, 'x3': 0.13919, 'x4': 0.224523, 'x5': 0.955353, 'x6': 0.641796}. [INFO 04-25 21:27:07] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.046218, 0.1), 'l2norm': (1.344774, 0.1)}. [INFO 04-25 21:27:20] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.628777, 'x2': 0.232364, 'x3': 0.032492, 'x4': 0.115041, 'x5': 0.081049, 'x6': 0.431374}. [INFO 04-25 21:27:20] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-0.097935, 0.1), 'l2norm': (0.831384, 0.1)}. [INFO 04-25 21:27:29] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.639537, 'x2': 0.316663, 'x3': 0.068793, 'x4': 0.194442, 'x5': 0.226739, 'x6': 0.433794}. [INFO 04-25 21:27:29] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-0.759589, 0.1), 'l2norm': (0.775916, 0.1)}. [INFO 04-25 21:27:37] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.665487, 'x2': 0.293105, 'x3': 0.000115, 'x4': 0.202545, 'x5': 0.258844, 'x6': 0.455193}. [INFO 04-25 21:27:37] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-1.023126, 0.1), 'l2norm': (1.026266, 0.1)}. [INFO 04-25 21:27:51] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.678476, 'x2': 0.283359, 'x3': 0.0, 'x4': 0.211517, 'x5': 0.302936, 'x6': 0.4686}. [INFO 04-25 21:27:51] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-1.100207, 0.1), 'l2norm': (0.827118, 0.1)}. [INFO 04-25 21:27:55] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.749384, 'x2': 0.324648, 'x3': 0.0, 'x4': 0.277968, 'x5': 0.307693, 'x6': 0.489239}. [INFO 04-25 21:27:55] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-0.520247, 0.1), 'l2norm': (1.035583, 0.1)}. [INFO 04-25 21:28:02] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.650144, 'x2': 0.273984, 'x3': 0.0, 'x4': 0.198529, 'x5': 0.326291, 'x6': 0.463495}. [INFO 04-25 21:28:02] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-0.90135, 0.1), 'l2norm': (0.997695, 0.1)}. [INFO 04-25 21:28:28] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.674411, 'x2': 0.343923, 'x3': 0.0, 'x4': 0.147457, 'x5': 0.299825, 'x6': 0.420233}. [INFO 04-25 21:28:28] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-0.556067, 0.1), 'l2norm': (0.951424, 0.1)}. [INFO 04-25 21:28:31] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.633521, 'x2': 0.273156, 'x3': 0.0, 'x4': 0.240819, 'x5': 0.28017, 'x6': 0.441031}. [INFO 04-25 21:28:31] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-0.882174, 0.1), 'l2norm': (0.865362, 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-25 21:28:31] 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, 43.13 seconds.