-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexp1.py
153 lines (138 loc) · 5.96 KB
/
exp1.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
import datetime
from pathlib import Path
from typing import Dict
from typing import List
import numpy as np
import yaml
from emukit.core import ContinuousParameter
from emukit.core import ParameterSpace
from joblib import Parallel
from joblib import delayed
from active_learning import convert_all_numpy_to_float
from active_learning import run_experiment
from metrics import avg_precision
from metrics import create_f1_evaluator
from models import Hierarchical
from models import MaskedGPClassifier
from models import MaskedModel
from plots import plot_ground_truth
from plots import plot_metrics
from test_functions import performance_function
from utils import summarise_results
def main(
misclassification_threshold: float = 0.02,
n_repeats: int = 5,
save_dir: Path = Path("tmp"),
n_jobs: int = 15,
max_iter: int = 150,
):
np.random.seed(0)
random_state = np.random.randint(np.iinfo(np.int32).max, size=5 * n_repeats)
save_dir = save_dir / datetime.datetime.now().strftime("%Y_%B_%d_%p%I:%M")
save_dir = save_dir / "test"
save_dir.mkdir(parents=True, exist_ok=True)
parameter_space = ParameterSpace([ContinuousParameter("x1", 0, 1)])
metric_fns = {
"f1": create_f1_evaluator(parameter_space, performance_function),
"Average Precision": avg_precision(parameter_space, performance_function),
}
(save_dir).mkdir(parents=True, exist_ok=True)
plot_ground_truth(performance_function, parameter_space, save_dir)
analytic_failure_probability = 0.036906
all_results = Parallel(n_jobs=n_jobs)(
list(
(
*(
delayed(run_experiment)(
performance_function=performance_function,
model=Hierarchical(),
parameter_space=parameter_space,
save_dir=save_dir / f"hierarchical_{idx}",
metric_fns=metric_fns,
misclassification_threshold=misclassification_threshold,
true_pf=analytic_failure_probability,
seed=random_state[idx],
max_iterations=max_iter,
)
for idx in range(n_repeats)
),
*(
delayed(run_experiment)(
performance_function=performance_function,
model=MaskedGPClassifier(),
parameter_space=parameter_space,
save_dir=save_dir / f"masked_gp_cls_{idx}",
metric_fns=metric_fns,
misclassification_threshold=misclassification_threshold,
true_pf=analytic_failure_probability,
seed=random_state[n_repeats + idx],
max_iterations=max_iter,
)
for idx in range(n_repeats)
),
*(
delayed(run_experiment)(
performance_function=performance_function,
model=MaskedModel(mask_with=1.0),
parameter_space=parameter_space,
save_dir=save_dir / f"masked_{idx}",
metric_fns=metric_fns,
misclassification_threshold=misclassification_threshold,
true_pf=analytic_failure_probability,
seed=random_state[2 * n_repeats + idx],
max_iterations=max_iter,
)
for idx in range(n_repeats)
),
*(
delayed(run_experiment)(
performance_function=performance_function,
model=MaskedModel(mask_with=0.5),
parameter_space=parameter_space,
save_dir=save_dir / f"masked05_{idx}",
metric_fns=metric_fns,
misclassification_threshold=misclassification_threshold,
true_pf=analytic_failure_probability,
seed=random_state[3 * n_repeats + idx],
max_iterations=max_iter,
)
for idx in range(n_repeats)
),
*(
delayed(run_experiment)(
performance_function=performance_function,
model=MaskedModel(mask_with=0.1),
parameter_space=parameter_space,
save_dir=save_dir / f"masked01_{idx}",
metric_fns=metric_fns,
misclassification_threshold=misclassification_threshold,
true_pf=analytic_failure_probability,
seed=random_state[4 * n_repeats + idx],
max_iterations=max_iter,
)
for idx in range(n_repeats)
),
)
)
)
metric_results_hierarchical = all_results[:n_repeats]
metric_results_masked_gpcls = all_results[n_repeats : 2 * n_repeats]
metric_results_masked = all_results[2 * n_repeats : 3 * n_repeats]
metric_results_masked05 = all_results[3 * n_repeats : 4 * n_repeats]
metric_results_masked01 = all_results[4 * n_repeats : 5 * n_repeats]
result_dict: Dict[str, List[List[Dict[str, float]]]] = {
"Hierarchical GP": metric_results_hierarchical,
r"Masked GP $\alpha=1$": metric_results_masked,
r"Masked GP $\alpha=0.5$": metric_results_masked05,
r"Masked GP $\alpha=0.1$": metric_results_masked01,
"Masked GP Classification": metric_results_masked_gpcls,
}
plot_metrics(
result_dict,
save_dir,
analytic_failure_probability,
)
with open(save_dir / "results.yaml", "w") as f:
yaml.dump(convert_all_numpy_to_float(summarise_results(result_dict)), f)
if __name__ == "__main__":
main()