hbayes.target_space¶
FuncFailed¶
TargetSpace¶
-
class
hbayes.target_space.TargetSpace(target_func, pbounds, random_state=None)[source]¶ - Overview:
Holds the param-space coordinates (X) and target values (Y) Allows for constant-time appends while ensuring no duplicates are added.
- Examples:
>>> from hbayes.target_space import TargetSpace >>> >>> def target_func(p1, p2): ... return p1 + p2 >>> >>> pbounds = {'p1': (0, 1), 'p2': (1, 100)} >>> space = TargetSpace(target_func, pbounds, random_state=0) >>> x = space.random_sample() >>> y = space.probe(x) >>> print(x, y) [ 0.5488135 71.80374727] 72.35256077479684 >>> print(space.max()) {'target': 72.35256077479684, 'params': {'p1': 0.5488135039273248, 'p2': 71.80374727086952}}
-
__init__(target_func, pbounds, random_state=None)[source]¶ Constructor of
TargetSpace.- Parameters
target_func – Function to be maximized.
pbounds – Dictionary with parameters names as keys and a tuple with minimum and maximum values.
random_state – Optionally specify a seed for a random number generator
-
probe(x: Union[numpy.ndarray, Dict[str, float]]) → Optional[float][source]¶ Evaluates a single point x, to obtain the value y and then records them as observations.
Note
If
xhas been previously seen returns a cached value of y.If
FuncFailedis raised fromtarget_func, this pair of x and y will be ignored, with return value ofNone.
- Parameters
x – A single point, with
len(params) == self.dim.- Returns
Target function value.
-
random_sample() → numpy.ndarray[source]¶ Creates random points within the bounds of the space.
- Returns
[dim] array points with dimensions corresponding to self._keys
- Examples:
>>> from hbayes.target_space import TargetSpace >>> >>> target_func = lambda p1, p2: p1 + p2 >>> pbounds = {'p1': (0, 1), 'p2': (1, 100)} >>> space = TargetSpace(target_func, pbounds, random_state=0) >>> space.random_sample() array([ 0.5488135 , 71.80374727])
-
register(x, y)[source]¶ Append a point and its target value to the known data.
- Parameters
x – A single point, with len(x) == self.dim.
y – Target function value.
- Raises
KeyError – If the point is not unique.
Note
Runs in ammortized constant time.
- Examples:
>>> import numpy as np >>> from hbayes.target_space import TargetSpace >>> >>> pbounds = {'p1': (0, 1), 'p2': (1, 100)} >>> space = TargetSpace(lambda p1, p2: p1 + p2, pbounds) >>> len(space) 0 >>> x_ = np.array([0, 0]) >>> y = 1 >>> space.register(x_, y) [[0. 0.]] [1.] >>> len(space) 1