Beispiel #1
0
    def test_unsupported_space(self):
        """Test tpe only work for supported search space"""
        space = Space()
        dim1 = Real('yolo1', 'uniform', -10, 10)
        space.register(dim1)
        dim2 = Real('yolo2', 'reciprocal', 10, 20)
        space.register(dim2)
        categories = ['a', 0.1, 2, 'c']
        dim3 = Categorical('yolo3', categories)
        space.register(dim3)
        dim4 = Fidelity('epoch', 1, 9, 3)
        space.register(dim4)
        TPE(space)

        space = Space()
        dim = Real('yolo1', 'norm', 0.9)
        space.register(dim)

        with pytest.raises(ValueError) as ex:
            TPE(space)

        assert 'TPE now only supports uniform, loguniform, uniform discrete and choices' \
               in str(ex.value)

        space = Space()
        dim = Real('yolo1', 'uniform', 0.9, shape=(2, 1))
        space.register(dim)

        with pytest.raises(ValueError) as ex:
            TPE(space)

        assert 'TPE now only supports 1D shape' in str(ex.value)
Beispiel #2
0
    def test_unsupported_space(self):
        """Test tpe only work for supported search space"""
        space = Space()
        dim1 = Real("yolo1", "uniform", -10, 10)
        space.register(dim1)
        dim2 = Real("yolo2", "reciprocal", 10, 20)
        space.register(dim2)
        categories = ["a", 0.1, 2, "c"]
        dim3 = Categorical("yolo3", categories)
        space.register(dim3)
        dim4 = Fidelity("epoch", 1, 9, 3)
        space.register(dim4)
        TPE(space)

        space = Space()
        dim = Real("yolo1", "norm", 0.9)
        space.register(dim)

        with pytest.raises(ValueError) as ex:
            tpe = TPE(space)
            tpe.space = build_required_space(
                space, shape_requirement=TPE.requires_shape
            )

        assert (
            "TPE now only supports uniform, loguniform, uniform discrete and choices"
            in str(ex.value)
        )
Beispiel #3
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
Beispiel #4
0
    def test_sample_real_dimension(self):
        """Test sample values for a real dimension"""
        space = Space()
        dim1 = Real('yolo1', 'uniform', -10, 20)
        space.register(dim1)
        dim2 = Real('yolo2', 'uniform', -5, 10, shape=(2))
        space.register(dim2)
        dim3 = Real('yolo3', 'reciprocal', 1, 20)
        space.register(dim3)

        tpe = TPE(space)
        points = numpy.random.uniform(-10, 10, 20)
        below_points = [points[:8]]
        above_points = [points[8:]]
        points = tpe._sample_real_dimension(dim1, 1, below_points,
                                            above_points)
        points = numpy.asarray(points)
        assert len(points) == 1
        assert all(points >= -10)
        assert all(points < 10)

        points = numpy.random.uniform(1, 20, 20)
        below_points = [points[:8]]
        above_points = [points[8:]]
        points = tpe._sample_real_dimension(dim3, 1, below_points,
                                            above_points)
        points = numpy.asarray(points)
        assert len(points) == 1
        assert all(points >= 1)
        assert all(points < 20)

        below_points = numpy.random.uniform(-10, 0, 25).reshape(1, 25)
        above_points = numpy.random.uniform(0, 10, 75).reshape(1, 75)
        points = tpe._sample_real_dimension(dim1, 1, below_points,
                                            above_points)
        points = numpy.asarray(points)
        assert len(points) == 1
        assert all(points >= -10)
        assert all(points < 0)

        points = numpy.random.uniform(-5, 5, 32)
        below_points = [points[:8], points[8:16]]
        above_points = [points[16:24], points[24:]]
        points = tpe._sample_real_dimension(dim2, 2, below_points,
                                            above_points)
        points = numpy.asarray(points)
        assert len(points) == 2
        assert all(points >= -10)
        assert all(points < 10)

        tpe.n_ei_candidates = 0
        points = tpe._sample_real_dimension(dim2, 2, below_points,
                                            above_points)
        assert len(points) == 0
