Module panama.ml.auto.search_space
Classes
class SearchSpace (name: str = 'sp')
-
A class for defining a search space for hyperparameter tuning.
Attributes
name
- The name of the search space.
params
- A dictionary of parameter names and their search spaces.
Initializes the search space with the given name.
Args
name
- The name of the search space.
Returns
None.
Expand source code
class SearchSpace: """A class for defining a search space for hyperparameter tuning. Attributes: name: The name of the search space. params: A dictionary of parameter names and their search spaces. """ def __init__(self, name: str = "sp"): """Initializes the search space with the given name. Args: name: The name of the search space. Returns: None. """ self.name = name self.params = {} self.params_orig = {} def add_param(self, param_name: str, param_space: hp) -> None: """Adds a parameter and its search space to the search space. Args: param_name: The name of the parameter. param_space: The hyperopt search space for the parameter. Returns: None. """ self.params[param_name] = param_space def from_dict(self, space_dict: Dict) -> "SearchSpace": """Constructs the search space from a dictionary of parameter names and their search spaces. Args: space_dict: A dictionary of parameter names and their search spaces. Returns: SearchSpace: The constructed search space. """ self.params_orig = space_dict for param_name, param_space in space_dict.items(): if (param_space["type"] == "int") and ("min" in param_space.keys()) and ("max" in param_space.keys()): self.add_param( param_name, hp.uniformint( param_name, param_space["min"], param_space["max"], ), ) elif (param_space["type"] == "float") and ("min" in param_space.keys()) and ("max" in param_space.keys()): self.add_param( param_name, hp.uniform(param_name, param_space["min"], param_space["max"]), ) elif param_space["type"] == "choice" and "values" in param_space.keys(): self.add_param(param_name, hp.choice(param_name, param_space["values"])) else: raise ValueError(f"Invalid search space for parameter {param_name}") def from_json(self, space_file: str) -> "SearchSpace": """Constructs the search space from a JSON file. Args: space_file: The path to the JSON file. Returns: SearchSpace: The constructed search space. """ with open(space_file, "r") as f: space_dict = json.load(f) self.from_dict(space_dict) def from_default(self, model) -> "SearchSpace": """Constructs the search space from a JSON file. Args: model (BaseTunableModel): model for which default search space is needed. Returns: SearchSpace: The constructed search space. """ self.from_dict(model.DEFAULT_SEARCH_DICT) def get_space(self) -> Dict: """Returns the dictionary of parameter names and their search spaces. Args: None. Returns: dict: A dictionary of parameter names and their search spaces. """ return self.params def get_space_dict(self) -> Dict: """Returns the dictionary of parameter names and their search spaces. Args: None. Returns: dict: A dictionary of parameter names and their search spaces. """ return self.params_orig
Methods
def add_param(self, param_name: str, param_space:
) ‑> None -
Adds a parameter and its search space to the search space.
Args
param_name
- The name of the parameter.
param_space
- The hyperopt search space for the parameter.
Returns
None.
def from_default(self, model) ‑> SearchSpace
-
Constructs the search space from a JSON file.
Args
model
:BaseTunableModel
- model for which default search space is needed.
Returns
SearchSpace
- The constructed search space.
def from_dict(self, space_dict: Dict) ‑> SearchSpace
-
Constructs the search space from a dictionary of parameter names and their search spaces.
Args
space_dict
- A dictionary of parameter names and their search spaces.
Returns
SearchSpace
- The constructed search space.
def from_json(self, space_file: str) ‑> SearchSpace
-
Constructs the search space from a JSON file.
Args
space_file
- The path to the JSON file.
Returns
SearchSpace
- The constructed search space.
def get_space(self) ‑> Dict
-
Returns the dictionary of parameter names and their search spaces.
Args
None.
Returns
dict
- A dictionary of parameter names and their search spaces.
def get_space_dict(self) ‑> Dict
-
Returns the dictionary of parameter names and their search spaces.
Args
None.
Returns
dict
- A dictionary of parameter names and their search spaces.