forked from microsoft/MLOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out the functionality of best config tracking into a separate …
…abstract Optimizer class (microsoft#709)
- Loading branch information
Showing
5 changed files
with
58 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# | ||
""" | ||
Mock optimizer for mlos_bench. | ||
""" | ||
|
||
import logging | ||
from abc import ABCMeta | ||
from typing import Optional, Tuple, Union | ||
|
||
from mlos_bench.environments.status import Status | ||
from mlos_bench.tunables.tunable_groups import TunableGroups | ||
|
||
from mlos_bench.optimizers.base_optimizer import Optimizer | ||
from mlos_bench.services.base_service import Service | ||
|
||
_LOG = logging.getLogger(__name__) | ||
|
||
|
||
class TrackBestOptimizer(Optimizer, metaclass=ABCMeta): | ||
""" | ||
Base Optimizer class that keeps track of the best score and configuration. | ||
""" | ||
|
||
def __init__(self, | ||
tunables: TunableGroups, | ||
config: dict, | ||
global_config: Optional[dict] = None, | ||
service: Optional[Service] = None): | ||
super().__init__(tunables, config, global_config, service) | ||
self._best_config: Optional[TunableGroups] = None | ||
self._best_score: Optional[float] = None | ||
|
||
def register(self, tunables: TunableGroups, status: Status, | ||
score: Optional[Union[float, dict]] = None) -> Optional[float]: | ||
registered_score = super().register(tunables, status, score) | ||
if status.is_succeeded() and ( | ||
self._best_score is None or (registered_score is not None and registered_score < self._best_score) | ||
): | ||
self._best_score = registered_score | ||
self._best_config = tunables.copy() | ||
return registered_score | ||
|
||
def get_best_observation(self) -> Union[Tuple[float, TunableGroups], Tuple[None, None]]: | ||
if self._best_score is None: | ||
return (None, None) | ||
assert self._best_config is not None | ||
return (self._best_score * self._opt_sign, self._best_config) |