Beispiel #5
0
    def test_sample_categorical_dimension(self):
        """Test sample values for a categorical dimension"""
        space = Space()
        categories = ["a", "b", 11, 15, 17, 18, 19, 20, 25, "c"]
        dim1 = Categorical("yolo1", categories)
        space.register(dim1)
        dim2 = Categorical("yolo2", categories, shape=(2))
        space.register(dim2)

        tpe = TPE(space)

        obs_points = numpy.random.randint(0, 10, 100)
        obs_points = [categories[point] for point in obs_points]
        below_points = [obs_points[:25]]
        above_points = [obs_points[25:]]
        points = tpe.sample_one_dimension(
            dim1, 1, below_points, above_points, tpe._sample_categorical_point
        )
        assert len(points) == 1
        assert points[0] in categories

        obs_points_below = numpy.random.randint(0, 3, 25)
        obs_points_above = numpy.random.randint(3, 10, 75)
        below_points = [[categories[point] for point in obs_points_below]]
        above_points = [[categories[point] for point in obs_points_above]]
        points = tpe.sample_one_dimension(
            dim1, 1, below_points, above_points, tpe._sample_categorical_point
        )
        assert len(points) == 1
        assert points[0] in categories[:3]

        obs_points = numpy.random.randint(0, 10, 100)
        obs_points = [categories[point] for point in obs_points]
        below_points = [obs_points[:25], obs_points[25:50]]
        above_points = [obs_points[50:75], obs_points[75:]]

        points = tpe.sample_one_dimension(
            dim2, 2, below_points, above_points, tpe._sample_categorical_point
        )
        assert len(points) == 2
        assert points[0] in categories
        assert points[1] in categories

        tpe.n_ei_candidates = 0
        points = tpe.sample_one_dimension(
            dim2, 2, below_points, above_points, tpe._sample_categorical_point
        )
        assert len(points) == 0
Beispiel #6
0
    def test_sample_int_dimension(self):
        """Test sample values for a integer dimension"""
        space = Space()
        dim1 = Integer("yolo1", "uniform", -10, 20)
        space.register(dim1)

        dim2 = Integer("yolo2", "uniform", -5, 10, shape=(2))
        space.register(dim2)

        tpe = TPE(space)

        obs_points = numpy.random.randint(-10, 10, 100)
        below_points = [obs_points[:25]]
        above_points = [obs_points[25:]]
        points = tpe.sample_one_dimension(
            dim1, 1, below_points, above_points, tpe._sample_int_point
        )
        points = numpy.asarray(points)
        assert len(points) == 1
        assert all(points >= -10)
        assert all(points < 10)

        obs_points_below = numpy.random.randint(-10, 0, 25).reshape(1, 25)
        obs_points_above = numpy.random.randint(0, 10, 75).reshape(1, 75)
        points = tpe.sample_one_dimension(
            dim1, 1, obs_points_below, obs_points_above, tpe._sample_int_point
        )
        points = numpy.asarray(points)
        assert len(points) == 1
        assert all(points >= -10)
        assert all(points < 0)

        obs_points = numpy.random.randint(-5, 5, 100)
        below_points = [obs_points[:25], obs_points[25:50]]
        above_points = [obs_points[50:75], obs_points[75:]]
        points = tpe.sample_one_dimension(
            dim2, 2, below_points, above_points, tpe._sample_int_point
        )
        points = numpy.asarray(points)
        assert len(points) == 2
        assert all(points >= -10)
        assert all(points < 10)

        tpe.n_ei_candidates = 0
        points = tpe.sample_one_dimension(
            dim2, 2, below_points, above_points, tpe._sample_int_point
        )
        assert len(points) == 0
Beispiel #7
0
def tpe(space):
    """Return an instance of TPE."""
    return TPE(space, seed=1)