botorch#

Surrogate models built using BoTorch API and torch_model objects

Classes

SurrogateBoTorch([model_type, seed, ...])

BoTorch GP model, subclass of the obsidian SurrogateModel

class obsidian.surrogates.botorch.SurrogateBoTorch(model_type: str = 'GP', seed: int | None = None, verbose: bool = False, hps: dict = {})[source]#

Bases: SurrogateModel

BoTorch GP model, subclass of the obsidian SurrogateModel

model_type#

The type of model to be used.

Defaults to 'GP'. Options are as follows:

  • 'GP': Gaussian Process with default settings (Matern Kernel, Gamma covariance priors)

  • 'MixedGP': GP with mixed parameter types (continuous, categorical). Will be re-selected

    by default if ‘GP’ is selected and input space is mixed.

  • 'DKL': GP with a NN feature-extractor (deep kernel learning)

  • 'GPflat': GP without priors. May result in optimization instability, but removes bias

    for special situations.

  • 'GPprior': GP with custom priors on the mean, likelihood, and covariance

  • 'MTGP': Multi-task GP for multi-output optimization. Will be re-selected by default

    if ‘GP’ is selected and the input space contains Task parameters.

  • 'DNN': Dropout neural network. Uses MC sampling to mask neurons during training and

    to estimate uncertainty.

Type:

str

hps#

Optional surrogate function hyperparameters.

Type:

dict

mll#

The marginal log likelihood of the model.

Type:

ExactMarginalLogLikelihood

torch_model#

The torch model for the surrogate.

Type:

torch.nn.Module

loss#

The loss of the model.

Type:

float

r2_score#

The R2 score of the model.

Type:

float

fit(X: DataFrame, y: Series, cat_dims=None, task_feature=None)[source]#

Fits the surrogate model to data

Parameters:
  • X (pd.DataFrame) – Input parameters for the training data

  • y (pd.Series) – Training data responses

  • cat_dims (list, optional) – A list of indices for categorical dimensions in the input data. Default is None.

Returns:

None. Updates the surrogate model attributes, including regressed parameters.

init_model(X: DataFrame, y: Series, cat_dims: list[int], task_feature: int)[source]#

Instantiates the torch model for the surrogate. Cannot be called during __init__ normally as X,y are required and may not be available until fit methods are called

Parameters:
  • X (pd.DataFrame) – Input parameters for the training data.

  • y (pd.Series) – Training data responses.

  • cat_dims (list) – A list of indices for categorical dimensions in the input data.

Returns:

None. Updates surrogate attributes, including self.torch_model.

Raises:

TypeError – If cat_dims is not a list of integers.

classmethod load_state(obj_dict: dict)[source]#

Generates a BoTorch surrogate model from a state dictionary (saved after fitting). Note that the training data is necessary to instantiate a matching model, but no fitting occurs here.

Parameters:

obj_dict (OrderedDict) – The state dictionary of a previously fit BoTorch model.

Returns:

None. Updates the parameters of the model.

predict(X: DataFrame, q: float | None = None)[source]#

Computes model predictions over new experimental space.

Parameters:
  • X (pd.DataFrame) – Input parameters for the prediction space.

  • q (float, op)

Returns:

A tuple containing:
  • mu (ndarray): Mean responses for each experiment in the prediction space.

  • q_pred | sd (ndarray): Quantile or standard deviation of the predicted response for each experiment.

Return type:

tuple

save_state() dict[source]#

Saves the state of the SurrogateBoTorch model.

Returns:

A dictionary containing the state of the SurrogateBoTorch model.

Return type:

dict

score(X: DataFrame, y: Series) tuple[source]#

Computes simple model statistics on a dataset.

Parameters:
  • X (pd.DataFrame) – Input parameters for the evaluation data.

  • y (pd.DataFrame) – Evaluation data responses.

Returns:

A tuple containing the loss and R2 score of the evaluation.

Return type:

tuple