dair_pll.hyperparameter
Interface for hyperparameter declaration and optimization.
Each experiment run (see
SupervisedLearningExperiment
) can
have its
hyperparameters optimized via optuna
. By design, each experiment
is fully described via a
SupervisedLearningExperimentConfig
object.
This file implements a Hyperparameter
class, which can be declared
as a member variable of a
SupervisedLearningExperimentConfig
,
or recursively as one of its own dataclass()
members.
The following hyperparameters types and priors are supported:
- class dair_pll.hyperparameter.ScalarT
Templating type hint for
Scalar
s.alias of TypeVar(‘ScalarT’, int, float)
- class dair_pll.hyperparameter.Hyperparameter(value, distribution)[source]
Bases:
ABC
Class for declaring and sampling hyperparameters.
Hyperparameters have both a
value
and adistribution
from which vales might be selected.Declaration of a hyperparameter in a configuration may look like:
@dataclass class XXXConfig: int_par: Int = Int(5, (0, 10)) float_par: Float = Float(0.1, (1e-4, 1e3), log=True) cat_par: Categorical = Categorical('val2', ['val0','val1', 'val2'])
In these cases, the first argument is the default
value
of the hyperparameter. However, at hyperparameter optimization time,optuna
will select hyperparameters from thedistribution
via thesuggest()
function. Some hyperparameter types have a default distribution as described in their documentation.-
distribution:
typing.Sequence
[typing.Union
[int
,float
,str
]] Parameters for distribution from which to select value.
-
value:
typing.Union
[int
,float
,str
] Hyperparameter’s current value.
-
distribution:
- class dair_pll.hyperparameter.Scalar(value, distribution=None, log=False)[source]
Bases:
Hyperparameter
,ABC
,Generic
[ScalarT
]Abstract scalar hyperparameter type.
Defines a uniform or log-uniform distribution over a scalar type, such as integers or real numbers.
The bounds of the distribution can either be specified as a tuple in the
distribution
attribute, or set as a default based on the providedvalue
in the abstract methoddefault_range()
.-
distribution:
typing.Tuple
[~ScalarT
,~ScalarT
] Bounds of scalar distribution in format (lower, upper).
-
distribution:
- class dair_pll.hyperparameter.Int(value, distribution=None, log=False)[source]
Bases:
Scalar
Integer scalar hyperparameter.
-
distribution:
typing.Tuple
[int
,int
] Bounds of scalar distribution in format (lower, upper).
-
distribution:
- class dair_pll.hyperparameter.Float(value, distribution=None, log=False)[source]
Bases:
Scalar
Real number (floating-point) scalar hyperparameter.
-
distribution:
typing.Tuple
[float
,float
] Bounds of scalar distribution in format (lower, upper).
-
distribution:
- class dair_pll.hyperparameter.Categorical(value, distribution)[source]
Bases:
Hyperparameter
Categorical hyperparameter.
-
value:
typing.Union
[int
,float
,str
] Hyperparameter’s current value.
-
distribution:
typing.List
[typing.Union
[int
,float
,str
]] Parameters for distribution from which to select value.
-
value:
- dair_pll.hyperparameter.is_dataclass_instance(obj)[source]
Helper function to check if input object is a
dataclass()
.- Return type:
- dair_pll.hyperparameter.traverse_config(config, callback, namespace='')[source]
Recursively searches through
config
and its memberdataclass()
es, for memberHyperparameter
objects.While traversing the tree, maintains a namespace constructed from a concatenation of the attributes’ names.
When a
Hyperparameter
‘h’ under attribute name attr is encountered, this function calls :py:arg:`callback` with inputs(namespace + attr, h)
.- Parameters:
config (
Any
) – Configurationdataclass()
.callback (
Callable
[[str
,Hyperparameter
],None
]) – Callback performed on eachHyperparameter
.namespace (
str
) – (Optional/internal) prefix for naming hyperparameters.
- Return type:
- dair_pll.hyperparameter.generate_suggestion(config, trial)[source]
Suggests a value all hyperparameters in configuration (but does not set these values).
Recursively searches through
config
and its memberdataclass()
es, and generates a suggestion for each containedHyperparameter
.
- dair_pll.hyperparameter.load_suggestion(config, suggestion)[source]
Fill all hyperparameters in configuration with suggestions.
Recursively searches through
config
and its memberdataclass()
es, and sets the values to the suggestion for each containedHyperparameter
.The
suggestion
is assumed to be generated by runninggenerate_suggestion()
on an identical type toconfig
.
- dair_pll.hyperparameter.hyperparameter_values(config)[source]
Lists current values for all hyperparameters in configuration.
Recursively searches through
config
and its memberdataclass()
es, and records value for each containedHyperparameter
.