|
1
|
|
|
""" |
|
2
|
|
|
Hyperactive is very versatile, because it can handle not just numerical or |
|
3
|
|
|
string variables in the search space, but also functions. If you want to |
|
4
|
|
|
search for the best list, numpy array, dataframed or class you can put them into a |
|
5
|
|
|
function that returns them as shown in the example below. |
|
6
|
|
|
|
|
7
|
|
|
This enables many possibilities for more complex optimization applications. |
|
8
|
|
|
Neural architecture search, feature engineering, ensemble optimization and many other applications are |
|
9
|
|
|
only possible or much easier if you can put functions in the search space. |
|
10
|
|
|
""" |
|
11
|
|
|
|
|
12
|
|
|
from hyperactive import Hyperactive |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def function_0(): |
|
16
|
|
|
# do stuff in function0 |
|
17
|
|
|
return |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
def function_1(): |
|
21
|
|
|
# do stuff in function1 |
|
22
|
|
|
return |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def function_2(): |
|
26
|
|
|
# do stuff in function2 |
|
27
|
|
|
return |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
def list1(): |
|
31
|
|
|
return [1, 0, 0] |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
def list2(): |
|
35
|
|
|
return [0, 1, 0] |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
def list3(): |
|
39
|
|
|
return [0, 0, 1] |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
# Hyperactive can handle python objects in the search space |
|
43
|
|
|
search_space = { |
|
44
|
|
|
"int": list(range(1, 10)), |
|
45
|
|
|
"float": [0.1, 0.01, 0.001], |
|
46
|
|
|
"string": ["string1", "string2"], |
|
47
|
|
|
"function": [function_0, function_1, function_2], |
|
48
|
|
|
"list": [list1, list2, list3], |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
def objective_function(para): |
|
53
|
|
|
# score must be a number |
|
54
|
|
|
score = 1 |
|
55
|
|
|
return score |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
hyper = Hyperactive() |
|
59
|
|
|
hyper.add_search(objective_function, search_space, n_iter=20) |
|
60
|
|
|
hyper.run() |
|
61
|
|
|
|
|
62
|
|
|
search_data = hyper.search_data(objective_function) |
|
63
|
|
|
|
|
64
|
|
|
print("\n Search Data: \n", search_data) |
|
65
|
|
|
|