This tutorial introduces the Sparsity Exploration Bayesian Optimization (SEBO) method and demonstrates how to utilize it using the Ax API. SEBO is designed to enhance Bayesian Optimization (BO) by taking the interpretability and simplicity of configurations into consideration. In essence, SEBO incorporates sparsity, modeled as the $L_0$ norm, as an additional objective in BO. By employing multi-objective optimization techniques such as Expected Hyper-Volume Improvement, SEBO enables the joint optimization of objectives while simultaneously incorporating feature-level sparsity. This allows users to efficiently explore different trade-offs between objectives and sparsity.
For a more detailed understanding of the SEBO algorithm, please refer to the following publication:
By following this tutorial, you will learn how to leverage the SEBO method through the Ax API, empowering you to effectively balance objectives and sparsity in your optimization tasks. Let's get started!
import math
import os
import warnings
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import torch
from ax import Data, Experiment, ParameterType, RangeParameter, SearchSpace
from ax.core.objective import Objective
from ax.core.optimization_config import OptimizationConfig
from ax.metrics.noisy_function import NoisyFunctionMetric
from ax.modelbridge.generation_strategy import GenerationStep, GenerationStrategy
from ax.modelbridge.registry import Models
from ax.models.torch.botorch_modular.sebo import SEBOAcquisition
from ax.models.torch.botorch_modular.surrogate import Surrogate
from ax.runners.synthetic import SyntheticRunner
from ax.service.ax_client import AxClient, ObjectiveProperties
from ax.utils.common.typeutils import checked_cast
from botorch.acquisition.multi_objective import qNoisyExpectedHypervolumeImprovement
from botorch.models import SaasFullyBayesianSingleTaskGP, SingleTaskGP
%matplotlib inline
matplotlib.rcParams.update({"font.size": 16})
warnings.filterwarnings('ignore')
SMOKE_TEST = os.environ.get("SMOKE_TEST")
torch.manual_seed(12345) # To always get the same Sobol points
tkwargs = {
"dtype": torch.double,
"device": torch.device("cuda" if torch.cuda.is_available() else "cpu"),
}
In this simple experiment we use the Branin function embedded in a 10-dimensional space. Additional resources:
aug_dim = 8
# evaluation function
def branin_augment(x_vec, augment_dim):
assert len(x_vec) == augment_dim
x1, x2 = (
15 * x_vec[0] - 5,
15 * x_vec[1],
) # Only dimensions 0 and augment_dim-1 affect the value of the function
t1 = x2 - 5.1 / (4 * math.pi**2) * x1**2 + 5 / math.pi * x1 - 6
t2 = 10 * (1 - 1 / (8 * math.pi)) * np.cos(x1)
return t1**2 + t2 + 10
class AugBraninMetric(NoisyFunctionMetric):
def f(self, x: np.ndarray) -> float:
return checked_cast(float, branin_augment(x_vec=x, augment_dim=aug_dim))
# Create search space in Ax
search_space = SearchSpace(
parameters=[
RangeParameter(
name=f"x{i}",
parameter_type=ParameterType.FLOAT,
lower=0.0, upper=1.0
)
for i in range(aug_dim)
]
)
# Create optimization goals
optimization_config = OptimizationConfig(
objective=Objective(
metric=AugBraninMetric(
name="objective",
param_names=[f"x{i}" for i in range(aug_dim)],
noise_sd=None, # Set noise_sd=None if you want to learn the noise, otherwise it defaults to 1e-6
),
minimize=True,
)
)
# Experiment
experiment = Experiment(
name="sebo_experiment",
search_space=search_space,
optimization_config=optimization_config,
runner=SyntheticRunner(),
)
# target sparse point to regularize towards to. Here we set target sparse value being zero for all the parameters.
target_point = torch.tensor([0 for _ in range(aug_dim)], **tkwargs)
N_INIT = 10
if SMOKE_TEST:
N_BATCHES = 1
BATCH_SIZE = 1
SURROGATE_CLASS = None # Auto-pick SingleTaskGP
else:
N_BATCHES = 4
BATCH_SIZE = 5
SURROGATE_CLASS = SaasFullyBayesianSingleTaskGP
print(f"Doing {N_INIT + N_BATCHES * BATCH_SIZE} evaluations")
Doing 30 evaluations
# Initial Sobol points
sobol = Models.SOBOL(search_space=experiment.search_space)
for _ in range(N_INIT):
experiment.new_trial(sobol.gen(1)).run()
data = experiment.fetch_data()
for i in range(N_BATCHES):
model = Models.BOTORCH_MODULAR(
experiment=experiment,
data=data,
surrogate=Surrogate(botorch_model_class=SURROGATE_CLASS), # can use SAASGP (i.e. SaasFullyBayesianSingleTaskGP) for high-dim cases
search_space=experiment.search_space,
botorch_acqf_class=qNoisyExpectedHypervolumeImprovement,
acquisition_class=SEBOAcquisition,
acquisition_options={
"penalty": "L0_norm", # it can be L0_norm or L1_norm.
"target_point": target_point,
"sparsity_threshold": aug_dim,
},
torch_device=tkwargs['device'],
)
generator_run = model.gen(BATCH_SIZE)
trial = experiment.new_batch_trial(generator_run=generator_run)
trial.run()
new_data = trial.fetch_data(metrics=list(experiment.metrics.values()))
data = Data.from_multiple_data([data, new_data])
print(f"Iteration: {i}, Best so far: {data.df['mean'].min():.3f}")
Iteration: 0, Best so far: 2.494
Iteration: 1, Best so far: 2.494
Iteration: 2, Best so far: 2.096
Iteration: 3, Best so far: 1.952
Visualize the objective and sparsity trade-offs using SEBO. Each point represent designs along the Pareto frontier found by SEBO. The x-axis corresponds to the number of active parameters used, i.e. non-sparse parameters, and the y-axis corresponds the best identified objective values. Based on this, decision-makers balance both simplicity/interpretability of generated policies and optimization performance when deciding which configuration to use.
def nnz_exact(x, sparse_point):
return len(x) - (np.array(x) == np.array(sparse_point)).sum()
df = data.df
df['L0_norm'] = df['arm_name'].apply(lambda d: nnz_exact(list(experiment.arms_by_name[d].parameters.values()), [0 for _ in range(aug_dim)]) )
result_by_sparsity = {l: df[df.L0_norm <= l]['mean'].min() for l in range(1, aug_dim+1)}
result_by_sparsity
{1: 5.150157404871891, 2: 2.0592217231905074, 3: 2.0592217231905074, 4: 1.9515453912989535, 5: 1.9515453912989535, 6: 1.9515453912989535, 7: 1.9515453912989535, 8: 1.9515453912989535}
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(list(result_by_sparsity.keys()), list(result_by_sparsity.values()), '.b-', label="sebo", markersize=10)
ax.grid(True)
ax.set_title(f"Branin, D={aug_dim}", fontsize=20)
ax.set_xlabel("Number of active parameters", fontsize=20)
ax.set_ylabel("Best value found", fontsize=20)
# ax.legend(fontsize=18)
plt.show()
Please check Service API tutorial for more detailed information.
GenerationStrategy
¶gs = GenerationStrategy(
name="SEBO_L0",
steps=[
GenerationStep( # Initialization step
model=Models.SOBOL,
num_trials=N_INIT,
),
GenerationStep( # BayesOpt step
model=Models.BOTORCH_MODULAR,
# No limit on how many generator runs will be produced
num_trials=-1,
model_kwargs={ # Kwargs to pass to `BoTorchModel.__init__`
"surrogate": Surrogate(botorch_model_class=SURROGATE_CLASS),
"acquisition_class": SEBOAcquisition,
"botorch_acqf_class": qNoisyExpectedHypervolumeImprovement,
"acquisition_options": {
"penalty": "L0_norm", # it can be L0_norm or L1_norm.
"target_point": target_point,
"sparsity_threshold": aug_dim,
},
},
)
]
)
ax_client = AxClient(generation_strategy=gs)
experiment_parameters = [
{
"name": f"x{i}",
"type": "range",
"bounds": [0, 1],
"value_type": "float",
"log_scale": False,
}
for i in range(aug_dim)
]
objective_metrics = {
"objective": ObjectiveProperties(minimize=False, threshold=-10),
}
ax_client.create_experiment(
name="branin_augment_sebo_experiment",
parameters=experiment_parameters,
objectives=objective_metrics,
)
[INFO 09-23 21:56:15] 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-23 21:56:15] ax.service.utils.instantiation: Created search space: SearchSpace(parameters=[RangeParameter(name='x0', parameter_type=FLOAT, range=[0.0, 1.0]), 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]), RangeParameter(name='x7', parameter_type=FLOAT, range=[0.0, 1.0])], parameter_constraints=[]).
def evaluation(parameters):
# put parameters into 1-D array
x = [parameters.get(param["name"]) for param in experiment_parameters]
res = branin_augment(x_vec=x, augment_dim=aug_dim)
eval_res = {
# flip the sign to maximize
"objective": (res * -1, 0.0),
}
return eval_res
Running only 1 BO trial for demonstration.
for _ in range(N_INIT + 1):
parameters, trial_index = ax_client.get_next_trial()
res = evaluation(parameters)
ax_client.complete_trial(trial_index=trial_index, raw_data=res)
[INFO 09-23 21:56:15] ax.service.ax_client: Generated new trial 0 with parameters {'x0': 0.663117, 'x1': 0.06209, 'x2': 0.05106, 'x3': 0.106706, 'x4': 0.209877, 'x5': 0.883472, 'x6': 0.433962, 'x7': 0.521746} using model Sobol.
[INFO 09-23 21:56:15] ax.service.ax_client: Completed trial 0 with data: {'objective': (-12.357172, 0.0)}.
[INFO 09-23 21:56:15] ax.service.ax_client: Generated new trial 1 with parameters {'x0': 0.292397, 'x1': 0.879328, 'x2': 0.827177, 'x3': 0.76057, 'x4': 0.759851, 'x5': 0.479232, 'x6': 0.687372, 'x7': 0.190337} using model Sobol.
[INFO 09-23 21:56:15] ax.service.ax_client: Completed trial 1 with data: {'objective': (-55.842117, 0.0)}.
[INFO 09-23 21:56:15] ax.service.ax_client: Generated new trial 2 with parameters {'x0': 0.076506, 'x1': 0.310275, 'x2': 0.264478, 'x3': 0.259889, 'x4': 0.53493, 'x5': 0.077636, 'x6': 0.102992, 'x7': 0.471347} using model Sobol.
[INFO 09-23 21:56:15] ax.service.ax_client: Completed trial 2 with data: {'objective': (-90.978131, 0.0)}.
[INFO 09-23 21:56:15] ax.service.ax_client: Generated new trial 3 with parameters {'x0': 0.939657, 'x1': 0.631114, 'x2': 0.614857, 'x3': 0.607142, 'x4': 0.49924, 'x5': 0.544345, 'x6': 0.854282, 'x7': 0.802778} using model Sobol.
[INFO 09-23 21:56:15] ax.service.ax_client: Completed trial 3 with data: {'objective': (-53.56435, 0.0)}.
[INFO 09-23 21:56:15] ax.service.ax_client: Generated new trial 4 with parameters {'x0': 0.873154, 'x1': 0.491664, 'x2': 0.915377, 'x3': 0.666388, 'x4': 0.32378, 'x5': 0.788093, 'x6': 0.193053, 'x7': 0.294464} using model Sobol.
[INFO 09-23 21:56:15] ax.service.ax_client: Completed trial 4 with data: {'objective': (-41.234554, 0.0)}.
[INFO 09-23 21:56:15] ax.service.ax_client: Generated new trial 5 with parameters {'x0': 0.236809, 'x1': 0.574756, 'x2': 0.205165, 'x3': 0.451138, 'x4': 0.647891, 'x5': 0.31747, 'x6': 0.943427, 'x7': 0.993305} using model Sobol.
[INFO 09-23 21:56:15] ax.service.ax_client: Completed trial 5 with data: {'objective': (-11.179628, 0.0)}.
[INFO 09-23 21:56:15] ax.service.ax_client: Generated new trial 6 with parameters {'x0': 0.397719, 'x1': 0.239605, 'x2': 0.644298, 'x3': 0.950217, 'x4': 0.936166, 'x5': 0.219554, 'x6': 0.266331, 'x7': 0.712326} using model Sobol.
[INFO 09-23 21:56:15] ax.service.ax_client: Completed trial 6 with data: {'objective': (-16.440131, 0.0)}.
[INFO 09-23 21:56:15] ax.service.ax_client: Generated new trial 7 with parameters {'x0': 0.526495, 'x1': 0.826786, 'x2': 0.477099, 'x3': 0.166578, 'x4': 0.096063, 'x5': 0.627708, 'x6': 0.518582, 'x7': 0.013462} using model Sobol.
[INFO 09-23 21:56:15] ax.service.ax_client: Completed trial 7 with data: {'objective': (-99.261211, 0.0)}.
[INFO 09-23 21:56:15] ax.service.ax_client: Generated new trial 8 with parameters {'x0': 0.60372, 'x1': 0.346384, 'x2': 0.692216, 'x3': 0.383378, 'x4': 0.867337, 'x5': 0.592505, 'x6': 0.145545, 'x7': 0.34735} using model Sobol.
[INFO 09-23 21:56:15] ax.service.ax_client: Completed trial 8 with data: {'objective': (-16.569624, 0.0)}.
[INFO 09-23 21:56:16] ax.service.ax_client: Generated new trial 9 with parameters {'x0': 0.474943, 'x1': 0.71219, 'x2': 0.437113, 'x3': 0.733409, 'x4': 0.160993, 'x5': 0.05926, 'x6': 0.897185, 'x7': 0.92876} using model Sobol.
[INFO 09-23 21:56:16] ax.service.ax_client: Completed trial 9 with data: {'objective': (-60.913999, 0.0)}.
[INFO 09-23 21:56:53] ax.service.ax_client: Generated new trial 10 with parameters {'x0': 0.0, 'x1': 0.0, 'x2': 0.0, 'x3': 0.0, 'x4': 0.0, 'x5': 1.0, 'x6': 0.0, 'x7': 1.0} using model BoTorch.
[INFO 09-23 21:56:53] ax.service.ax_client: Completed trial 10 with data: {'objective': (-308.129096, 0.0)}.
Total runtime of script: 12 minutes, 56.19 seconds.