btb.tuning.tunable module

class btb.tuning.tunable.Tunable(hyperparams)[source]

Bases: object

Tunable class.

The Tunable class represents a collection of HyperParams that need to be tuned as a whole, at once.

hyperparams

Dict of HyperParams.

cardinality

Int or np.inf amount that indicates the number of combinations possible for this tunable.

Parameters

hyperparams (dict) – Dictionary object that contains the name and the hyperparameter asociated to it.

cardinality = 1
dimensions = 0
classmethod from_dict(dict_hyperparams)[source]

Create an instance from a dictionary containing information over hyperparameters.

Class method that creates an instance from a dictionary that describes the type of a hyperparameter, the range or values that this can have and the default value of the hyperparameter.

Parameters

dict_hyperparams (dict) –

A python dictionary containing as key the given name for the hyperparameter and as value a dictionary containing the following keys:

  • Type (str):

    bool for BoolHyperParam, int for IntHyperParam, float for FloatHyperParam, str for CategoricalHyperParam.

  • Range or Values (list):

    Range / values that this hyperparameter can take, in case of CategoricalHyperParam those will be used as the choices, for NumericalHyperParams the min value will be used as the minimum value and the max value will be used as the maximum value.

  • Default (str, bool, int, float or None):

    The default value for the hyperparameter.

Returns

A Tunable instance with the given hyperparameters.

Return type

Tunable

get_defaults()[source]

Return the default combination for the hyperparameters.

hyperparams = None
inverse_transform(values)[source]

Invert one or more hyperparameter configurations.

Invert one or more hyperparameter configurations from the normalized search space \([0, 1]^K\) to the original hyperparameter space.

Parameters

values (array-like) – 2D array of normalized values with shape (n, dimensions) where dimensions is the sum of dimensions from all the HyperParams that compose this tunable.

Returns

pandas.DataFrame

Example

The example below shows a simple usage of a Tunable class which will inverse transform a valid data from a 2D list and a pandas.DataFrame will be returned.

>>> from btb.tuning.hyperparams.boolean import BooleanHyperParam
>>> from btb.tuning.hyperparams.categorical import CategoricalHyperParam
>>> from btb.tuning.hyperparams.numerical import IntHyperParam
>>> chp = CategoricalHyperParam(['cat', 'dog'])
>>> bhp = BooleanHyperParam()
>>> ihp = IntHyperParam(1, 10)
>>> hyperparams = {
...     'chp': chp,
...     'bhp': bhp,
...     'ihp': ihp
... }
>>> tunable = Tunable(hyperparams)
>>> values = [
...     [1, 0, 0, 0.95],
...     [0, 1, 1, 0.05]
... ]
>>> tunable.inverse_transform(values)
   chp    bhp ihp
0  cat  False  10
1  dog   True   1
names = None
sample(n_samples)[source]

Sample values in the hyperparameters space for this tunable.

Parameters

n_samlpes (int) – Number of values to sample.

Returns

2D array with shape of (n_samples, dimensions) where dimensions is the sum of dimensions from all the HyperParams that compose this tunable.

Return type

numpy.ndarray

Example

The example below shows a simple usage of a Tunable class which will generate 2 samples by calling it’s sample method. This will return a numpy.ndarray.

>>> from btb.tuning.hyperparams.boolean import BooleanHyperParam
>>> from btb.tuning.hyperparams.categorical import CategoricalHyperParam
>>> from btb.tuning.hyperparams.numerical import IntHyperParam
>>> chp = CategoricalHyperParam(['cat', 'dog'])
>>> bhp = BooleanHyperParam()
>>> ihp = IntHyperParam(1, 10)
>>> hyperparams = {
...     'chp': chp,
...     'bhp': bhp,
...     'ihp': ihp
... }
>>> tunable = Tunable(hyperparams)
>>> tunable.sample(2)
array([[0.  , 1.  , 0.  , 0.45],
       [1.  , 0.  , 1.  , 0.95]])
transform(values)[source]

Transform one or more hyperparameter configurations.

Transform one or more hyperparameter configurations from the original hyperparameter space to the normalized search space.

Parameters

values (pandas.DataFrame, pandas.Series, dict, list(dict), 2D array-like) – Values of shape (n, len(self.hyperparams)).

Returns

2D array of shape (len(values), dimensions) where dimensions is the sum of dimensions from all the HyperParams that compose this tunable.

Return type

numpy.ndarray

Example

The example below shows a simple usage of a Tunable class which will transform a valid data from a 2D list and a numpy.ndarray is being returned.

>>> from btb.tuning.hyperparams.boolean import BooleanHyperParam
>>> from btb.tuning.hyperparams.categorical import CategoricalHyperParam
>>> from btb.tuning.hyperparams.numerical import IntHyperParam
>>> chp = CategoricalHyperParam(['cat', 'dog'])
>>> bhp = BooleanHyperParam()
>>> ihp = IntHyperParam(1, 10)
>>> hyperparams = {
...     'chp': chp,
...     'bhp': bhp,
...     'ihp': ihp
... }
>>> tunable = Tunable(hyperparams)
>>> values = [
...     ['cat', False, 10],
...     ['dog', True, 1],
... ]
>>> tunable.transform(values)
array([[1.  , 0.  , 0.  , 0.95],
       [0.  , 1.  , 1.  , 0.05]])