-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbo_fromdata.py
More file actions
executable file
·222 lines (187 loc) · 5.37 KB
/
bo_fromdata.py
File metadata and controls
executable file
·222 lines (187 loc) · 5.37 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python3
"""
This script demonstrates a Bayesian Optimization (BO) routine on a chosen
dataset and plots performance based on max yield obtained of various acquisition
function choices: Expected Improvement (EI), Probability of Improvement (PI),
Upper Confidence Bound (UCB), Predictive Variance (PV), random.
The approach is:
1. Obtain an initial set of training data from chosen dataset
2. Train a GP model on the training data
3. Compute the acquisition function at the remaining data points
4. Add the point with the highest value based on the acquisition function of
choice to the training data
5. Return to step 2 and repeat until the user defined number of acquired points
is reached
Usage:
# Make script executable
chmod +x ./bo_fromdata.py
# See help.
./bo_fromdata.py -h
# Perform BO with 5 initial starting points, 30 iterations, and a Matern kernel
./bo_fromdata.py -in 5 -it 30 -k matern
# Perform BO with 10 initial starting points, 30 iterations, and an RBF kernel
./bo_fromdata.py -in 10 -it 30 -k rbf
"""
import argparse
from surmod import bayesian_optimization as bo, data_processing
def parse_arguments():
"""Get command line arguments."""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="Perform Bayesian optimization on JAG data.",
)
parser.add_argument(
"-d",
"--data",
type=str,
choices=["JAG", "borehole"],
default="JAG",
help="Which dataset to use (default: JAG).",
)
parser.add_argument(
"-ny",
"--normalize_y",
action="store_true",
help="Whether or not to normalize the output values in the"
" GaussianProcessRegressor.",
)
parser.add_argument(
"-it",
"--num_iter",
type=int,
default=10,
help="Number of BO iterations (number of data points to acquire).",
)
parser.add_argument(
"-in",
"--num_init",
type=int,
default=5,
help="Number of initial sample points.",
)
parser.add_argument(
"-k",
"--kernel",
type=str,
choices=["matern", "rbf", "matern_dot"],
default="matern",
help="Choose kernel.",
)
parser.add_argument(
"-s",
"--seed",
type=int,
default=42,
help="Set random seed for reproducibility.",
)
parser.add_argument(
"-xi",
"--xi",
type=float,
default=0.0,
help="Exploration-exploitation trade-off parameter for EI and PI acquisition functions (non-negative float).",
)
parser.add_argument(
"-kappa",
"--kappa",
type=float,
default=2.0,
help="Exploration-exploitation trade-off parameter for UCB acquisition function (non-negative float).",
)
args = parser.parse_args()
return args
def main():
# Parse command-line arguments
args = parse_arguments()
data = args.data
normalize_y = args.normalize_y
kernel = args.kernel
num_init = args.num_init
num_iter = args.num_iter
seed = args.seed
# Check data availability
num_samples = num_init + num_iter
if num_samples > 10000:
raise ValueError(
f"Total samples ({num_samples}) exceed existing dataset(s) size "
"limit (10000)."
)
df = data_processing.load_data(dataset=data, n_samples=num_samples, random=False)
x = df.iloc[:, :-1].to_numpy()
y = df.iloc[:, -1].to_numpy()
bayes_opt_EI = bo.BayesianOptimizer(
data,
x,
y,
normalize_y,
kernel,
isotropic=False,
acquisition_function="EI",
n_acquire=num_iter,
seed=seed,
)
bayes_opt_PI = bo.BayesianOptimizer(
data,
x,
y,
normalize_y,
kernel,
isotropic=False,
acquisition_function="PI",
n_acquire=num_iter,
seed=seed,
)
bayes_opt_UCB = bo.BayesianOptimizer(
data,
x,
y,
normalize_y,
kernel,
isotropic=False,
acquisition_function="UCB",
n_acquire=num_iter,
seed=seed,
)
bayes_opt_PV = bo.BayesianOptimizer(
data,
x,
y,
normalize_y,
kernel,
isotropic=False,
acquisition_function="PV",
n_acquire=num_iter,
seed=seed,
)
bayes_opt_rand = bo.BayesianOptimizer(
data,
x,
y,
normalize_y,
kernel,
isotropic=False,
acquisition_function="random",
n_acquire=num_iter,
seed=seed,
)
# Run Bayesian Optimization for different acquisition functions
max_yield_history_EI = bayes_opt_EI.bayes_opt(df, num_init)[2]
max_yield_history_PI = bayes_opt_PI.bayes_opt(df, num_init)[2]
max_yield_history_UCB = bayes_opt_UCB.bayes_opt(df, num_init)[2]
max_yield_history_PV = bayes_opt_PV.bayes_opt(df, num_init)[2]
max_yield_history_random = bayes_opt_rand.bayes_opt(df, num_init)[2]
bo.plot_acquisition_comparison(
max_yield_history_EI,
max_yield_history_PI,
max_yield_history_UCB,
max_yield_history_PV,
max_yield_history_random,
kernel,
num_iter,
num_init,
data,
xi=args.xi,
kappa=args.kappa,
)
if __name__ == "__main__":
main()