Пример #1
0
    def test_split_trials(self):
        """Test observed trials can be split based on TPE gamma"""
        space = Space()
        dim1 = Real("yolo1", "uniform", -3, 6)
        space.register(dim1)
        tpe = TPE(space, seed=1)
        rng = np.random.RandomState(1)
        points = numpy.linspace(-3, 3, num=10, endpoint=False).reshape(-1, 1)
        objectives = numpy.linspace(0, 1, num=10, endpoint=False)
        point_objectives = list(zip(points, objectives))
        rng.shuffle(point_objectives)
        points, objectives = zip(*point_objectives)
        for point, objective in zip(points, objectives):
            trial = _array_to_trial(point, space=tpe.space, y=objective)
            tpe.observe([trial])

        tpe.gamma = 0.25
        below_trials, above_trials = tpe.split_trials()
        below_points = [
            _trial_to_array(t, space=tpe.space) for t in below_trials
        ]
        assert below_points == [[-3.0], [-2.4], [-1.8]]
        assert len(above_trials) == 7

        tpe.gamma = 0.2
        below_trials, above_trials = tpe.split_trials()
        below_points = [
            _trial_to_array(t, space=tpe.space) for t in below_trials
        ]
        assert below_points == [[-3.0], [-2.4]]
        assert len(above_trials) == 8
Пример #2
0
    def test_suggest(self, tpe: TPE):
        """Test suggest with no shape dimensions"""
        tpe.n_initial_points = 10
        results = numpy.random.random(10)
        for i in range(10):
            trials = tpe.suggest(1)
            assert trials is not None
            assert len(trials) == 1
            points = [_trial_to_array(t, space=tpe.space) for t in trials]
            assert len(points[0]) == 3
            assert not isinstance(points[0][0], tuple)
            trials[0] = _add_result(trials[0], results[i])
            tpe.observe(trials)

        trials = tpe.suggest(1)
        assert trials is not None
        assert len(trials) == 1
        points = [_trial_to_array(t, space=tpe.space) for t in trials]
        assert len(points[0]) == 3
        assert not isinstance(points[0][0], tuple)
Пример #3
0
    def test_suggest_initial_points(self, tpe: TPE, monkeypatch):
        """Test that initial points can be sampled correctly"""
        _points = [(i, i - 6, "c") for i in range(1, 12)]
        _trials = [
            format_trials.tuple_to_trial(point, space=tpe.space)
            for point in _points
        ]
        index = 0

        def sample(num: int = 1, seed=None) -> list[Trial]:
            nonlocal index
            result = _trials[index:index + num]
            index += num
            return result

        monkeypatch.setattr(tpe.space, "sample", sample)

        tpe.n_initial_points = 10
        results = numpy.random.random(10)
        for i in range(1, 11):
            trials = tpe.suggest(1)
            assert trials is not None
            trial = trials[0]
            assert trial.params == _trials[i]
            point = format_trials.trial_to_tuple(trial, space=tpe.space)
            assert point == (i, i - 6, "c")
            trial.results = [
                Trial.Result(name="objective",
                             type="objective",
                             value=results[i - 1])
            ]
            tpe.observe([trial])

        trials = tpe.suggest(1)
        assert trials is not None
        trial = trials[0]
        assert trial == _trials[-1]
        # BUG: This is failing. We expect this trial to be sampled from the model, not from the
        # search space.
        assert format_trials.trial_to_tuple(trial,
                                            space=tpe.space) != (11, 5, "c")
Пример #4
0
    def test_suggest_ei_candidates(self, tpe: TPE):
        """Test suggest with no shape dimensions"""
        tpe.n_initial_points = 2
        tpe.n_ei_candidates = 0

        results = numpy.random.random(2)
        for i in range(2):
            trials = tpe.suggest(1)
            assert trials is not None
            assert len(trials) == 1
            points = [format_trials.trial_to_tuple(trials[0], space=tpe.space)]
            assert len(points[0]) == 3
            assert not isinstance(points[0][0], tuple)
            trials[0] = _add_result(trials[0], results[i])
            tpe.observe(trials)

        trials = tpe.suggest(1)
        assert not trials

        tpe.n_ei_candidates = 24
        trials = tpe.suggest(1)
        assert trials is not None
        assert len(trials) > 0