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: 1.978
Iteration: 3, Best so far: 1.978
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.149716158344265, 2: 2.7323562630337737, 3: 1.978481306884305, 4: 1.978481306884305, 5: 1.978481306884305, 6: 1.978481306884305, 7: 1.978481306884305, 8: 1.978481306884305}
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 11-23 06:40:19] 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-23 06:40:19] 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 11-23 06:40:19] ax.service.ax_client: Generated new trial 0 with parameters {'x0': 0.477097, 'x1': 0.841431, 'x2': 0.370299, 'x3': 0.171158, 'x4': 0.760352, 'x5': 0.065085, 'x6': 0.707266, 'x7': 0.965243} using model Sobol.
[INFO 11-23 06:40:19] ax.service.ax_client: Completed trial 0 with data: {'objective': (-94.048278, 0.0)}.
[INFO 11-23 06:40:19] ax.service.ax_client: Generated new trial 1 with parameters {'x0': 0.891833, 'x1': 0.326752, 'x2': 0.612443, 'x3': 0.735843, 'x4': 0.345468, 'x5': 0.670298, 'x6': 0.073721, 'x7': 0.088411} using model Sobol.
[INFO 11-23 06:40:19] ax.service.ax_client: Completed trial 1 with data: {'objective': (-15.235848, 0.0)}.
[INFO 11-23 06:40:19] ax.service.ax_client: Generated new trial 2 with parameters {'x0': 0.674192, 'x1': 0.595042, 'x2': 0.222611, 'x3': 0.435491, 'x4': 0.093271, 'x5': 0.838499, 'x6': 0.964301, 'x7': 0.332227} using model Sobol.
[INFO 11-23 06:40:19] ax.service.ax_client: Completed trial 2 with data: {'objective': (-72.817335, 0.0)}.
[INFO 11-23 06:40:19] ax.service.ax_client: Generated new trial 3 with parameters {'x0': 0.199157, 'x1': 0.111735, 'x2': 0.980469, 'x3': 0.971884, 'x4': 0.551611, 'x5': 0.428073, 'x6': 0.316596, 'x7': 0.705886} using model Sobol.
[INFO 11-23 06:40:19] ax.service.ax_client: Completed trial 3 with data: {'objective': (-70.704362, 0.0)}.
[INFO 11-23 06:40:19] ax.service.ax_client: Generated new trial 4 with parameters {'x0': 0.11604, 'x1': 0.689614, 'x2': 0.640921, 'x3': 0.848008, 'x4': 0.682041, 'x5': 0.136322, 'x6': 0.84017, 'x7': 0.414718} using model Sobol.
[INFO 11-23 06:40:19] ax.service.ax_client: Completed trial 4 with data: {'objective': (-5.373719, 0.0)}.
[INFO 11-23 06:40:19] ax.service.ax_client: Generated new trial 5 with parameters {'x0': 0.515028, 'x1': 0.204172, 'x2': 0.398706, 'x3': 0.307224, 'x4': 0.204295, 'x5': 0.600949, 'x6': 0.440512, 'x7': 0.545208} using model Sobol.
[INFO 11-23 06:40:19] ax.service.ax_client: Completed trial 5 with data: {'objective': (-1.411663, 0.0)}.
[INFO 11-23 06:40:19] ax.service.ax_client: Generated new trial 6 with parameters {'x0': 0.79297, 'x1': 0.998874, 'x2': 0.758385, 'x3': 0.616124, 'x4': 0.483836, 'x5': 0.894173, 'x6': 0.58115, 'x7': 0.782166} using model Sobol.
[INFO 11-23 06:40:19] ax.service.ax_client: Completed trial 6 with data: {'objective': (-208.727027, 0.0)}.
[INFO 11-23 06:40:19] ax.service.ax_client: Generated new trial 7 with parameters {'x0': 0.333683, 'x1': 0.482303, 'x2': 0.000602, 'x3': 0.039237, 'x4': 0.879063, 'x5': 0.37441, 'x6': 0.199559, 'x7': 0.162173} using model Sobol.
[INFO 11-23 06:40:19] ax.service.ax_client: Completed trial 7 with data: {'objective': (-21.146753, 0.0)}.
[INFO 11-23 06:40:19] ax.service.ax_client: Generated new trial 8 with parameters {'x0': 0.285533, 'x1': 0.500724, 'x2': 0.819427, 'x3': 0.355738, 'x4': 0.265482, 'x5': 0.458681, 'x6': 0.753368, 'x7': 0.453462} using model Sobol.
[INFO 11-23 06:40:19] ax.service.ax_client: Completed trial 8 with data: {'objective': (-17.329802, 0.0)}.
[INFO 11-23 06:40:19] ax.service.ax_client: Generated new trial 9 with parameters {'x0': 0.841622, 'x1': 0.019064, 'x2': 0.077256, 'x3': 0.799326, 'x4': 0.848151, 'x5': 0.80612, 'x6': 0.402538, 'x7': 0.584689} using model Sobol.
[INFO 11-23 06:40:19] ax.service.ax_client: Completed trial 9 with data: {'objective': (-13.371954, 0.0)}.
[INFO 11-23 06:40:57] 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': 0.0, 'x6': 1.0, 'x7': 0.0} using model BoTorch.
[INFO 11-23 06:40:57] ax.service.ax_client: Completed trial 10 with data: {'objective': (-308.129096, 0.0)}.
Total runtime of script: 15 minutes, 20.47 seconds.