hbayes.target_space

FuncFailed

class hbayes.target_space.FuncFailed[source]
Overview:

Exception class thrown then target_func is failed. The result (including x and y) will be skipped and ignored after that.

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

max()[source]

Get maximum target value found and corresponding parameters.

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 x has been previously seen returns a cached value of y.

  • If FuncFailed is raised from target_func, this pair of x and y will be ignored, with return value of None.

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
res()[source]

Get all target values found and corresponding parameters.

set_bounds(new_bounds: Dict[str, Tuple[float, float]])[source]

A method that allows changing the lower and upper searching bounds

Parameters

new_bounds – A dictionary with the parameter name and its new bounds