btb.tuning.tuners.base module¶
Package where the BaseTuner class and BaseMetaModelTuner are defined.
-
class
btb.tuning.tuners.base.BaseMetaModelTuner(tunable, maximize=True, num_candidates=1000, min_trials=5)[source]¶ Bases:
btb.tuning.tuners.base.BaseTuner,btb.tuning.metamodels.base.BaseMetaModel,btb.tuning.acquisition.base.BaseAcquisitionBaseMetaModelTuner class.
BaseMetaModelTuner class is the abstract representation of a tuner that is based on a model and an
Acquisition. This model will try to predict the score that will be obtained with the proposed parameters by being trained over theself.trialsandself.raw_scoresrecorded by the user.-
tunable¶ Instance of a tunable class containing hyperparameters to be tuned.
-
trials¶ A
numpy.ndarraywith shape(n, self.tunable.dimensions)wherenis the number of trials recorded.- Type
numpy.ndarray
-
scores¶ A
numpy.ndarraywith shape(n, 1)wherenis the number of scores recorded.- Type
numpy.ndarray
- Parameters
tunable (btb.tuning.tunable.Tunable) – Instance of a tunable class containing hyperparameters to be tuned.
num_candidates (int) – Number of samples to generate and select the best of it for each proposal. Defaults to 1000.
maximize (bool) – If
Truethe model will understand that the score bigger is better, ifFalsethe smaller is better. Defaults toTrue.min_trials (int) – Number of recorded
trialsneeded to perform a fitting over the model. Defaults to 5.
-
record(trials, scores)[source]¶ Record one or more
trialswith the associatedscoresand re-fit the model.Trialsare recorded with the associatedscoresto them. The amount of trials must be equal to the amount of scores recived and vice versa. Once recorded, themodelis being fitted withself.trialsandself.raw_scoresthat contain any previous records and the ones that where just recorded.- Parameters
trials (pandas.DataFrame, pandas.Series, dict, list(dict), 2D array-like) – Values of shape
(n, len(self.tunable.hyperparameters))or dict with keys that areself.tunable.names.scores (single value or array-like) – A single value or array-like of values representing the score achieved with the trials.
- Raises
ValueError – A
ValueErrorexception is being produced iflen(trials)is not equal tolen(scores).
Example
The example below shows simple usage case where an
UniformTuneris being imported, instantiated with atunableobject and it’s method record is being called two times with valid trials and scores.>>> from btb.tuning.tunable import Tunable >>> from btb.tuning.hyperparams import BooleanHyperParam >>> from btb.tuning.hyperparams import CategoricalHyperParam >>> from btb.tuning.tuners import UniformTuner >>> bhp = BooleanHyperParam() >>> chp = CategoricalHyperParam(['cat', 'dog']) >>> tunable = Tunable({'bhp': bhp, 'chp': chp}) >>> tuner = UniformTuner(tunable) >>> tuner.record({'bhp': True, 'chp': 'cat'}, 0.8) >>> trials = [{'bhp': False, 'chp': 'cat'}, {'bhp': True, 'chp': 'dog'}] >>> scores = [0.8, 0.1] >>> tuner.record(trials, scores)
-
-
class
btb.tuning.tuners.base.BaseTuner(tunable, maximize=True)[source]¶ Bases:
objectBaseTuner class.
BaseTuner class is the abstract representation of a tuner that is not based on a model.
-
tunable¶ Instance of a tunable class containing hyperparameters to be tuned.
-
trials¶ A
numpy.ndarraywith shape(n, self.tunable.dimensions)wherenis the number of trials recorded.- Type
numpy.ndarray
-
raw_scores¶ A
numpy.ndarraywith shape(n, 1)wherenis the number of scores recorded.- Type
numpy.ndarray
-
scores¶ A
numpy.ndarraywith shape(n, 1)wherenis the number of normalized scores recorded.- Type
numpy.ndarray
- Parameters
tunable (btb.tuning.tunable.Tunable) – Instance of a tunable class containing hyperparameters to be tuned.
maximize (bool) – If
Truethe scores are interpreted as bigger is better, ifFalsethen smaller is better. Defaults toTrue.
-
propose(n=1, allow_duplicates=False)[source]¶ Propose one or more new hyperparameter configurations.
Validate that the amount of proposals requested is valid when
allow_duplicatesisFalseand raise an exception in case there is any missmatch betweenn, uniqueself.trialsandself.tunable.cardinality. Call the implemented_proposemethod and convert the returned data in to hyperparameter space values.- Parameters
n (int) – Number of candidates to create. Defaults to 1.
allow_duplicates (bool) – If it’s False, the tuner will propose trials that are not recorded. Otherwise will generate trials that can be repeated. Defaults to
False.
- Returns
If
nis 1, adictwill be returned containing the hyperparameter names and values. Otherwise, ifnis bigger than 1, a list of such dicts is returned.- Return type
dict or list
- Raises
ValueError – A
ValueErrorexception is being produced if the amount of requested proposals is bigger than the possible combinations andallow_duplicatesisFalse.ValueError – A
ValueErrorexception is being produced if the unique amount of recorded trials is the same as the amount of combinations available forself.tunable.ValueError – A
ValueErrorexception is being produced if the unique amount of recorded trials is the same as the amount of combinations available forself.tunable.
Example
The example below shows simple usage case where an
UniformTuneris being imported, instantiated with atunableobject and it’s method propose is being called three times, first with a single proposal, a second with two proposals forcing them to be different and once where the values can be repeated.>>> from btb.tuning.tunable import Tunable >>> from btb.tuning.hyperparams import BooleanHyperParam >>> from btb.tuning.hyperparams import CategoricalHyperParam >>> from btb.tuning.tuners import UniformTuner >>> bhp = BooleanHyperParam() >>> chp = CategoricalHyperParam(['cat', 'dog']) >>> tunable = Tunable({'bhp': bhp, 'chp': chp}) >>> tuner = UniformTuner(tunable) >>> tuner.propose(1) {'bhp': True, 'chp': 'dog'} >>> tuner.propose(2) [{'bhp': True, 'chp': 'cat'}, {'bhp': True, 'chp': 'dog'}] >>> tuner.propose(2, allow_duplicates=True) [{'bhp': False, 'chp': 'dog'}, {'bhp': False, 'chp': 'dog'}]
-
record(trials, scores)[source]¶ Record one or more
trialswith the associatedscores.Trialsare recorded with their associatedscores. The amount of trials must be equal to the amount of scores recived and vice versa.- Parameters
trials (pandas.DataFrame, pandas.Series, dict, list(dict), 2D array-like) – Values of shape
(n, len(self.tunable.hyperparameters))or dict with keys that areself.tunable.names.scores (single value or array-like) – A single value or array-like of values representing the score achieved with the trials.
- Raises
ValueError – A
ValueErrorexception is being produced iflen(trials)is not equal tolen(scores).
Example
The example below shows simple usage case where an
UniformTuneris being imported, instantiated with atunableobject and it’s method record is being called two times with valid trials and scores.>>> from btb.tuning.tunable import Tunable >>> from btb.tuning.hyperparams import BooleanHyperParam >>> from btb.tuning.hyperparams import CategoricalHyperParam >>> from btb.tuning.tuners import UniformTuner >>> bhp = BooleanHyperParam() >>> chp = CategoricalHyperParam(['cat', 'dog']) >>> tunable = Tunable({'bhp': bhp, 'chp': chp}) >>> tuner = UniformTuner(tunable) >>> tuner.record({'bhp': True, 'chp': 'cat'}, 0.8) >>> trials = [{'bhp': False, 'chp': 'cat'}, {'bhp': True, 'chp': 'dog'}] >>> scores = [0.8, 0.1] >>> tuner.record(trials, scores)
-