In this tutorial, we show how to do multi-fidelity BO with discrete fidelities based on [1], where each fidelity is a different "information source." This tutorial uses the same setup as the continuous multi-fidelity BO tutorial, except with discrete fidelity parameters that are interpreted as multiple information sources.
We use a GP model with a single task that models the design and fidelity parameters jointly. In some cases, where there is not a natural ordering in the fidelity space, it may be more appropriate to use a multi-task model (with, say, an ICM kernel). We will provide a tutorial once this functionality is in place.
[1] M. Poloczek, J. Wang, P.I. Frazier. Multi-Information Source Optimization. NeurIPS, 2017
import os
import torch
tkwargs = {
"dtype": torch.double,
"device": torch.device("cuda" if torch.cuda.is_available() else "cpu"),
}
SMOKE_TEST = os.environ.get("SMOKE_TEST")
We'll consider the Augmented Hartmann multi-fidelity synthetic test problem. This function is a version of the Hartmann6 test function with an additional dimension representing the fidelity parameter; details are in [2]. The function takes the form f(x,s) where x∈[0,1]6 and s∈{0.5,0.75,1}. The target fidelity is 1.0, which means that our goal is to solve max by making use of cheaper evaluations f(x,s) for s \in \{0.5, 0.75\}. In this example, we'll assume that the cost function takes the form 5.0 + s, illustrating a situation where the fixed cost is 5.0.
from botorch.test_functions.multi_fidelity import AugmentedHartmann
problem = AugmentedHartmann(negate=True).to(**tkwargs)
fidelities = torch.tensor([0.5, 0.75, 1.0], **tkwargs)
We use a SingleTaskMultiFidelityGP
as the surrogate model, which uses a kernel from [2] that is well-suited for multi-fidelity applications. The SingleTaskMultiFidelityGP
models the design and fidelity parameters jointly, so its domain is [0,1]^7.
from botorch.models.gp_regression_fidelity import SingleTaskMultiFidelityGP
from botorch.models.transforms.outcome import Standardize
from gpytorch.mlls.exact_marginal_log_likelihood import ExactMarginalLogLikelihood
def generate_initial_data(n=16):
# generate training data
train_x = torch.rand(n, 6, **tkwargs)
train_f = fidelities[torch.randint(3, (n, 1))]
train_x_full = torch.cat((train_x, train_f), dim=1)
train_obj = problem(train_x_full).unsqueeze(-1) # add output dimension
return train_x_full, train_obj
def initialize_model(train_x, train_obj):
# define a surrogate model suited for a "training data"-like fidelity parameter
# in dimension 6, as in [2]
model = SingleTaskMultiFidelityGP(train_x, train_obj, outcome_transform=Standardize(m=1), data_fidelity=6)
mll = ExactMarginalLogLikelihood(model.likelihood, model)
return mll, model
The helper function illustrates how one can initialize an qMFKG acquisition function. In this example, we assume that the affine cost is known. We then use the notion of a CostAwareUtility
in BoTorch to scalarize the "competing objectives" of information gain and cost. The MFKG acquisition function optimizes the ratio of information gain to cost, which is captured by the InverseCostWeightedUtility
.
In order for MFKG to evaluate the information gain, it uses the model to predict the function value at the highest fidelity after conditioning on the observation. This is handled by the project
argument, which specifies how to transform a tensor X
to its target fidelity. We use a default helper function called project_to_target_fidelity
to achieve this.
An important point to keep in mind: in the case of standard KG, one can ignore the current value and simply optimize the expected maximum posterior mean of the next stage. However, for MFKG, since the goal is optimize information gain per cost, it is important to first compute the current value (i.e., maximum of the posterior mean at the target fidelity). To accomplish this, we use a FixedFeatureAcquisitionFunction
on top of a PosteriorMean
.
from botorch import fit_gpytorch_mll
from botorch.models.cost import AffineFidelityCostModel
from botorch.acquisition.cost_aware import InverseCostWeightedUtility
from botorch.acquisition import PosteriorMean
from botorch.acquisition.knowledge_gradient import qMultiFidelityKnowledgeGradient
from botorch.acquisition.fixed_feature import FixedFeatureAcquisitionFunction
from botorch.optim.optimize import optimize_acqf
from botorch.acquisition.utils import project_to_target_fidelity
bounds = torch.tensor([[0.0] * problem.dim, [1.0] * problem.dim], **tkwargs)
target_fidelities = {6: 1.0}
cost_model = AffineFidelityCostModel(fidelity_weights={6: 1.0}, fixed_cost=5.0)
cost_aware_utility = InverseCostWeightedUtility(cost_model=cost_model)
def project(X):
return project_to_target_fidelity(X=X, target_fidelities=target_fidelities)
def get_mfkg(model):
curr_val_acqf = FixedFeatureAcquisitionFunction(
acq_function=PosteriorMean(model),
d=7,
columns=[6],
values=[1],
)
_, current_value = optimize_acqf(
acq_function=curr_val_acqf,
bounds=bounds[:,:-1],
q=1,
num_restarts=10 if not SMOKE_TEST else 2,
raw_samples=1024 if not SMOKE_TEST else 4,
options={"batch_limit": 10, "maxiter": 200},
)
return qMultiFidelityKnowledgeGradient(
model=model,
num_fantasies=128 if not SMOKE_TEST else 2,
current_value=current_value,
cost_aware_utility=cost_aware_utility,
project=project,
)
This helper function optimizes the acquisition function and returns the batch \{x_1, x_2, \ldots x_q\} along with the observed function values. The function optimize_acqf_mixed
sequentially optimizes the acquisition function over x for each value of the fidelity s \in \{0, 0.5, 1.0\}.
from botorch.optim.optimize import optimize_acqf_mixed
torch.set_printoptions(precision=3, sci_mode=False)
NUM_RESTARTS = 5 if not SMOKE_TEST else 2
RAW_SAMPLES = 128 if not SMOKE_TEST else 4
BATCH_SIZE = 4
def optimize_mfkg_and_get_observation(mfkg_acqf):
"""Optimizes MFKG and returns a new candidate, observation, and cost."""
# generate new candidates
candidates, _ = optimize_acqf_mixed(
acq_function=mfkg_acqf,
bounds=bounds,
fixed_features_list=[{6: 0.5}, {6: 0.75}, {6: 1.0}],
q=BATCH_SIZE,
num_restarts=NUM_RESTARTS,
raw_samples=RAW_SAMPLES,
# batch_initial_conditions=X_init,
options={"batch_limit": 5, "maxiter": 200},
)
# observe new values
cost = cost_model(candidates).sum()
new_x = candidates.detach()
new_obj = problem(new_x).unsqueeze(-1)
print(f"candidates:\n{new_x}\n")
print(f"observations:\n{new_obj}\n\n")
return new_x, new_obj, cost
First, let's generate some initial random data and fit a surrogate model.
train_x, train_obj = generate_initial_data(n=16)
We can now use the helper functions above to run a few iterations of BO.
cumulative_cost = 0.0
N_ITER = 3 if not SMOKE_TEST else 1
for i in range(N_ITER):
mll, model = initialize_model(train_x, train_obj)
fit_gpytorch_mll(mll)
mfkg_acqf = get_mfkg(model)
new_x, new_obj, cost = optimize_mfkg_and_get_observation(mfkg_acqf)
train_x = torch.cat([train_x, new_x])
train_obj = torch.cat([train_obj, new_obj])
cumulative_cost += cost
candidates: tensor([[0.199, 0.101, 0.436, 0.433, 0.197, 0.421, 0.750], [0.142, 0.274, 0.308, 0.413, 0.298, 0.570, 0.750], [0.097, 0.141, 0.417, 0.453, 0.477, 0.536, 0.500], [0.123, 0.022, 0.328, 0.430, 0.270, 0.689, 0.500]], device='cuda:0', dtype=torch.float64) observations: tensor([[1.369], [2.308], [1.404], [2.297]], device='cuda:0', dtype=torch.float64) candidates: tensor([[0.276, 0.159, 0.231, 0.462, 0.295, 0.633, 1.000], [0.213, 0.163, 0.297, 0.336, 0.276, 0.671, 0.750], [0.029, 0.235, 0.236, 0.405, 0.290, 0.709, 0.500], [0.159, 0.205, 0.360, 0.397, 0.361, 0.717, 1.000]], device='cuda:0', dtype=torch.float64) observations: tensor([[2.170], [2.984], [2.197], [2.588]], device='cuda:0', dtype=torch.float64) candidates: tensor([[0.268, 0.224, 0.340, 0.334, 0.230, 0.751, 0.500], [0.263, 0.181, 0.242, 0.307, 0.335, 0.735, 0.500], [0.166, 0.163, 0.345, 0.260, 0.278, 0.711, 0.500], [0.257, 0.238, 0.337, 0.311, 0.316, 0.639, 0.750]], device='cuda:0', dtype=torch.float64) observations: tensor([[2.565], [2.818], [3.036], [3.036]], device='cuda:0', dtype=torch.float64)
In multi-fidelity BO, there are usually fewer observations of the function at the target fidelity, so it is important to use a recommendation function that uses the correct fidelity. Here, we maximize the posterior mean with the fidelity dimension fixed to the target fidelity of 1.0.
def get_recommendation(model):
rec_acqf = FixedFeatureAcquisitionFunction(
acq_function=PosteriorMean(model),
d=7,
columns=[6],
values=[1],
)
final_rec, _ = optimize_acqf(
acq_function=rec_acqf,
bounds=bounds[:,:-1],
q=1,
num_restarts=10,
raw_samples=512,
options={"batch_limit": 5, "maxiter": 200},
)
final_rec = rec_acqf._construct_X_full(final_rec)
objective_value = problem(final_rec)
print(f"recommended point:\n{final_rec}\n\nobjective value:\n{objective_value}")
return final_rec
final_rec = get_recommendation(model)
print(f"\ntotal cost: {cumulative_cost}\n")
recommended point: tensor([[0.213, 0.164, 0.302, 0.327, 0.283, 0.689, 1.000]], device='cuda:0', dtype=torch.float64) objective value: tensor([3.021], device='cuda:0', dtype=torch.float64) total cost: 68.0
Let's now repeat the same steps using a standard EI acquisition function (note that this is not a rigorous comparison as we are only looking at one trial in order to keep computational requirements low).
from botorch.acquisition import qExpectedImprovement
def get_ei(model, best_f):
return FixedFeatureAcquisitionFunction(
acq_function=qExpectedImprovement(model=model, best_f=best_f),
d=7,
columns=[6],
values=[1],
)
def optimize_ei_and_get_observation(ei_acqf):
"""Optimizes EI and returns a new candidate, observation, and cost."""
candidates, _ = optimize_acqf(
acq_function=ei_acqf,
bounds=bounds[:, :-1],
q=BATCH_SIZE,
num_restarts=10,
raw_samples=512,
options={"batch_limit": 5, "maxiter": 200},
)
# add the fidelity parameter
candidates = ei_acqf._construct_X_full(candidates)
# observe new values
cost = cost_model(candidates).sum()
new_x = candidates.detach()
new_obj = problem(new_x).unsqueeze(-1)
print(f"candidates:\n{new_x}\n")
print(f"observations:\n{new_obj}\n\n")
return new_x, new_obj, cost
cumulative_cost = 0.0
train_x, train_obj = generate_initial_data(n=16)
for _ in range(N_ITER):
mll, model = initialize_model(train_x, train_obj)
fit_gpytorch_mll(mll)
ei_acqf = get_ei(model, best_f=train_obj.max())
new_x, new_obj, cost = optimize_ei_and_get_observation(ei_acqf)
train_x = torch.cat([train_x, new_x])
train_obj = torch.cat([train_obj, new_obj])
cumulative_cost += cost
candidates: tensor([[0.247, 0.687, 0.581, 0.760, 0.093, 0.132, 1.000], [0.319, 0.850, 0.639, 0.865, 0.000, 0.120, 1.000], [0.349, 0.666, 0.555, 0.986, 0.000, 0.126, 1.000], [0.297, 0.792, 0.450, 0.889, 0.034, 0.028, 1.000]], device='cuda:0', dtype=torch.float64) observations: tensor([[0.973], [1.091], [0.340], [0.902]], device='cuda:0', dtype=torch.float64) candidates: tensor([[0.194, 0.858, 0.622, 0.799, 0.000, 0.095, 1.000], [0.341, 0.854, 0.590, 0.767, 0.000, 0.085, 1.000], [0.999, 0.439, 0.828, 0.975, 0.633, 0.176, 1.000], [0.296, 0.859, 0.677, 0.806, 0.119, 0.054, 1.000]], device='cuda:0', dtype=torch.float64) observations: tensor([[ 0.862], [ 1.975], [ 0.000], [ 1.514]], device='cuda:0', dtype=torch.float64)
/data/sandcastle/boxes/fbsource/fbcode/buck-out/opt/gen/bento/kernels/bento_kernel_ae#link-tree/gpytorch/utils/cholesky.py:40: NumericalWarning: A not p.d., added jitter of 1.0e-08 to the diagonal
candidates: tensor([[0.360, 0.891, 0.588, 0.749, 0.019, 0.036, 1.000], [0.049, 0.894, 0.345, 0.210, 0.482, 0.463, 1.000], [0.398, 0.970, 0.504, 0.213, 0.814, 0.724, 1.000], [0.817, 0.879, 0.691, 0.842, 0.455, 0.937, 1.000]], device='cuda:0', dtype=torch.float64) observations: tensor([[2.271], [0.216], [0.055], [0.036]], device='cuda:0', dtype=torch.float64)
final_rec = get_recommendation(model)
print(f"\ntotal cost: {cumulative_cost}\n")
recommended point: tensor([[0.352, 0.874, 0.589, 0.756, 0.008, 0.060, 1.000]], device='cuda:0', dtype=torch.float64) objective value: tensor([2.166], device='cuda:0', dtype=torch.float64) total cost: 72.0