-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_gpucb.py
More file actions
73 lines (61 loc) · 1.82 KB
/
example_gpucb.py
File metadata and controls
73 lines (61 loc) · 1.82 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
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from thompson_sampling import GaussianProcessesStrategy, ThompsonSampling
def fitness(params: np.ndarray):
reward = (
np.exp(-(params ** 2)) + np.cos(params) + np.random.normal(scale=0.1, size=params.shape)
)
return reward, params
def on_update(epoch, strategy, last_epoch):
if epoch % 10 == 0:
print("Epoch {} - Current sample {}".format(epoch, strategy.sample()))
if last_epoch:
print("SEARCH DONE")
print(strategy.sample())
return False
def plot1d(strategy):
data = np.exp(-(strategy.X_grid ** 2)) + np.cos(strategy.X_grid)
plt.plot(strategy.X_grid, data, label="Ground truth")
plt.plot(strategy.X_grid, strategy.mu, "r-*", label="mu")
plt.plot(strategy.X_grid, strategy.sigma, label="sigma")
plt.legend()
plt.show()
def plot(strategy):
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_wireframe(
strategy.mesh[0],
strategy.mesh[1],
strategy.mu.reshape(strategy.mesh[0].shape),
alpha=0.5,
color="g",
)
ax.plot_wireframe(
strategy.mesh[0],
strategy.mesh[1],
fitness(strategy.mesh)[0],
alpha=0.5,
color="b",
)
ax.scatter(
[x[0] for x in strategy.X],
[x[1] for x in strategy.X],
strategy.y,
c="r",
marker="o",
alpha=1.0,
)
plt.show()
plt.savefig("fig_%02d.png" % len(strategy.X))
if __name__ == "__main__":
strategy = GaussianProcessesStrategy(1, np.arange(-10, 10, 0.1), 1.0)
ts = ThompsonSampling(
thompson_strategy=strategy,
epochs=50,
fitness_function=fitness,
callbacks={"on_update": on_update},
num_processors=10,
)
strategy = ts.run()
plot1d(strategy)