1
|
|
|
""" |
2
|
|
|
Hyperactive saves all positions it explores in a memory dictionary. If it encounters |
3
|
|
|
this positions again Hyperactive will just read the score from the memory dictionary |
4
|
|
|
instead of reevaluating the objective function. If there is a machine-/deep-learning |
5
|
|
|
model within the objective function this memory saves you a lot of computation |
6
|
|
|
time, because it is much faster to just look up the score in a dictionary instead |
7
|
|
|
of retraining an entire machine learning model. |
8
|
|
|
|
9
|
|
|
You can also pass the search data to the "memory_warm_start"-parameter of the next |
10
|
|
|
optimization run. This way the next optimization run has the memory of the |
11
|
|
|
previous run, which (again) saves you a lot of computation time. |
12
|
|
|
""" |
13
|
|
|
import time |
14
|
|
|
from sklearn.model_selection import cross_val_score |
15
|
|
|
from sklearn.tree import DecisionTreeRegressor |
16
|
|
|
from sklearn.datasets import load_diabetes |
17
|
|
|
from hyperactive import Hyperactive |
18
|
|
|
|
19
|
|
|
data = load_diabetes() |
20
|
|
|
X, y = data.data, data.target |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
def model(opt): |
24
|
|
|
gbr = DecisionTreeRegressor( |
25
|
|
|
max_depth=opt["max_depth"], |
26
|
|
|
min_samples_split=opt["min_samples_split"], |
27
|
|
|
) |
28
|
|
|
scores = cross_val_score(gbr, X, y, cv=10) |
29
|
|
|
|
30
|
|
|
return scores.mean() |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
search_space = { |
34
|
|
|
"max_depth": list(range(10, 35)), |
35
|
|
|
"min_samples_split": list(range(2, 22)), |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
c_time1 = time.time() |
39
|
|
|
hyper = Hyperactive() |
40
|
|
|
hyper.add_search(model, search_space, n_iter=100) |
41
|
|
|
hyper.run() |
42
|
|
|
d_time1 = time.time() - c_time1 |
43
|
|
|
print("Optimization time 1:", round(d_time1, 2)) |
44
|
|
|
|
45
|
|
|
# Hyperactive collects the search data |
46
|
|
|
search_data = hyper.search_data(model) |
47
|
|
|
|
48
|
|
|
# You can pass the search data to memory_warm_start to save time |
49
|
|
|
c_time2 = time.time() |
50
|
|
|
hyper = Hyperactive() |
51
|
|
|
hyper.add_search(model, search_space, n_iter=100, memory_warm_start=search_data) |
52
|
|
|
# The next run will be faster, because Hyperactive knows parts of the search space |
53
|
|
|
hyper.run() |
54
|
|
|
d_time2 = time.time() - c_time2 |
55
|
|
|
print("Optimization time 2:", round(d_time2, 2)) |
56
|
|
|
|