btb.tuning.hyperparams.base module

Package where the BaseHyperParam class is defined.

class btb.tuning.hyperparams.base.BaseHyperParam[source]

Bases: object

BaseHyperParam class.

A BaseHyperParam is an abstract representation of a single parameter that can be tuned.

dimensions

Number of dimensions that the hyperparameter uses to be represented in the search space.

Type

int

cardinality

Number of possible values for this hyperparameter.

Type

int or np.inf

cardinality = 0
default = None
dimensions = 0
inverse_transform(values)[source]

Invert one or more search space values.

Validates that the input values are within the search space and then transform them into hyperparameter values.

Parameters

values (scalar or array-like) – Scalar or array-like of values to be converted into the hyperparameter space.

Returns

2D numpy.ndarray containing values from the original hyperparameter space.

Return type

numpy.ndarray

Example

The example below shows simple usage case where an IntHyperParam is being imported, instantiated with a range from 1 to 4, and its method inverse_transform is being called two times with a scalar from the search space and an array of two valid values from the search space.

>>> from btb.tuning.hyperparams.numerical import IntHyperParam
>>> ihp = IntHyperParam(min=1, max=4)
>>> ihp.inverse_transform(0.125)
array([[1]])
>>> ihp.inverse_transform([0.125, 0.375])
array([[1],
       [2]])
abstract sample(n_samples)[source]

Generate an array of n_samples random samples in the search space.

Parameters

n_samples (int) – Number of values to sample.

Returns

2D numpy.ndarray with a shape (n_samples, self.dimensions).

Return type

numpy.ndarray

Example

The example below shows simple usage case where an IntHyperParam is being imported, instantiated with a range from 1 to 4, and its method sample is being called with a number of samples to be obtained. A numpy.ndarray with values from the search space is being returned.

>>> from btb.tuning.hyperparams.numerical import IntHyperParam
>>> instance = IntHyperParam(min=1, max=4)
>>> instance.sample(2)
array([[0.625],
       [0.375]])
transform(values)[source]

Transform one or more hyperparameter values.

Validates that the input values are within the accepted dimensions and that they are within the hyperparameter space. Then transform one or more hyperparameter values from the original hyperparameter space into the normalized search space \([0, 1]^K\). The accepted value formats are:

  • Scalar:

    A single scalar value from the original hyperparameter space.

  • List:

    A list composed by values from the original hyperparameter space.

  • 2D array-like:

    Two dimensions array-like object that contains values from the original hyperparameter space.

Parameters

values (scalar, list or array-like) – Single scalar value, list of values or array-like of values from the hyperparameter space to be converted into the search space.

Returns

2D numpy.ndarray of shape (len(values), self.dimensions) containing the search space values.

Return type

numpy.ndarray

Example

The example below shows simple usage case where an IntHyperParam is being imported, instantiated with a range from 1 to 4, and its method transform is being called three times with a single scalar value, an array of two valid values and a 2D array with 1 dimension.

>>> from btb.tuning.hyperparams.numerical import IntHyperParam
>>> ihp = IntHyperParam(min=1, max=4)
>>> ihp.transform(1)
array([[0.125]])
>>> ihp.transform([1, 2])
array([[0.125],
       [0.375]])
>>> ihp.transform([[1], [2]])
array([[0.125],
       [0.375]])