def convert_hyperparams_to_hparams()

in src/python/tensorflow_cloud/tuner/utils.py [0:0]


def convert_hyperparams_to_hparams(
    hyperparams: hp_module.HyperParameters) -> Dict[hparams_api.HParam, Any]:
    """Converts KerasTuner HyperParameters to TensorBoard HParams.

    Args:
        hyperparams: A KerasTuner HyperParameters instance

    Returns:
        A dict that maps TensorBoard HParams to current values.
    """
    hparams = {}
    for hp in hyperparams.space:
        hparams_value = {}
        try:
            hparams_value = hyperparams.get(hp.name)
        except ValueError:
            continue

        hparams_domain = {}
        if isinstance(hp, hp_module.Choice):
            hparams_domain = hparams_api.Discrete(hp.values)
        elif isinstance(hp, hp_module.Int):
            if hp.step is None or hp.step == 1:
                hparams_domain = hparams_api.IntInterval(
                    hp.min_value, hp.max_value)
            else:
                # Note: `hp.max_value` is inclusive, unlike the end index
                # of Python `range()`, which is exclusive
                values = list(
                    range(hp.min_value, hp.max_value + 1, hp.step))
                hparams_domain = hparams_api.Discrete(values)
        elif isinstance(hp, hp_module.Float):
            if hp.step is None:
                hparams_domain = hparams_api.RealInterval(
                    hp.min_value, hp.max_value)
            else:
                # Note: `hp.max_value` is inclusive, which is also
                # the default for Numpy's linspace
                num_samples = int((hp.max_value - hp.min_value) / hp.step)
                end_value = hp.min_value + (num_samples * hp.step)
                values = np.linspace(
                    hp.min_value, end_value, num_samples + 1).tolist()
                hparams_domain = hparams_api.Discrete(values)
        elif isinstance(hp, hp_module.Boolean):
            hparams_domain = hparams_api.Discrete([True, False])
        elif isinstance(hp, hp_module.Fixed):
            hparams_domain = hparams_api.Discrete([hp.value])
        else:
            raise ValueError(
                "`HyperParameter` type not recognized: {}".format(hp))

        hparams_key = hparams_api.HParam(hp.name, hparams_domain)
        hparams[hparams_key] = hparams_value

    return hparams