forked from angusfung/population-based-training
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toy_example.py
161 lines (120 loc) · 5.21 KB
/
toy_example.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
154
155
156
157
158
159
160
161
#!/usr/bin/env python
import numpy as np
import operator
import matplotlib.pyplot as plt
class Worker(object):
def __init__(self, idx, obj, surrogate_obj, h, theta, pop_score, pop_params):
self.idx = idx
self.obj = obj
self.surrogate_obj = surrogate_obj
self.theta = theta
self.h = h
self.score = 0 # current score
self.pop_score = pop_score # reference to population statistics
self.pop_params = pop_params
# for plotting
self.theta_history = []
self.loss_history = []
self.rms = 0 # for rmsprop
self.update() # intialize population
def step(self, vanilla=False, rmsprop=False, Adam=False):
"""one step of GD"""
decay_rate = 0.9
alpha = 0.01
eps = 1e-5
d_surrogate_obj = -2.0 * self.h * self.theta
if vanilla:
self.theta += d_surrogate_obj * alpha # ascent to maximize function
else:
self.rms = decay_rate * self.rms + (1-decay_rate) * d_surrogate_obj**2
self.theta += alpha * d_surrogate_obj / (np.sqrt(self.rms) + eps)
def eval(self):
"""metric we want to optimize e.g mean episodic return or validation set performance"""
self.score = self.obj(self.theta)
return self.score
def exploit(self):
"""copy weights, hyperparams from the member in the population with the highest performance"""
best_worker_idx = max(self.pop_score.items(), key=operator.itemgetter(1))[0]
if best_worker_idx != self.idx:
best_worker_theta, best_worker_h = self.pop_params[best_worker_idx]
self.theta = np.copy(best_worker_theta)
# self.h = np.copy(best_worker_h)
return True
return False
def explore(self):
"""perturb hyperparaters with noise from a normal distribution"""
eps = np.random.randn(*self.h.shape) * 0.1
self.h += eps
def update(self):
"""update worker stats in global dictionary"""
self.pop_score[self.idx] = self.score
self.pop_params[self.idx] = (np.copy(self.theta), np.copy(self.h)) # np arrays are mutable
self.theta_history.append(np.copy(self.theta))
self.loss_history.append(self.score)
def run(steps=200, explore=True, exploit=True):
# Q and Q_hat, as per fig. 2: https://arxiv.org/pdf/1711.09846.pdf
obj = lambda theta: 1.2 - np.sum(theta**2)
surrogate_obj = lambda theta, h: 1.2 - np.sum(h*theta**2)
pop_score = {} # score for all members
pop_params = {} # params for all members
# initialize two workers
population = [
Worker(1, obj, surrogate_obj, np.array([1.,0.]), np.array([0.9, 0.9]), pop_score, pop_params),
Worker(2, obj, surrogate_obj, np.array([0.,1.]), np.array([0.9, 0.9]), pop_score, pop_params),
]
for step in range(steps):
for worker in population:
worker.step(vanilla=True) # one step of GD
worker.eval() # evaluate current model
if step % 10 == 0:
if explore and exploit:
do_explore = worker.exploit()
if do_explore:
worker.explore()
elif explore and not exploit:
worker.explore()
elif not explore and exploit:
worker.exploit()
elif not explore and not exploit:
pass
worker.update()
return population
def plot_loss(run, i, steps, title):
plt.subplot(2,4,i)
plt.plot(run[0].loss_history, color='b', lw=0.7)
plt.plot(run[1].loss_history, color='r', lw=0.7)
plt.axhline(y=1.2, linestyle='dotted', color='k')
axes = plt.gca()
axes.set_xlim([0,steps])
axes.set_ylim([0.0, 1.21])
plt.title(title)
plt.xlabel('Step')
plt.ylabel('Q')
def plot_theta(run, i, steps, title):
x_b = [_[0] for _ in run[0].theta_history]
y_b = [_[1] for _ in run[0].theta_history]
x_r = [_[0] for _ in run[1].theta_history]
y_r = [_[1] for _ in run[1].theta_history]
plt.subplot(2,4,i)
plt.scatter(x_b, y_b, color='b', s=2)
plt.scatter(x_r, y_r, color='r', s=2)
plt.title(title)
plt.xlabel('theta0')
plt.ylabel('theta1')
def main():
steps = 200
run1 = run(steps=steps)
run2 = run(steps=steps, exploit=False)
run3 = run(steps=steps, explore=False)
run4 = run(steps=steps, exploit=False, explore=False)
plot_loss(run1, 3, steps=steps, title='PBT')
plot_loss(run2, 4, steps=steps, title='Explore only')
plot_loss(run3, 7, steps=steps, title='Exploit only')
plot_loss(run4, 8, steps=steps, title='Grid Search')
plot_theta(run1, 1, steps=steps, title='PBT')
plot_theta(run2, 2, steps=steps, title='Explore only')
plot_theta(run3, 5, steps=steps, title='Exploit only')
plot_theta(run4, 6, steps=steps, title='Grid Search')
plt.show()
if __name__ == '__main__':
main()