-
Notifications
You must be signed in to change notification settings - Fork 0
/
toyBOmodel.py
44 lines (40 loc) · 1.41 KB
/
toyBOmodel.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
from sklearn.gaussian_process import GaussianProcessRegressor
self.GP = GaussianProcessRegressor(...)
# 定义acquisition function
def PI(x, gp, y_max, xi):
mean, std = gp.predict(x, return_std=True)
z = (mean - y_max - xi)/std
return norm.cdf(z)
def EI(x, gp, y_max, xi):
mean, std = gp.predict(x, return_std=True)
a = (mean - y_max - xi)
z = a / std
return a * norm.cdf(z) + std * norm.pdf(z)
def UCB(x, gp, kappa):
mean, std = gp.predict(x, return_std=True)
return mean + kappa * std
# 寻找acquisition function最大的对应解
def acq_max(ac, gp, y_max, bounds, random_state, n_warmup=10000):
# 随机采样选择最大值
x_tries = np.random.RandomState(random_state).uniform(bounds[:, 0], bounds[:, 1],
size=(n_warmup, bounds.shape[0]))
ys = ac(x_tries, gp=gp, y_max=y_max)
x_max = x_tries[ys.argmax()]
max_acq = ys.max()
return x_max
if __name__ == '__main__':
while iteration < n_iter:
# 更新高斯过程的后验分布
self.GP.fit(X, y)
# 根据acquisition函数计算下一个试验点
suggestion = acq_max(
ac=utility_function,
gp=self.GP,
y_max=y.max(),
bounds=self.bounds,
random_state=self.random_state
)
# 进行试验(采样),更新观测点集合
X.append(suggestion)
y.append(target_func(suggestion))
iteration += 1