-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
405 lines (312 loc) · 12.1 KB
/
main.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import argparse
import gym
import time
import sys
import os
import numpy as np
import pickle
from gym.spaces import Box, Discrete
import multiprocessing as mp
# default Net.train() parameters
DP = {
'sigma': 0.5,
'alpha': 0.5,
'npop': 50,
'show_every': 10,
'print_stats': True,
'render': True,
}
# activation function
def relu(z):
return np.maximum(0, z)
# NOTE: If you change this, make sure you deal with
# low values with cont action values.
activ = relu
def worker(env_id, nets, rewards):
N = 10 # how many runs to mean over
with gym.make(env_id) as env:
env = setup_wrappers(env)
try:
while x := nets.get():
j, net = x
reward = sum(net.evaluate(env) for _ in range(N)) / N
rewards.put((j, reward))
except ValueError as e:
print('queue closed?:', e)
print('worker exit')
def evaluate_many(agents, env, req=1, *args, **kwargs):
env_id = env.spec.id
procs = []
work = mp.Queue(len(agents))
rewards = mp.Queue(len(agents))
for core in range(mp.cpu_count()):
p = mp.Process(target=worker, args=(env_id, work, rewards))
p.start()
procs.append(p)
R = np.zeros(len(agents))
for j, net in enumerate(agents):
work.put((j, net)) # should be fine because of buffer
work.close()
for i in range(len(agents)):
j, r = rewards.get()
print(f'got reward {i}: {r}', end=(' '*20)+'\r')
R[j] = r
rewards.close()
# print('p.join()')
for p in procs:
p.terminate()
p.join()
# print('done')
return R
class Net:
def __init__(self, weights, biases):
assert len(weights) == len(biases)
self.weights = weights
self.biases = biases
# perhaps reconstructing layers is a bit hackish /shrug
self.layers = [weights[0].shape[1]] + [len(b) for b in biases]
@classmethod
def random(cls, layers):
biases = [
np.random.randn(x)
for x in layers[1:]
]
weights = [
np.random.randn(y, x)
for x, y in zip(layers[:-1], layers[1:])
]
return cls(weights, biases)
def save(self, filename):
with open(filename, 'wb') as f:
pickle.dump(self, f)
@classmethod
def load(cls, filename):
with open(filename, 'rb') as f:
net = pickle.load(f)
# keep compat with old networks if we update init.
return Net(net.weights, net.biases)
# this is terrible, don't do this, don't write code like this
# I really should find a better way to do this :l
@classmethod
def from_params(cls, params, layers):
total_biases = sum(layers[1:])
total_weights = sum(a*b for a,b in zip(layers[:-1], layers[1:]))
biases_arr = params[:total_biases]
weights_arr = params[total_biases:total_weights]
biases = []
weights = []
offset = 0
for l in range(len(layers) - 1):
size = layers[l+1]
biases.append(biases_arr[offset:offset+size])
offset += size
w_shapes = list(zip(layers[1:], layers[:-1]))
for l in range(len(layers) - 1):
size = w_shapes[l][0]*w_shapes[l][1]
weights.append(
params[offset:offset+size].reshape(w_shapes[l])
)
offset += size
return cls(weights, biases)
def params(self):
return np.concatenate(self.biases + [w.flatten() for w in self.weights])
def forward(self, a):
# flatten in case we have a matrix as input.
# a = a.reshape(self.weights[0].shape[1])
for w, b in zip(self.weights[:-1], self.biases[:-1]):
a = activ(np.dot(w, a) + b)
# don't run activation on the last neruon
a = np.dot(self.weights[-1], a) + self.biases[-1]
return a
def evaluate(self, env, render=False, sleep=0):
total_reward = 0
obs = env.reset()
done = False
while not done:
if render:
env.render()
time.sleep(sleep)
action = self.forward(obs)
if isinstance(env.action_space, Discrete):
action = np.argmax(action)
elif isinstance(env.action_space, Box):
action = action.reshape(env.action_space.shape)
obs, reward, done, _ = env.step(action)
total_reward += reward
# for mountaincar
# if obs[0] >= env.goal_position:
# total_reward += 100
return total_reward
def update_step(self, env, npop, sigma, alpha):
params = self.params()
noise = np.random.randn(npop, params.size)
R = evaluate_many(
[Net.from_params(params + sigma*noise[j], self.layers) for j in range(npop)],
req=10,
env=env,
render=False, sleep=0,
)
# R = np.zeros(npop)
# for j in range(npop):
# m_net = Net.from_params(params + sigma*noise[j], self.layers)
# R[j] = m_net.evaluate(env)
A = (R - R.mean()) / (R.std() or 1.0)
delta = np.dot(noise.T, A) / (npop*sigma)
params = params + delta*alpha
new = Net.from_params(params, self.layers)
self.biases = new.biases
self.weights = new.weights
def train(
self, env, generations,
print_stats=DP['print_stats'], render=DP['render'], show_every=DP['show_every'],
npop=DP['npop'], sigma=DP['sigma'], alpha=DP['alpha']
):
"""
sigma is noise standard deviation
alpha is learning rate
"""
for gen in range(generations):
self.update_step(env, npop, sigma, alpha)
# print current best reward
if print_stats:
reward = self.evaluate(
env, render=(render and (gen % show_every == 0)), sleep=0)
print(f'gen {gen} reward: {reward}')
def train_cma(
self, env, generations,
print_stats=DP['print_stats'], render=DP['render'], show_every=DP['show_every'],
npop=DP['npop'],
):
import mycma
def f(params, N=10):
net = Net.from_params(params, layers=self.layers)
return sum(net.evaluate(env, sleep=0) for _ in range(N)) / N
for gen, params in enumerate(mycma.train(f, self.params(), npop=npop, iterations=generations)):
# update our net
new = Net.from_params(params, self.layers)
self.biases = new.biases
self.weights = new.weights
if print_stats:
if render and (gen % show_every == 0):
self.evaluate(env, render=True, sleep=0)
reward = f(params)
print(f'gen {gen} reward: {reward}')
def train_pycma(
self, env, generations,
print_stats=DP['print_stats'], render=DP['render'], show_every=DP['show_every'],
npop=DP['npop'],
):
import cma
def f(params, N=10):
net = Net.from_params(params, layers=self.layers)
return sum(net.evaluate(env, sleep=0) for _ in range(N)) / N
es = cma.CMAEvolutionStrategy(self.params(), 1)
es.sp.popsize = npop
for gen in range(generations):
solutions = es.ask()
R = [-f(x) for x in solutions]
es.tell(solutions, R)
params = es.best.x
# update our net
new = Net.from_params(params, self.layers)
self.biases = new.biases
self.weights = new.weights
if print_stats:
es.disp()
def space_to_n(space):
if isinstance(space, Box):
return np.prod(space.shape)
elif isinstance(space, Discrete):
return space.n
else:
raise NotImplemented
def setup_wrappers(env):
obs_shape = env.observation_space.shape
is_image = len(obs_shape) == 3
if is_image:
from gym.wrappers import GrayScaleObservation
from gym.wrappers import FlattenObservation
from gym.wrappers import ResizeObservation
env = GrayScaleObservation(env)
# env = ResizeObservation(env, (obs_shape[0]//3, obs_shape[0]//3))
env = FlattenObservation(env)
return env
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--env', help='the gym enviorment to use', default='CartPole-v1')
parser.add_argument('--save', help='save filename to use (saved in ./nets)')
parser.add_argument('--eval', help='evaluate network', default=False, action='store_true')
parser.add_argument('--train', help='train network', default=False, action='store_true')
parser.add_argument('--npop', help='population count', type=int, default=DP['npop'])
parser.add_argument('--sigma', help='noise standard deviation', type=float, default=DP['sigma'])
parser.add_argument('--alpha', help='learning rate', type=float, default=DP['alpha'])
parser.add_argument('--layers', help='hidden layers', nargs='+', type=int, default=[16])
parser.add_argument('--gen', help='number of generations', type=int, default=100)
parser.add_argument('--nbest', help='nbest for CMA-ES', type=int, default=10)
parser.add_argument('--cma', help='use CMA-ES', default=False, action='store_true')
parser.add_argument('--pycma', help='use pycma', default=False, action='store_true')
parser.add_argument(
'--show-every',
help='how many generations between rendering network in training',
type=int,
default=DP['show_every'],
)
args = parser.parse_args()
save_file = os.path.join('./nets', args.save or f'{args.env}-{"x".join(map(str, args.layers))}.pkl')
if not args.train and not args.eval:
# neither supplied, set both to True
args.train = True
args.eval = True
with gym.make(args.env) as env:
env = setup_wrappers(env)
input_layer = space_to_n(env.observation_space)
output_layer = space_to_n(env.action_space)
layers = (input_layer, *args.layers, output_layer)
print(f'Layers: {layers}')
print(f'Env: {env}')
if os.path.exists(save_file):
net = Net.load(save_file)
# ensure compat with env
assert net.layers[0] == input_layer, f'got input layer {net.layers[0]} want {input_layer}'
assert net.layers[-1] == output_layer, f'got output layer {net.layers[-1]} want {output_layer}'
print(f'Loaded network from {save_file}')
else:
net = Net.random(layers)
print('Initialized random network')
if args.train:
print('Training network...')
try:
if args.cma:
net.train_cma(
env, args.gen,
render=args.eval, print_stats=True, show_every=args.show_every,
npop=args.npop
)
elif args.pycma:
net.train_pycma(
env, args.gen,
render=args.eval, print_stats=True, show_every=args.show_every,
npop=args.npop
)
else:
net.train(
env, args.gen,
render=args.eval, print_stats=True, show_every=args.show_every,
npop=args.npop, sigma=args.sigma, alpha=args.alpha,
)
# AssertionError raised by some envs when interrupted.
except (KeyboardInterrupt, AssertionError):
pass
finally:
net.save(save_file)
print(f'Saved network to {save_file}')
if args.eval:
print('Evaluating network...')
try:
while True:
print('reward', net.evaluate(env, render=True))
except KeyboardInterrupt:
pass
print('\nDone!')
if __name__ == '__main__':
main()