|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from typing import Iterable, Any |
|
|
|
|
|
from .pipeline import Pipeline |
|
|
from collections.abc import Mapping |
|
|
|
|
|
|
|
|
class Parameter: |
|
|
"""Base hyper-parameter""" |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
class Categorical(Parameter): |
|
|
"""Categorical hyper-parameter |
|
|
|
|
|
The value is sampled from `choices`. |
|
|
|
|
|
Parameters |
|
|
---------- |
|
|
choices : iterable |
|
|
Candidates of hyper-parameter value. |
|
|
""" |
|
|
|
|
|
def __init__(self, choices: Iterable): |
|
|
super().__init__() |
|
|
self.choices = list(choices) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DiscreteUniform(Parameter): |
|
|
"""Discrete uniform hyper-parameter |
|
|
|
|
|
The value is sampled from the range [low, high], |
|
|
and the step of discretization is `q`. |
|
|
|
|
|
Parameters |
|
|
---------- |
|
|
low : `float` |
|
|
Lower endpoint of the range of suggested values. |
|
|
`low` is included in the range. |
|
|
high : `float` |
|
|
Upper endpoint of the range of suggested values. |
|
|
`high` is included in the range. |
|
|
q : `float` |
|
|
A step of discretization. |
|
|
""" |
|
|
|
|
|
def __init__(self, low: float, high: float, q: float): |
|
|
super().__init__() |
|
|
self.low = float(low) |
|
|
self.high = float(high) |
|
|
self.q = float(q) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Integer(Parameter): |
|
|
"""Integer hyper-parameter |
|
|
|
|
|
The value is sampled from the integers in [low, high]. |
|
|
|
|
|
Parameters |
|
|
---------- |
|
|
low : `int` |
|
|
Lower endpoint of the range of suggested values. |
|
|
`low` is included in the range. |
|
|
high : `int` |
|
|
Upper endpoint of the range of suggested values. |
|
|
`high` is included in the range. |
|
|
""" |
|
|
|
|
|
def __init__(self, low: int, high: int): |
|
|
super().__init__() |
|
|
self.low = int(low) |
|
|
self.high = int(high) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LogUniform(Parameter): |
|
|
"""Log-uniform hyper-parameter |
|
|
|
|
|
The value is sampled from the range [low, high) in the log domain. |
|
|
|
|
|
Parameters |
|
|
---------- |
|
|
low : `float` |
|
|
Lower endpoint of the range of suggested values. |
|
|
`low` is included in the range. |
|
|
high : `float` |
|
|
Upper endpoint of the range of suggested values. |
|
|
`high` is excluded from the range. |
|
|
""" |
|
|
|
|
|
def __init__(self, low: float, high: float): |
|
|
super().__init__() |
|
|
self.low = float(low) |
|
|
self.high = float(high) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Uniform(Parameter): |
|
|
"""Uniform hyper-parameter |
|
|
|
|
|
The value is sampled from the range [low, high) in the linear domain. |
|
|
|
|
|
Parameters |
|
|
---------- |
|
|
low : `float` |
|
|
Lower endpoint of the range of suggested values. |
|
|
`low` is included in the range. |
|
|
high : `float` |
|
|
Upper endpoint of the range of suggested values. |
|
|
`high` is excluded from the range. |
|
|
""" |
|
|
|
|
|
def __init__(self, low: float, high: float): |
|
|
super().__init__() |
|
|
self.low = float(low) |
|
|
self.high = float(high) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Frozen(Parameter): |
|
|
"""Frozen hyper-parameter |
|
|
|
|
|
The value is fixed a priori |
|
|
|
|
|
Parameters |
|
|
---------- |
|
|
value : |
|
|
Fixed value. |
|
|
""" |
|
|
|
|
|
def __init__(self, value: Any): |
|
|
super().__init__() |
|
|
self.value = value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParamDict(Pipeline, Mapping): |
|
|
"""Dict-like structured hyper-parameter |
|
|
|
|
|
Usage |
|
|
----- |
|
|
>>> params = ParamDict(param1=Uniform(0.0, 1.0), param2=Uniform(-1.0, 1.0)) |
|
|
>>> params = ParamDict(**{"param1": Uniform(0.0, 1.0), "param2": Uniform(-1.0, 1.0)}) |
|
|
""" |
|
|
|
|
|
def __init__(self, **params): |
|
|
super().__init__() |
|
|
self.__params = params |
|
|
for param_name, param_value in params.items(): |
|
|
setattr(self, param_name, param_value) |
|
|
|
|
|
def __len__(self): |
|
|
return len(self.__params) |
|
|
|
|
|
def __iter__(self): |
|
|
return iter(self.__params) |
|
|
|
|
|
def __getitem__(self, param_name): |
|
|
return getattr(self, param_name) |
|
|
|