1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
import numpy as np |
6
|
|
|
|
7
|
|
|
from sklearn.datasets import load_iris |
8
|
|
|
from sklearn.model_selection import cross_val_score |
9
|
|
|
from sklearn.tree import DecisionTreeClassifier |
10
|
|
|
|
11
|
|
|
from hyperactive import Hyperactive |
12
|
|
|
|
13
|
|
|
data = load_iris() |
14
|
|
|
X, y = data.data, data.target |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
def model(para, X, y): |
18
|
|
|
dtc = DecisionTreeClassifier( |
19
|
|
|
max_depth=para["max_depth"], |
20
|
|
|
min_samples_split=para["min_samples_split"], |
21
|
|
|
min_samples_leaf=para["min_samples_leaf"], |
22
|
|
|
) |
23
|
|
|
scores = cross_val_score(dtc, X, y, cv=2) |
24
|
|
|
|
25
|
|
|
return scores.mean() |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
search_space = { |
29
|
|
|
"max_depth": range(1, 21), |
30
|
|
|
"min_samples_split": range(2, 21), |
31
|
|
|
"min_samples_leaf": range(1, 21), |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
def test_n_jobs(): |
36
|
|
|
n_jobs_list = [1, 2, 4, 10, 100, -1] |
37
|
|
|
for n_jobs in n_jobs_list: |
38
|
|
|
search = { |
39
|
|
|
"model": model, |
40
|
|
|
"search_space": search_space, |
41
|
|
|
"n_iter": 3, |
42
|
|
|
"n_jobs": 1, |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
hyper = Hyperactive(X, y) |
46
|
|
|
hyper.add_search(**search) |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def test_positional_args(): |
50
|
|
|
hyper = Hyperactive(X, y) |
51
|
|
|
hyper.add_search(model, search_space) |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def test_random_state(): |
55
|
|
|
opt0 = Hyperactive(X, y, random_state=False, memory=memory) |
|
|
|
|
56
|
|
|
opt0.search(search_config) |
|
|
|
|
57
|
|
|
|
58
|
|
|
opt1 = Hyperactive(X, y, random_state=0, memory=memory) |
59
|
|
|
opt1.search(search_config) |
60
|
|
|
|
61
|
|
|
opt2 = Hyperactive(X, y, random_state=1, memory=memory) |
62
|
|
|
opt2.search(search_config) |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
def test_max_time(): |
66
|
|
|
opt0 = Hyperactive(X, y, memory=memory) |
|
|
|
|
67
|
|
|
opt0.search(search_config, max_time=0.00001) |
|
|
|
|
68
|
|
|
|
69
|
|
|
|
70
|
|
|
def test_memory(): |
71
|
|
|
opt0 = Hyperactive(X, y, memory=True) |
72
|
|
|
opt0.search(search_config) |
|
|
|
|
73
|
|
|
|
74
|
|
|
opt1 = Hyperactive(X, y, memory=False) |
75
|
|
|
opt1.search(search_config) |
76
|
|
|
|
77
|
|
|
opt2 = Hyperactive(X, y, memory="short") |
78
|
|
|
opt2.search(search_config) |
79
|
|
|
|
80
|
|
|
opt3 = Hyperactive(X, y, memory="long") |
81
|
|
|
opt3.search(search_config) |
82
|
|
|
|
83
|
|
|
opt4 = Hyperactive(X, y, memory="long") |
84
|
|
|
opt4.search(search_config) |
85
|
|
|
|
86
|
|
|
opt = Hyperactive(X, y, memory=memory, verbosity=0) |
|
|
|
|
87
|
|
|
opt.search(search_config) |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
""" |
91
|
|
|
def test_dill(): |
92
|
|
|
from sklearn.gaussian_process import GaussianProcessClassifier |
93
|
|
|
from sklearn.gaussian_process.kernels import RBF, Matern |
94
|
|
|
from hypermemory import reset_memory |
95
|
|
|
|
96
|
|
|
reset_memory( |
97
|
|
|
meta_path="/home/simon/git_workspace/Hyperactive/hyperactive/meta_data/", |
98
|
|
|
force_true=True, |
99
|
|
|
) |
100
|
|
|
|
101
|
|
|
def model(para, X, y): |
102
|
|
|
gpc = GaussianProcessClassifier(kernel=para["kernel"]) |
103
|
|
|
scores = cross_val_score(gpc, X, y, cv=2) |
104
|
|
|
|
105
|
|
|
return scores.mean() |
106
|
|
|
|
107
|
|
|
search_config = {model: {"kernel": [RBF(), Matern()]}} |
108
|
|
|
|
109
|
|
|
opt0 = Hyperactive(X, y, memory="long") |
110
|
|
|
opt0.search(search_config) |
111
|
|
|
|
112
|
|
|
print("\n\n ------------------------------------------------------- \n\n") |
113
|
|
|
|
114
|
|
|
opt1 = Hyperactive(X, y, memory="long") |
115
|
|
|
opt1.search(search_config) |
116
|
|
|
""" |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
def test_verbosity0(): |
120
|
|
|
opt = Hyperactive(X, y, verbosity=0, memory=memory) |
|
|
|
|
121
|
|
|
opt.search(search_config) |
|
|
|
|
122
|
|
|
|
123
|
|
|
|
124
|
|
|
def test_verbosity1(): |
125
|
|
|
opt = Hyperactive(X, y, verbosity=0, memory=memory) |
|
|
|
|
126
|
|
|
opt.search(search_config, n_jobs=2) |
|
|
|
|
127
|
|
|
|
128
|
|
|
|
129
|
|
|
def test_verbosity2(): |
130
|
|
|
opt = Hyperactive(X, y, verbosity=1, memory=memory) |
|
|
|
|
131
|
|
|
opt.search(search_config, n_jobs=2) |
|
|
|
|
132
|
|
|
|
133
|
|
|
|
134
|
|
|
def test_verbosity3(): |
135
|
|
|
opt = Hyperactive(X, y, verbosity=1, memory=memory) |
|
|
|
|
136
|
|
|
opt.search(search_config) |
|
|
|
|
137
|
|
|
|
138
|
|
|
|
139
|
|
|
def test_verbosity4(): |
140
|
|
|
opt = Hyperactive(X, y, verbosity=2, memory=memory) |
|
|
|
|
141
|
|
|
opt.search(search_config) |
|
|
|
|
142
|
|
|
|
143
|
|
|
|
144
|
|
|
def test_verbosity5(): |
145
|
|
|
opt = Hyperactive(X, y, verbosity=2, memory=memory) |
|
|
|
|
146
|
|
|
opt.search(search_config, n_jobs=2) |
|
|
|
|
147
|
|
|
|
148
|
|
|
|
149
|
|
|
def test_scatter_init(): |
150
|
|
|
init_config = {model: {"scatter_init": 10}} |
151
|
|
|
opt = Hyperactive(X, y, memory=memory) |
|
|
|
|
152
|
|
|
opt.search(search_config, init_config=init_config) |
|
|
|
|
153
|
|
|
|
154
|
|
|
opt = Hyperactive(X, y, memory=memory, verbosity=0) |
155
|
|
|
opt.search(search_config, init_config=init_config) |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
def test_warm_start(): |
159
|
|
|
init_config = { |
160
|
|
|
model: {"max_depth": 10, "min_samples_split": 2, "min_samples_leaf": 5} |
161
|
|
|
} |
162
|
|
|
opt = Hyperactive(X, y, memory=memory) |
|
|
|
|
163
|
|
|
opt.search(search_config, n_iter=0, init_config=init_config) |
|
|
|
|
164
|
|
|
|
165
|
|
|
assert opt.results[model] == init_config[model] |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
def test_warm_start_multiple(): |
169
|
|
|
|
170
|
|
|
opt = Hyperactive(X, y, memory="short") |
171
|
|
|
opt.search(search_config, n_iter=10, n_jobs=2) |
|
|
|
|
172
|
|
|
|
173
|
|
|
|
174
|
|
|
def test_partial_warm_start(): |
175
|
|
|
init_config = {model: {"min_samples_split": 2, "min_samples_leaf": 5}} |
176
|
|
|
opt = Hyperactive(X, y, memory=memory) |
|
|
|
|
177
|
|
|
opt.search(search_config, n_iter=0, init_config=init_config) |
|
|
|
|
178
|
|
|
|
179
|
|
|
opt = Hyperactive(X, y, memory=memory, verbosity=0) |
180
|
|
|
opt.search(search_config, n_iter=0, init_config=init_config) |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
def test_optimizer_args(): |
184
|
|
|
opt = Hyperactive(X, y, memory=memory) |
|
|
|
|
185
|
|
|
opt.search(search_config, optimizer={"HillClimbing": {"epsilon": 0.1}}) |
|
|
|
|
186
|
|
|
|
187
|
|
|
|
188
|
|
|
""" |
189
|
|
|
def test_ray_1(): |
190
|
|
|
ray.init() |
191
|
|
|
opt = Hyperactive(X, y, memory=memory) |
192
|
|
|
opt.search(search_config, n_jobs=1) |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
def test_ray_2(): |
196
|
|
|
ray.init() |
197
|
|
|
opt = Hyperactive(X, y, memory=memory) |
198
|
|
|
opt.search(search_config, n_jobs=2) |
199
|
|
|
""" |
200
|
|
|
|