def test_sampling_zero_length_intervals():
    f = hp_module.Float("f", 2, 2)
    rand_sample = f.random_sample()
    assert rand_sample == 2

    val = 2
    prob = hp_module.value_to_cumulative_prob(val, f)
    assert prob == 1
def test_reverse_log_sampling_random_state():
    f = hp_module.Float("f", 1e-3, 1e3, sampling="reverse_log")
    rand_sample = f.random_sample()
    assert rand_sample >= f.min_value
    assert rand_sample <= f.max_value

    val = 1e-3
    prob = hp_module.value_to_cumulative_prob(val, f)
    assert prob == 0
    new_val = hp_module.cumulative_prob_to_value(prob, f)
    assert np.isclose(val, new_val)

    val = 1
    prob = hp_module.value_to_cumulative_prob(val, f)
    assert prob > 0 and prob < 1
    new_val = hp_module.cumulative_prob_to_value(prob, f)
    assert np.isclose(val, new_val)
Beispiel #3
0
    def _vectorize_trials(self):
        x = []
        y = []
        ongoing_trials = set(self.ongoing_trials.values())
        for trial in self.trials.values():
            # Create a vector representation of each Trial's hyperparameters.
            trial_hps = trial.hyperparameters
            vector = []
            for hp in self._nonfixed_space():
                # For hyperparameters not present in the trial (either added after
                # the trial or inactive in the trial), set to default value.
                if (trial_hps.is_active(hp)  # inactive
                        and hp.name
                        in trial_hps.values  # added after the trial
                    ):
                    trial_value = trial_hps.values[hp.name]
                else:
                    trial_value = hp.default

                # Embed an HP value into the continuous space [0, 1].
                prob = hp_module.value_to_cumulative_prob(trial_value, hp)
                vector.append(prob)

            if trial in ongoing_trials:
                # "Hallucinate" the results of ongoing trials. This ensures that
                # repeat trials are not selected when running distributed.
                x_h = np.array(vector).reshape((1, -1))
                y_h_mean, y_h_std = self.gpr.predict(x_h)
                # Give a pessimistic estimate of the ongoing trial.
                score = y_h_mean[0] + y_h_std[0]
            elif trial.status == "COMPLETED":
                score = trial.score
                # Always frame the optimization as a minimization for scipy.minimize.
                if self.objective.direction == "max":
                    score = -1 * score
            else:
                continue

            x.append(vector)
            y.append(score)

        x = np.array(x)
        y = np.array(y)
        return x, y