HyperParameters
classkeras_tuner.HyperParameters()
Container for both a hyperparameter space, and current values.
A HyperParameters
instance can be pass to HyperModel.build(hp)
as an
argument to build a model.
To prevent the users from depending on inactive hyperparameter values, only
active hyperparameters should have values in HyperParameters.values
.
Attributes
HyperParameter
objects.Boolean
methodHyperParameters.Boolean(name, default=False, parent_name=None, parent_values=None)
Choice between True and False.
Arguments
HyperParameter
instance in the search space.HyperParameter
to use as the condition to activate the
current HyperParameter
.HyperParameter
to use as the condition to activate the
current HyperParameter
.Returns
The value of the hyperparameter, or None if the hyperparameter is not active.
Choice
methodHyperParameters.Choice(
name, values, ordered=None, default=None, parent_name=None, parent_values=None
)
Choice of one value among a predefined set of possible values.
Arguments
HyperParameter
instance in the search space.True
for float/int
values. Must be False
for any other values.values
values
otherwise.HyperParameter
to use as the condition to activate the
current HyperParameter
.HyperParameter
to use as the condition to activate the
current HyperParameter
.Returns
The value of the hyperparameter, or None if the hyperparameter is not active.
Fixed
methodHyperParameters.Fixed(name, value, parent_name=None, parent_values=None)
Fixed, untunable value.
Arguments
HyperParameter
instance in the search space.HyperParameter
to use as the condition to activate the
current HyperParameter
.HyperParameter
to use as the condition to activate the
current HyperParameter
.Returns
The value of the hyperparameter, or None if the hyperparameter is not active.
Float
methodHyperParameters.Float(
name,
min_value,
max_value,
step=None,
sampling="linear",
default=None,
parent_name=None,
parent_values=None,
)
Floating point value hyperparameter.
Example #1:
hp.Float(
"image_rotation_factor",
min_value=0,
max_value=1)
All values in interval [0, 1] have equal probability of being sampled.
Example #2:
hp.Float(
"image_rotation_factor",
min_value=0,
max_value=1,
step=0.2)
step
is the minimum distance between samples.
The possible values are [0, 0.2, 0.4, 0.6, 0.8, 1.0].
Example #3:
hp.Float(
"learning_rate",
min_value=0.001,
max_value=10,
step=10,
sampling="log")
When sampling="log"
, the step
is multiplied between samples.
The possible values are [0.001, 0.01, 0.1, 1, 10].
Arguments
HyperParameter
instance in the search space.sampling="linear"
, it will be the
minimum additve between two samples. If sampling="log"
, it
will be the minimum multiplier between two samples.sampling
argument decides how the value
is projected into the range of [min_value, max_value].
"linear": min_value + value * (max_value - min_value)
"log": min_value * (max_value / min_value) ^ value
"reverse_log":
(max_value -
min_value * ((max_value / min_value) ^ (1 - value) - 1))min_value
.HyperParameter
to use as the condition to activate the
current HyperParameter
.HyperParameter
to use as the condition to activate the
current HyperParameter
.Returns
The value of the hyperparameter, or None if the hyperparameter is not active.
Int
methodHyperParameters.Int(
name,
min_value,
max_value,
step=None,
sampling="linear",
default=None,
parent_name=None,
parent_values=None,
)
Integer hyperparameter.
Note that unlike Python's range
function, max_value
is included in
the possible values this parameter can take on.
Example #1:
hp.Int(
"n_layers",
min_value=6,
max_value=12)
The possible values are [6, 7, 8, 9, 10, 11, 12].
Example #2:
hp.Int(
"n_layers",
min_value=6,
max_value=13,
step=3)
step
is the minimum distance between samples.
The possible values are [6, 9, 12].
Example #3:
hp.Int(
"batch_size",
min_value=2,
max_value=32,
step=2,
sampling="log")
When sampling="log"
the step
is multiplied between samples.
The possible values are [2, 4, 8, 16, 32].
Arguments
HyperParameter
instance in the search space.sampling="linear"
, it will be the
minimum additve between two samples. If sampling="log"
, it
will be the minimum multiplier between two samples.sampling
argument decides how the value
is projected into the range of [min_value, max_value].
"linear": min_value + value * (max_value - min_value)
"log": min_value * (max_value / min_value) ^ value
"reverse_log":
(max_value -
min_value * ((max_value / min_value) ^ (1 - value) - 1))min_value
.HyperParameter
to use as the condition to activate the
current HyperParameter
.HyperParameter
to use as the condition to activate the
current HyperParameter
.Returns
The value of the hyperparameter, or None if the hyperparameter is not active.
conditional_scope
methodHyperParameters.conditional_scope(parent_name, parent_values)
Opens a scope to create conditional HyperParameters.
All HyperParameter
s created under this scope will only be active when
the parent HyperParameter
specified by parent_name
is equal to one
of the values passed in parent_values
.
When the condition is not met, creating a HyperParameter
under this
scope will register the HyperParameter
, but will return None
rather
than a concrete value.
Note that any Python code under this scope will execute regardless of whether the condition is met.
This feature is for the Tuner
to collect more information of the
search space and the current trial. It is especially useful for model
selection. If the parent HyperParameter
is for model selection, the
HyperParameter
s in a model should only be active when the model
selected, which can be implemented using conditional_scope
.
Examples
def MyHyperModel(HyperModel):
def build(self, hp):
model = Sequential()
model.add(Input(shape=(32, 32, 3)))
model_type = hp.Choice("model_type", ["mlp", "cnn"])
with hp.conditional_scope("model_type", ["mlp"]):
if model_type == "mlp":
model.add(Flatten())
model.add(Dense(32, activation='relu'))
with hp.conditional_scope("model_type", ["cnn"]):
if model_type == "cnn":
model.add(Conv2D(64, 3, activation='relu'))
model.add(GlobalAveragePooling2D())
model.add(Dense(10, activation='softmax'))
return model
Arguments
HyperParameter
to use as the condition to activate the
current HyperParameter
.HyperParameter
to use as the condition to activate the current
HyperParameter
.get
methodHyperParameters.get(name)
Return the current value of this hyperparameter set.