test_pchoice.py
5.28 KB
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
from __future__ import print_function
from builtins import range
from functools import partial
import numpy as np
import unittest
from hyperopt import hp, Trials, fmin, tpe, anneal, rand
import hyperopt.pyll.stochastic
class TestPChoice(unittest.TestCase):
def test_basic(self):
space = hp.pchoice(
"naive_type",
[(0.14, "gaussian"), (0.02, "multinomial"), (0.84, "bernoulli")],
)
a, b, c = 0, 0, 0
rng = np.random.RandomState(123)
for i in range(0, 1000):
nesto = hyperopt.pyll.stochastic.sample(space, rng=rng)
if nesto == "gaussian":
a += 1
elif nesto == "multinomial":
b += 1
elif nesto == "bernoulli":
c += 1
print((a, b, c))
assert a + b + c == 1000
assert 120 < a < 160
assert 0 < b < 40
assert 800 < c < 900
def test_basic2(self):
space = hp.choice(
"normal_choice",
[
hp.pchoice("fsd", [(0.1, "first"), (0.8, "second"), (0.1, 2)]),
hp.choice("something_else", [10, 20]),
],
)
a, b, c = 0, 0, 0
rng = np.random.RandomState(123)
for i in range(0, 1000):
nesto = hyperopt.pyll.stochastic.sample(space, rng=rng)
if nesto == "first":
a += 1
elif nesto == "second":
b += 1
elif nesto == 2:
c += 1
elif nesto in (10, 20):
pass
else:
assert 0, nesto
print((a, b, c))
assert b > 2 * a
assert b > 2 * c
def test_basic3(self):
space = hp.pchoice(
"something",
[
(0.2, hp.pchoice("number", [(0.8, 2), (0.2, 1)])),
(0.8, hp.pchoice("number1", [(0.7, 5), (0.3, 6)])),
],
)
a, b, c, d = 0, 0, 0, 0
rng = np.random.RandomState(123)
for i in range(0, 2000):
nesto = hyperopt.pyll.stochastic.sample(space, rng=rng)
if nesto == 2:
a += 1
elif nesto == 1:
b += 1
elif nesto == 5:
c += 1
elif nesto == 6:
d += 1
else:
assert 0, nesto
print((a, b, c, d))
assert a + b + c + d == 2000
assert 300 < a + b < 500
assert 1500 < c + d < 1700
assert a * 0.3 > b # a * 1.2 > 4 * b
assert c * 3 * 1.2 > d * 7
class TestSimpleFMin(unittest.TestCase):
# test that that a space with a pchoice in it is
# (a) accepted for each algo (random, tpe, anneal)
# and
# (b) handled correctly.
#
def setUp(self):
self.space = hp.pchoice("a", [(0.1, 0), (0.2, 1), (0.3, 2), (0.4, 3)])
self.trials = Trials()
def objective(self, a):
return [1, 1, 1, 0][a]
def test_random(self):
max_evals = 150
fmin(
self.objective,
space=self.space,
trials=self.trials,
algo=rand.suggest,
rstate=np.random.RandomState(4),
max_evals=max_evals,
)
a_vals = [t["misc"]["vals"]["a"][0] for t in self.trials.trials]
counts = np.bincount(a_vals)
assert counts[3] > max_evals * 0.35
assert counts[3] < max_evals * 0.60
def test_tpe(self):
max_evals = 100
fmin(
self.objective,
space=self.space,
trials=self.trials,
algo=partial(tpe.suggest, n_startup_jobs=10),
rstate=np.random.RandomState(4),
max_evals=max_evals,
)
a_vals = [t["misc"]["vals"]["a"][0] for t in self.trials.trials]
counts = np.bincount(a_vals)
assert counts[3] > max_evals * 0.6
def test_anneal(self):
max_evals = 100
fmin(
self.objective,
space=self.space,
trials=self.trials,
algo=partial(anneal.suggest),
rstate=np.random.RandomState(4),
max_evals=max_evals,
)
a_vals = [t["misc"]["vals"]["a"][0] for t in self.trials.trials]
counts = np.bincount(a_vals)
assert counts[3] > max_evals * 0.6
def test_constant_fn_rand():
space = hp.choice(
"preprocess_choice",
[
{"pwhiten": hp.pchoice("whiten_randomPCA", [(0.3, False), (0.7, True)])},
{"palgo": False},
{"pthree": 7},
],
)
fmin(fn=lambda x: 1, space=space, algo=rand.suggest, max_evals=50)
def test_constant_fn_tpe():
space = hp.choice(
"preprocess_choice",
[
{"pwhiten": hp.pchoice("whiten_randomPCA", [(0.3, False), (0.7, True)])},
{"palgo": False},
{"pthree": 7},
],
)
fmin(
fn=lambda x: 1,
space=space,
algo=tpe.suggest,
max_evals=50,
rstate=np.random.RandomState(44),
)
def test_constant_fn_anneal():
space = hp.choice(
"preprocess_choice",
[
{"pwhiten": hp.pchoice("whiten_randomPCA", [(0.3, False), (0.7, True)])},
{"palgo": False},
{"pthree": 7},
],
)
fmin(fn=lambda x: 1, space=space, algo=anneal.suggest, max_evals=50)