Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions ax/generators/torch/botorch_modular/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,9 @@ def gen(
expected_acquisition_value=expected_acquisition_value,
)
# log what model was used
metric_to_model_config_name = {
metric_name: model_config.name or str(model_config)
for metric_name, model_config in (
self.surrogate.metric_to_best_model_config.items()
)
}
gen_metadata["metric_to_model_config_name"] = metric_to_model_config_name
gen_metadata["metric_to_model_config_name"] = (
self.surrogate.model_name_by_metric
)
return TorchGenResults(
points=candidates.detach().cpu(),
weights=weights,
Expand Down
15 changes: 11 additions & 4 deletions ax/generators/torch/botorch_modular/surrogate.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,10 +835,9 @@ def model_selection(
f"Model {model_config} failed to fit with error {e}. Skipping."
)
continue
if (mc_name := model_config.name) is not None:
self._model_config_to_eval[mc_name] = {
self.surrogate_spec.eval_criterion: eval_metric
}
self._model_config_to_eval[model_config.identifier] = {
self.surrogate_spec.eval_criterion: eval_metric
}
if maximize ^ (eval_metric < best_eval_metric):
best_eval_metric = eval_metric
best_model = model
Expand Down Expand Up @@ -1103,6 +1102,14 @@ def outcomes(self) -> list[str]:
def outcomes(self, value: list[str]) -> None:
raise RuntimeError("Setting outcomes manually is disallowed.")

@property
def model_name_by_metric(self) -> dict[str, str]:
"""Returns a dictionary mapping metric names to model names."""
return {
metric_name: model_config.identifier
for metric_name, model_config in (self.metric_to_best_model_config.items())
}


submodel_input_constructor = Dispatcher(
name="submodel_input_constructor", encoder=_argparse_type_encoder
Expand Down
6 changes: 6 additions & 0 deletions ax/generators/torch/botorch_modular/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from collections.abc import Sequence
from copy import deepcopy
from dataclasses import dataclass, field
from functools import cached_property
from logging import Logger
from typing import Any, cast, Mapping

Expand Down Expand Up @@ -134,6 +135,11 @@ class string names and the values are dictionaries of input transform
likelihood_options: dict[str, Any] = field(default_factory=dict)
name: str | None = None

@cached_property
def identifier(self) -> str:
"""Returns a unique identifier for the model config."""
return self.name if self.name is not None else str(self)


def use_model_list(
datasets: Sequence[SupervisedDataset],
Expand Down
28 changes: 28 additions & 0 deletions ax/generators/torch/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import torch
from ax.core.search_space import SearchSpaceDigest
from ax.exceptions.core import AxWarning, UnsupportedError, UserInputError
from ax.generators.torch.botorch_modular.kernels import ScaleMaternKernel
from ax.generators.torch.botorch_modular.utils import (
_get_shared_rows,
choose_botorch_acqf_class,
Expand Down Expand Up @@ -667,3 +668,30 @@ def test_get_folds(self) -> None:
self.assertTrue(
torch.equal(cv_fold.train_dataset.Yvar, Yvar[3:]) # pyre-ignore[6]
)

def test_model_config(self) -> None:
# Test that model identifier is correctly computed.
mc1 = ModelConfig(
botorch_model_class=SingleTaskGP,
covar_module_class=ScaleMaternKernel,
covar_module_options={"ard_num_dims": 1},
name="GP",
)
self.assertEqual(mc1.identifier, "GP")
mc2 = ModelConfig(
botorch_model_class=SingleTaskGP,
covar_module_class=ScaleMaternKernel,
covar_module_options={"ard_num_dims": 1},
)
mc_str = (
"ModelConfig("
"botorch_model_class=<class 'botorch.models.gp_regression.SingleTaskGP'>, "
"model_options={}, mll_class=None, mll_options={}, "
"input_transform_classes=<class 'botorch.utils.types.DEFAULT'>, "
"input_transform_options={}, outcome_transform_classes=None, "
"outcome_transform_options={}, "
"covar_module_class=<class 'ax.generators.torch.botorch_modular.kernels."
"ScaleMaternKernel'>, covar_module_options={'ard_num_dims': 1}, "
"likelihood_class=None, likelihood_options={}, name=None)"
)
self.assertEqual(mc2.identifier, mc_str)