ยป Keras API reference / Keras Tuner / HyperParameters

HyperParameters

HyperParameters class

keras_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.

Attributes

  • values: A dict mapping hyperparameter names to current values.

Boolean method

HyperParameters.Boolean(name, default=False, parent_name=None, parent_values=None)

Choice between True and False.

Arguments

  • name: A string. the name of parameter. Must be unique for each HyperParameter instance in the search space.
  • default: Boolean, the default value to return for the parameter. If unspecified, the default value will be False.
  • parent_name: Optional string, specifying the name of the parent HyperParameter to use as the condition to activate the current HyperParameter.
  • parent_values: Optional list of the values of the parent HyperParameter to use as the condition to activate the current HyperParameter.

Returns

The current value of this hyperparameter.


Choice method

HyperParameters.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

  • name: A string. the name of parameter. Must be unique for each HyperParameter instance in the search space.
  • values: A list of possible values. Values must be int, float, str, or bool. All values must be of the same type.
  • ordered: Optional boolean, whether the values passed should be considered to have an ordering. Defaults to True for float/int values. Must be False for any other values.
  • default: Optional default value to return for the parameter. If unspecified, the default value will be: - None if None is one of the choices in values - The first entry in values otherwise.
  • parent_name: Optional string, specifying the name of the parent HyperParameter to use as the condition to activate the current HyperParameter.
  • parent_values: Optional list of the values of the parent HyperParameter to use as the condition to activate the current HyperParameter.

Returns

The current value of this hyperparameter.


Fixed method

HyperParameters.Fixed(name, value, parent_name=None, parent_values=None)

Fixed, untunable value.

Arguments

  • name: A string. the name of parameter. Must be unique for each HyperParameter instance in the search space.
  • value: The value to use (can be any JSON-serializable Python type).
  • parent_name: Optional string, specifying the name of the parent HyperParameter to use as the condition to activate the current HyperParameter.
  • parent_values: Optional list of the values of the parent HyperParameter to use as the condition to activate the current HyperParameter.

Returns

The current value of this hyperparameter.


Float method

HyperParameters.Float(
    name,
    min_value,
    max_value,
    step=None,
    sampling=None,
    default=None,
    parent_name=None,
    parent_values=None,
)

Floating point range, can be evenly divided.

Arguments

  • name: A string. the name of parameter. Must be unique for each HyperParameter instance in the search space.
  • min_value: Float, the lower bound of the range.
  • max_value: Float, the upper bound of the range.
  • step: Optional float, e.g. 0.1, the smallest meaningful distance between two values. Whether step should be specified is Oracle dependent, since some Oracles can infer an optimal step automatically.
  • sampling: Optional string. One of "linear", "log", "reverse_log". Acts as a hint for an initial prior probability distribution for how this value should be sampled, e.g. "log" will assign equal probabilities to each order of magnitude range.
  • default: Float, the default value to return for the parameter. If unspecified, the default value will be min_value.
  • parent_name: Optional string, specifying the name of the parent HyperParameter to use as the condition to activate the current HyperParameter.
  • parent_values: Optional list of the values of the parent HyperParameter to use as the condition to activate the current HyperParameter.

Returns

The current value of this hyperparameter.


Int method

HyperParameters.Int(
    name,
    min_value,
    max_value,
    step=1,
    sampling=None,
    default=None,
    parent_name=None,
    parent_values=None,
)

Integer range.

Note that unlike Python's range function, max_value is included in the possible values this parameter can take on.

Arguments

  • name: A string. the name of parameter. Must be unique for each HyperParameter instance in the search space.
  • min_value: Integer, the lower limit of range, inclusive.
  • max_value: Integer, the upper limit of range, inclusive.
  • step: Integer, the distance between two consecutive samples in the range. Defaults to 1.
  • sampling: Optional string. One of "linear", "log", "reverse_log". Acts as a hint for an initial prior probability distribution for how this value should be sampled, e.g. "log" will assign equal probabilities to each order of magnitude range.
  • default: Integer, default value to return for the parameter. If unspecified, the default value will be min_value.
  • parent_name: Optional string, specifying the name of the parent HyperParameter to use as the condition to activate the current HyperParameter.
  • parent_values: Optional list of the values of the parent HyperParameter to use as the condition to activate the current HyperParameter.

Returns

The current value of this hyperparameter.


conditional_scope method

HyperParameters.conditional_scope(parent_name, parent_values)

Opens a scope to create conditional HyperParameters.

All HyperParameters 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 HyperParameters 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"])
        if model_type == "mlp":
            with hp.conditional_scope("model_type", ["mlp"]):
                model.add(Flatten())
                model.add(Dense(32, activation='relu'))
        if model_type == "cnn":
            with hp.conditional_scope("model_type", ["cnn"]):
                model.add(Conv2D(64, 3, activation='relu'))
                model.add(GlobalAveragePooling2D())
        model.add(Dense(10, activation='softmax'))
        return model

Arguments

  • parent_name: A string, specifying the name of the parent HyperParameter to use as the condition to activate the current HyperParameter.
  • parent_values: A list of the values of the parent HyperParameter to use as the condition to activate the current HyperParameter.

get method

HyperParameters.get(name)

Return the current value of this hyperparameter set.