forked from optuna/optuna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_transform.py
215 lines (187 loc) · 7.73 KB
/
test_transform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import math
from typing import Any
import numpy
import pytest
from optuna._transform import _SearchSpaceTransform
from optuna.distributions import BaseDistribution
from optuna.distributions import CategoricalDistribution
from optuna.distributions import DiscreteUniformDistribution
from optuna.distributions import FloatDistribution
from optuna.distributions import IntLogUniformDistribution
from optuna.distributions import IntUniformDistribution
from optuna.distributions import LogUniformDistribution
from optuna.distributions import UniformDistribution
@pytest.mark.parametrize(
"param,distribution",
[
(0, IntUniformDistribution(0, 3)),
(1, IntLogUniformDistribution(1, 10)),
(2, IntUniformDistribution(0, 10, step=2)),
(0.0, UniformDistribution(0, 3)),
(1.0, LogUniformDistribution(1, 10)),
(0.2, DiscreteUniformDistribution(0, 1, q=0.2)),
(0.0, FloatDistribution(0, 3)),
(1.0, FloatDistribution(1, 10, log=True)),
(0.2, FloatDistribution(0, 1, step=0.2)),
("foo", CategoricalDistribution(["foo"])),
("bar", CategoricalDistribution(["foo", "bar", "baz"])),
],
)
def test_search_space_transform_shapes_dtypes(param: Any, distribution: BaseDistribution) -> None:
trans = _SearchSpaceTransform({"x0": distribution})
trans_params = trans.transform({"x0": param})
if isinstance(distribution, CategoricalDistribution):
expected_bounds_shape = (len(distribution.choices), 2)
expected_params_shape = (len(distribution.choices),)
else:
expected_bounds_shape = (1, 2)
expected_params_shape = (1,)
assert trans.bounds.shape == expected_bounds_shape
assert trans.bounds.dtype == numpy.float64
assert trans_params.shape == expected_params_shape
assert trans_params.dtype == numpy.float64
def test_search_space_transform_encoding() -> None:
trans = _SearchSpaceTransform({"x0": IntUniformDistribution(0, 3)})
assert len(trans.column_to_encoded_columns) == 1
numpy.testing.assert_equal(trans.column_to_encoded_columns[0], numpy.array([0]))
numpy.testing.assert_equal(trans.encoded_column_to_column, numpy.array([0]))
trans = _SearchSpaceTransform({"x0": CategoricalDistribution(["foo", "bar", "baz"])})
assert len(trans.column_to_encoded_columns) == 1
numpy.testing.assert_equal(trans.column_to_encoded_columns[0], numpy.array([0, 1, 2]))
numpy.testing.assert_equal(trans.encoded_column_to_column, numpy.array([0, 0, 0]))
trans = _SearchSpaceTransform(
{
"x0": UniformDistribution(0, 3),
"x1": CategoricalDistribution(["foo", "bar", "baz"]),
"x3": DiscreteUniformDistribution(0, 1, q=0.2),
}
)
assert len(trans.column_to_encoded_columns) == 3
numpy.testing.assert_equal(trans.column_to_encoded_columns[0], numpy.array([0]))
numpy.testing.assert_equal(trans.column_to_encoded_columns[1], numpy.array([1, 2, 3]))
numpy.testing.assert_equal(trans.column_to_encoded_columns[2], numpy.array([4]))
numpy.testing.assert_equal(trans.encoded_column_to_column, numpy.array([0, 1, 1, 1, 2]))
@pytest.mark.parametrize("transform_log", [True, False])
@pytest.mark.parametrize("transform_step", [True, False])
@pytest.mark.parametrize(
"param,distribution",
[
(0, IntUniformDistribution(0, 3)),
(3, IntUniformDistribution(0, 3)),
(1, IntLogUniformDistribution(1, 10)),
(10, IntLogUniformDistribution(1, 10)),
(2, IntUniformDistribution(0, 10, step=2)),
(10, IntUniformDistribution(0, 10, step=2)),
(0.0, UniformDistribution(0, 3)),
(3.0, UniformDistribution(0, 3)),
(1.0, LogUniformDistribution(1, 10)),
(10.0, LogUniformDistribution(1, 10)),
(0.2, DiscreteUniformDistribution(0, 1, q=0.2)),
(1.0, DiscreteUniformDistribution(0, 1, q=0.2)),
(0.0, FloatDistribution(0, 3)),
(1.0, FloatDistribution(1, 10, log=True)),
(0.2, FloatDistribution(0, 1, step=0.2)),
],
)
def test_search_space_transform_numerical(
transform_log: bool,
transform_step: bool,
param: Any,
distribution: BaseDistribution,
) -> None:
trans = _SearchSpaceTransform({"x0": distribution}, transform_log, transform_step)
expected_low = distribution.low # type: ignore
expected_high = distribution.high # type: ignore
if isinstance(distribution, LogUniformDistribution):
if transform_log:
expected_low = math.log(expected_low)
expected_high = math.log(expected_high)
elif isinstance(distribution, DiscreteUniformDistribution):
if transform_step:
half_step = 0.5 * distribution.q
expected_low -= half_step
expected_high += half_step
elif isinstance(distribution, FloatDistribution):
if transform_log and distribution.log:
expected_low = math.log(expected_low)
expected_high = math.log(expected_high)
if transform_step and distribution.step is not None:
half_step = 0.5 * distribution.step
expected_low -= half_step
expected_high += half_step
elif isinstance(distribution, IntUniformDistribution):
if transform_step:
half_step = 0.5 * distribution.step
expected_low -= half_step
expected_high += half_step
elif isinstance(distribution, IntLogUniformDistribution):
if transform_step:
half_step = 0.5
expected_low -= half_step
expected_high += half_step
if transform_log:
expected_low = math.log(expected_low)
expected_high = math.log(expected_high)
for bound in trans.bounds:
assert bound[0] == expected_low
assert bound[1] == expected_high
trans_params = trans.transform({"x0": param})
assert trans_params.size == 1
assert expected_low <= trans_params <= expected_high
@pytest.mark.parametrize(
"param,distribution",
[
("foo", CategoricalDistribution(["foo"])),
("bar", CategoricalDistribution(["foo", "bar", "baz"])),
],
)
def test_search_space_transform_values_categorical(
param: Any, distribution: CategoricalDistribution
) -> None:
trans = _SearchSpaceTransform({"x0": distribution})
for bound in trans.bounds:
assert bound[0] == 0.0
assert bound[1] == 1.0
trans_params = trans.transform({"x0": param})
for trans_param in trans_params:
assert trans_param in (0.0, 1.0)
def test_search_space_transform_untransform_params() -> None:
search_space = {
"x0": DiscreteUniformDistribution(0, 1, q=0.2),
"x1": CategoricalDistribution(["foo", "bar", "baz", "qux"]),
"x2": IntLogUniformDistribution(1, 10),
"x3": CategoricalDistribution(["quux", "quuz"]),
"x4": UniformDistribution(2, 3),
"x5": LogUniformDistribution(1, 10),
"x6": IntUniformDistribution(2, 4),
"x7": CategoricalDistribution(["corge"]),
"x8": UniformDistribution(-2, -2),
"x9": LogUniformDistribution(1, 1),
"x10": FloatDistribution(2, 3),
"x11": FloatDistribution(-2, 2),
"x12": FloatDistribution(1, 10),
"x13": FloatDistribution(1, 1),
"x14": FloatDistribution(0, 1, step=0.2),
}
params = {
"x0": 0.2,
"x1": "qux",
"x2": 1,
"x3": "quux",
"x4": 2.0,
"x5": 1.0,
"x6": 2,
"x7": "corge",
"x8": -2.0,
"x9": 1.0,
"x10": 2.0,
"x11": -2,
"x12": 1.0,
"x13": 1.0,
"x14": 0.2,
}
trans = _SearchSpaceTransform(search_space)
trans_params = trans.transform(params)
untrans_params = trans.untransform(trans_params)
for name in params.keys():
assert untrans_params[name] == params[name]