-
Notifications
You must be signed in to change notification settings - Fork 3
/
ppo2_kwng2.py
642 lines (542 loc) · 26.5 KB
/
ppo2_kwng2.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
import os
import time
import joblib
import numpy as np
import os.path as osp
import tensorflow as tf
from baselines import logger
from collections import deque
from baselines.common import explained_variance
from baselines.a2c.utils import discount_with_dones
from kwng_tf import KWNG
from kwng_tf_mean import KWNG as KWNG_mean
from kwng_tf_long import KWNG as KWNG_long
from gaussian_tf import Gaussian
import pdb
class Model(object):
def __init__(self, *, policy, ob_space, ac_space, nbatch_act, nbatch_train,
nsteps, ent_coef, vf_coef, max_grad_norm, D, omega, bias, M, gamma_W, wasserstein_coeff, KWNG_estimator, base_opt):
# D: number of random features
# omega: [D, actdim] random feature vector
# bias: [D, 1] random featture vector
# M: number of actions per state
# gamma_W: gamma for wasserstein distance
"""
need placeholder for old loss
"""
act_dim = ac_space.high.size
sess = tf.get_default_session()
self.sess = sess
self.KWNG_estimator = KWNG_estimator
self.clip_grad = max_grad_norm is not None
self.clip_value = max_grad_norm
self.old_loss = tf.Variable(-1., name='old_loss', trainable=False, dtype=tf.float32)
self.dot_prod = tf.Variable(0., name='dot_prod', trainable=False, dtype=tf.float32)
self.reduction_coeff = tf.Variable(0.85, name='reduction_coeff', trainable=False, dtype=tf.float32)#reduction_coeff
self.dumping_freq = 5 ##dumping_freq
self.min_red = 0.25 #min_red
self.max_red = 0.75 #max_red
self.eps_min = 1e-10
self.eps_max = 1e5
self.dumping_counter = tf.Variable(0, name='dumping_counter', trainable=False, dtype=tf.int32) #0
self.reduction_factor = tf.Variable(0., name='reduction_factor', trainable=False, dtype=tf.float32)
act_model = policy(sess, ob_space, ac_space, nbatch_act, 1, reuse=False)
train_model = policy(sess, ob_space, ac_space, nbatch_train, nsteps, reuse=True)
A = train_model.pdtype.sample_placeholder([None])
ADV = tf.placeholder(tf.float32, [None])
R = tf.placeholder(tf.float32, [None])
OLDNEGLOGPAC = tf.placeholder(tf.float32, [None])
OLDVPRED = tf.placeholder(tf.float32, [None])
LR = tf.placeholder(tf.float32, [])
CLIPRANGE = tf.placeholder(tf.float32, [])
neglogpac = train_model.pd.neglogp(A)
entropy = tf.reduce_mean(train_model.pd.entropy())
vpred = train_model.vf
vpredclipped = OLDVPRED + tf.clip_by_value(train_model.vf - OLDVPRED, - CLIPRANGE, CLIPRANGE)
vf_losses1 = tf.square(vpred - R)
vf_losses2 = tf.square(vpredclipped - R)
vf_loss = .5 * tf.reduce_mean(tf.maximum(vf_losses1, vf_losses2))
ratio = tf.exp(OLDNEGLOGPAC - neglogpac)
pg_losses = -ADV * ratio
pg_losses2 = -ADV * tf.clip_by_value(ratio, 1.0 - CLIPRANGE, 1.0 + CLIPRANGE)
pg_loss = tf.reduce_mean(tf.maximum(pg_losses, pg_losses2))
approxkl = .5 * tf.reduce_mean(tf.square(neglogpac - OLDNEGLOGPAC))
clipfrac = tf.reduce_mean(tf.to_float(tf.greater(tf.abs(ratio - 1.0), CLIPRANGE)))
# setup Wasserstein trust region here
A_reparameterized = train_model.a_reparameterized
BETA_1 = tf.placeholder(tf.float32, [None, D])
BETA_2 = tf.placeholder(tf.float32, [None, D])
def get_random_features(x):
nsample = x.shape[1]
return np.cos(np.dot(x, omega.T) + bias.T) * np.sqrt(2. / D)
def get_random_features_tf(x):
omega_tf = tf.constant(omega.T, dtype=tf.float32)
bias_tf = tf.constant(bias.T, dtype=tf.float32)
return tf.cos(tf.matmul(x, omega_tf) + bias_tf) * np.sqrt(2. / D)
# random feature for parameterized action
A_reparameterized_random_features = tf.reshape(get_random_features_tf(tf.reshape(A_reparameterized, [-1, act_dim])), [-1, M, D])
# placeholder for reference actions
target_actions = tf.placeholder(tf.float32, [None, M, act_dim])
target_actions_random_features = get_random_features_tf(tf.reshape(target_actions, [-1, act_dim]))
target_actions_random_features = tf.reshape(target_actions_random_features, [-1, M, D])
distance = tf.reduce_sum(tf.square(target_actions - A_reparameterized), axis=-1)
test_1 = tf.reduce_sum(tf.expand_dims(BETA_1, axis=1) * A_reparameterized_random_features, axis=-1)
test_2 = tf.reduce_sum(tf.expand_dims(BETA_2, axis=1) * target_actions_random_features, axis=-1)
# test_1, test_2, distance should have the same dimension [nbatch, M]
weight = test_1 + test_2 - distance
wasserstein_distances = test_1 + test_2 - gamma_W * tf.exp(weight / gamma_W)
wasserstein_distance = tf.reduce_mean(wasserstein_distances)
loss = pg_loss - entropy * ent_coef + vf_loss * vf_coef + wasserstein_distance * wasserstein_coeff
## adjust epsilon ## change reduction factor and epsilon, compute conditioning matrix
dumping_ops = self.dumping(loss, LR)
act_embed = tf.reshape(A_reparameterized, [-1, M*act_dim])
v_embed = tf.expand_dims(vpred, -1)
output = tf.concat([act_embed, v_embed], axis=-1)
print ("EMBEDDING SHAPE: ", output.get_shape().as_list())
self.KWNG_estimator.compute_cond_matrix(train_model, output) ###PICKED behav policy embedding###
####################
with tf.variable_scope('model'):
params = tf.trainable_variables()
grads = tf.gradients(loss, params)
## compute natural gradient ##
g = [tf.reshape(gr, [-1]) for gr in grads]
g = tf.concat(g, axis=0) # flattened gradients
cond_g = self.KWNG_estimator.compute_natural_gradient(g)
# if the dot product is negative, just use the euclidean grad
self.dot_prod = tf.assign(self.dot_prod, tf.reduce_sum(g * cond_g))
cond_g = tf.cond(self.dot_prod <= 0, lambda: g, lambda: cond_g)
# grad clipping by norm
if self.clip_grad:
cond_g, self.dot_prod = self.clip_gradient(cond_g)
# save old loss
self.old_loss = tf.assign(self.old_loss, loss)
# reshape gradients
start = 0; cond_g_list = [];
for gr in grads:
gr_shape = gr.get_shape().as_list()
gr_size = np.prod(gr_shape)
cond_g_list.append(tf.reshape(cond_g[start : start + gr_size], gr_shape))
start += gr_size
grads = cond_g_list
##############################
grads = list(zip(grads, params))
opt_dict = {
'adam': tf.train.AdamOptimizer(learning_rate=LR, epsilon=1e-5),
'momentum': tf.train.MomentumOptimizer(learning_rate=LR, momentum=0.9),
'sgd': tf.train.GradientDescentOptimizer(learning_rate=LR)
}
trainer = opt_dict[base_opt]
_train = trainer.apply_gradients(grads)
self.beta_1 = np.zeros([nbatch_train, D])
self.beta_2 = np.zeros([nbatch_train, D])
def clear_beta():
self.beta_1 = np.zeros([nbatch_train, D])
self.beta_2 = np.zeros([nbatch_train, D])
def train(lr, cliprange, obs, returns, masks, actions, values, neglogpacs, oldbatchactions, states=None, T=1, lr_beta=0.0):
# oldbatchactions: old actions executed by old policy, M action per state
# beta_1: param for test function 1
# beta_2: param for test function 2
advs = returns - values
advs = (advs - advs.mean()) / (advs.std() + 1e-8)
# update test function
# get random features for oldbatchactions
oldbatchactions_randomfeatures = np.reshape(get_random_features(np.reshape(oldbatchactions, [-1, act_dim])), [-1, M, D])
for t in range(T):
# sample new actions
newbatchactions = sess.run(A_reparameterized, feed_dict={train_model.X:obs}) # [nbatch, M, act_dim]
# get random features
newbatchactions_randomfeatures = np.reshape(get_random_features(np.reshape(newbatchactions, [-1, act_dim])), [-1, M, D])
# distance
dist = np.sum((newbatchactions - oldbatchactions)**2, axis=-1) # [nbatch, M]
# test functions
weight1 = np.sum(np.expand_dims(self.beta_1, axis=1) * newbatchactions_randomfeatures, axis=-1) # [nbatch, M]
weight2 = np.sum(np.expand_dims(self.beta_2, axis=1) * oldbatchactions_randomfeatures, axis=-1) # [nbatch, M]
coeff = 1.0 - np.exp((weight1 + weight2 - dist) / gamma_W) # [nbatch, M]
# compute gradient
update1 = np.mean(np.expand_dims(coeff, axis=2) * newbatchactions_randomfeatures, axis=1) # [nbatch, D]
update2 = np.mean(np.expand_dims(coeff, axis=2) * oldbatchactions_randomfeatures, axis=1) # [nbatch, D]
# update beta
self.beta_1 += lr_beta * update1
self.beta_2 += lr_beta * update2
# update policy
td_map = {train_model.X:obs, A:actions, ADV:advs, R:returns, LR:lr,
CLIPRANGE:cliprange, OLDNEGLOGPAC:neglogpacs, OLDVPRED:values, BETA_1:self.beta_1, BETA_2:self.beta_2, target_actions:oldbatchactions}
if states is not None:
td_map[train_model.S] = states
td_map[train_model.M] = masks
return sess.run(
[pg_loss, vf_loss, entropy, approxkl, clipfrac] + list(dumping_ops) + [self.dot_prod, self.old_loss, _train], #dumping_counter_op,
td_map
)[:-1]
self.loss_names = ['policy_loss', 'value_loss', 'policy_entropy', 'approxkl', 'clipfrac']
def save(save_path):
ps = sess.run(params)
joblib.dump(ps, save_path)
def load(load_path):
loaded_params = joblib.load(load_path)
restores = []
for p, loaded_p in zip(params, loaded_params):
restores.append(p.assign(loaded_p))
sess.run(restores)
self.train = train
self.clear_beta = clear_beta
self.train_model = train_model
self.act_model = act_model
self.step = act_model.step
self.value = act_model.value
self.initial_state = act_model.initial_state
self.save = save
self.load = load
self.sess = sess
tf.global_variables_initializer().run(session=sess) #pylint: disable=E1101
def dumping(self, loss, lr):
# compute reduction ratio
red = 2. * (self.old_loss - loss) / (lr * self.dot_prod)
new_reduction_factor = tf.cond(
tf.logical_and(self.old_loss > -1, red > self.reduction_factor),
lambda: red,
lambda: self.reduction_factor)
#self.reduction_factor = new_reduction_factor
reduction_factor_op = tf.assign(self.reduction_factor, new_reduction_factor)
# increment dumping counter
self.dumping_counter = tf.assign(self.dumping_counter, self.dumping_counter + 1)
base_pred = tf.logical_and(self.old_loss > -1, tf.equal(tf.mod(self.dumping_counter, self.dumping_freq), 0))
pred1 = tf.logical_and(base_pred, tf.logical_and(self.reduction_factor < self.min_red, self.KWNG_estimator.eps < self.eps_max))
pred2 = tf.logical_and(base_pred, tf.logical_and(self.reduction_factor > self.max_red, self.KWNG_estimator.eps > self.eps_min))
# if both conditions in pred1 are true, rescale eps like this
new_eps = tf.cond(
tf.cast(pred1, tf.bool),
lambda: self.KWNG_estimator.eps / self.reduction_coeff,
lambda: self.KWNG_estimator.eps)
# if both conditions in pred2 are true, rescale eps like this instead
new_eps = tf.cond(
tf.cast(pred2, tf.bool),
lambda: self.KWNG_estimator.eps * self.reduction_coeff,
lambda: self.KWNG_estimator.eps)
# if the base predicate is true at all, reset the reduction factor to 0
new_reduction_factor = tf.cond(
tf.cast(base_pred, tf.bool),
lambda: 0.,
lambda: new_reduction_factor)
self.reduction_factor = tf.assign(self.reduction_factor, new_reduction_factor)
self.KWNG_estimator.eps = tf.assign(self.KWNG_estimator.eps, new_eps)
return self.reduction_factor, self.dumping_counter, self.KWNG_estimator.eps
def clip_gradient(self, cond_g):
norm_grad = tf.norm(cond_g)
clip_coef = self.clip_value / (norm_grad + 1e-6)
result, new_dot_prod = tf.cond(clip_coef < 1.0,
lambda: (cond_g / norm_grad, self.dot_prod / norm_grad),
lambda: (cond_g, self.dot_prod))
return result, tf.assign(self.dot_prod, new_dot_prod)
class Runner(object):
def __init__(self, *, env, model, nsteps, gamma, lam, gae):
self.env = env
self.model = model
nenv = env.num_envs
self.obs = np.zeros((nenv,) + env.observation_space.shape, dtype=model.train_model.X.dtype.name)
self.obs[:] = env.reset()
self.gamma = gamma
self.lam = lam
self.nsteps = nsteps
self.states = model.initial_state
self.dones = [False for _ in range(nenv)]
self.gae = gae
def run(self):
mb_obs, mb_rewards, mb_actions, mb_values, mb_dones, mb_neglogpacs = [],[],[],[],[],[]
mb_batchactions = []
mb_states = self.states
epinfos = []
for _ in range(self.nsteps):
actions, values, self.states, neglogpacs, batchactions = self.model.step(self.obs, self.states, self.dones)
mb_obs.append(self.obs.copy())
mb_actions.append(actions)
mb_values.append(values)
mb_neglogpacs.append(neglogpacs)
mb_dones.append(self.dones)
mb_batchactions.append(batchactions)
self.obs[:], rewards, self.dones, infos = self.env.step(actions)
for info in infos:
maybeepinfo = info.get('episode')
if maybeepinfo: epinfos.append(maybeepinfo)
mb_rewards.append(rewards)
#batch of steps to batch of rollouts
mb_obs = np.asarray(mb_obs, dtype=self.obs.dtype)
mb_rewards = np.asarray(mb_rewards, dtype=np.float32)
mb_actions = np.asarray(mb_actions)
mb_values = np.asarray(mb_values, dtype=np.float32)
mb_neglogpacs = np.asarray(mb_neglogpacs, dtype=np.float32)
mb_dones = np.asarray(mb_dones, dtype=np.bool)
mb_batchactions = np.asarray(mb_batchactions, dtype=np.float32)
last_values = self.model.value(self.obs, self.states, self.dones)
#discount/bootstrap off value fn
if self.gae is True:
mb_returns = np.zeros_like(mb_rewards)
mb_advs = np.zeros_like(mb_rewards)
lastgaelam = 0
for t in reversed(range(self.nsteps)):
if t == self.nsteps - 1:
nextnonterminal = 1.0 - self.dones
nextvalues = last_values
else:
nextnonterminal = 1.0 - mb_dones[t+1]
nextvalues = mb_values[t+1]
delta = mb_rewards[t] + self.gamma * nextvalues * nextnonterminal - mb_values[t]
mb_advs[t] = lastgaelam = delta + self.gamma * self.lam * nextnonterminal * lastgaelam
mb_returns = mb_advs + mb_values
else:
mb_returns = discount_with_dones(mb_rewards, mb_dones, self.gamma)
mb_returns = np.array(mb_returns)
return (*map(sf01, (mb_obs, mb_returns, mb_dones, mb_actions, mb_values, mb_neglogpacs, mb_batchactions)),
mb_states, epinfos)
def sf01(arr):
"""
swap and then flatten axes 0 and 1
"""
s = arr.shape
return arr.swapaxes(0, 1).reshape(s[0] * s[1], *s[2:])
def constfn(val):
def f(_):
return val
return f
def learn(*, policy, env, nsteps, total_timesteps, ent_coef, lr,
vf_coef=0.5, max_grad_norm=0.5, gamma=0.99, lam=0.95,
log_interval=10, nminibatches=4, noptepochs=4, cliprange=0.2,
save_interval=0, callback=None, D=None, M=None, gamma_W=None, wasserstein_coeff=None,
T=None, lr_beta=None, sigma=None, gae=None, kwng_args=None, base_opt='adam', num_kwng_basis=5, kwng_method='kwng',
lr_decay=-1, decay_factor=4.):
assert gae is not None
if isinstance(lr, float): lr = constfn(lr)
else: assert callable(lr)
if isinstance(cliprange, float): cliprange = constfn(cliprange)
else: assert callable(cliprange)
total_timesteps = int(total_timesteps)
nenvs = env.num_envs
ob_space = env.observation_space
ac_space = env.action_space
nbatch = nenvs * nsteps
nbatch_train = nbatch // nminibatches
# generate random features with rbf kernel
omega = np.random.randn(D, ac_space.high.size) * 1.0 / sigma
bias = np.random.rand(D, 1) * 2 * np.pi
# make model function
### define kwng estimator ###
kernel = Gaussian(1, 0., dtype=tf.float32, device='gpu') #kwng_args['log_bandwidth']
kwng_dict = {
'kwng': KWNG,
'kwng_mean': KWNG_mean,
'kwng_long': KWNG_long
}
estimator = kwng_dict[kwng_method](kernel,
eps=1e-5,
num_basis=num_kwng_basis,
with_diag_mat=1)
make_model = lambda : Model(policy=policy, ob_space=ob_space, ac_space=ac_space, nbatch_act=nenvs, nbatch_train=nbatch_train,
nsteps=nsteps, ent_coef=ent_coef, vf_coef=vf_coef, KWNG_estimator=estimator,
max_grad_norm=max_grad_norm, D=D, omega=omega, bias=bias, M=M, gamma_W=gamma_W, wasserstein_coeff=wasserstein_coeff, base_opt=base_opt)
# make model
model = make_model()
runner = Runner(env=env, model=model, nsteps=nsteps, gamma=gamma, lam=lam, gae=gae)
# save model using saver
saver = tf.train.Saver()
if callback.arg['continue-train'] == 1:
saver.restore(model.sess, callback.directory + '/model/model')
elif callback.arg['continue-train'] == 0:
pass
else:
raise NotImplementedError
epinfobuf = deque(maxlen=100)
tfirststart = time.time()
nupdates = total_timesteps//nbatch
for update in range(1, nupdates+1):
assert nbatch % nminibatches == 0
nbatch_train = nbatch // nminibatches
tstart = time.time()
frac = 1.0 - (update - 1.0) / nupdates
lrnow = lr(frac)
if lr_decay != -1 and update % lr_decay == 0: lrnow /= decay_factor;
cliprangenow = cliprange(frac)
obs, returns, masks, actions, values, neglogpacs, oldbatchactions, states, epinfos = runner.run() #pylint: disable=E0632
epinfobuf.extend(epinfos)
mblossvals = []
# clear betas
model.clear_beta()
if states is None: # nonrecurrent version
inds = np.arange(nbatch)
for _ in range(noptepochs):
np.random.shuffle(inds)
for start in range(0, nbatch, nbatch_train):
end = start + nbatch_train
mbinds = inds[start:end]
slices = (arr[mbinds] for arr in (obs, returns, masks, actions, values, neglogpacs, oldbatchactions))
mblossvals.append(model.train(lrnow, cliprangenow, *slices, T=T, lr_beta=lr_beta))
else: # recurrent version
raise NotImplementedError
assert nenvs % nminibatches == 0
envsperbatch = nenvs // nminibatches
envinds = np.arange(nenvs)
flatinds = np.arange(nenvs * nsteps).reshape(nenvs, nsteps)
envsperbatch = nbatch_train // nsteps
for _ in range(noptepochs):
np.random.shuffle(envinds)
for start in range(0, nenvs, envsperbatch):
end = start + envsperbatch
mbenvinds = envinds[start:end]
mbflatinds = flatinds[mbenvinds].ravel()
slices = (arr[mbflatinds] for arr in (obs, returns, masks, actions, values, neglogpacs))
mbstates = states[mbenvinds]
mblossvals.append(model.train(lrnow, cliprangenow, *slices, mbstates))
lossvals = np.mean(mblossvals, axis=0)
tnow = time.time()
fps = int(nbatch / (tnow - tstart))
if update % log_interval == 0 or update == 1:
ev = explained_variance(values, returns)
logger.logkv("serial_timesteps", update*nsteps)
logger.logkv("nupdates", update)
logger.logkv("total_timesteps", update*nbatch)
logger.logkv("fps", fps)
logger.logkv("explained_variance", float(ev))
logger.logkv('eprewmean', safemean([epinfo['r'] for epinfo in epinfobuf]))
logger.logkv('eplenmean', safemean([epinfo['l'] for epinfo in epinfobuf]))
#logger.logkv('xpos', safemean([epinfo['pos'] for epinfo in epinfobuf]))
logger.logkv('time_elapsed', tnow - tfirststart)
for (lossval, lossname) in zip(lossvals, model.loss_names):
logger.logkv(lossname, lossval)
if lossname == 'policy_entropy':
policy_entropy = lossval
if lossname == 'approxkl':
approxkl = lossval
logger.dumpkvs()
if save_interval and (update % save_interval == 0 or update == 1) and logger.get_dir():
checkdir = osp.join(logger.get_dir(), 'checkpoints')
os.makedirs(checkdir, exist_ok=True)
savepath = osp.join(checkdir, '%.5i'%update)
print('Saving to', savepath)
model.save(savepath)
if callback is not None:
eprewmean = safemean([epinfo['r'] for epinfo in epinfobuf])
eplenmean = safemean([epinfo['l'] for epinfo in epinfobuf])
total_timesteps_sofar = update * nbatch
callback(locals(), globals())
env.close()
def safemean(xs):
return np.nan if len(xs) == 0 else np.mean(xs)
def render_evaluate(*, policy, env, nsteps, total_timesteps, ent_coef, lr,
vf_coef=0.5, max_grad_norm=0.5, gamma=0.99, lam=0.95,
log_interval=10, nminibatches=4, noptepochs=4, cliprange=0.2,
save_interval=0, callback=None):
logger.info('rendering')
if isinstance(lr, float): lr = constfn(lr)
else: assert callable(lr)
if isinstance(cliprange, float): cliprange = constfn(cliprange)
else: assert callable(cliprange)
total_timesteps = int(total_timesteps)
nenvs = 1
ob_space = env.observation_space
ac_space = env.action_space
nbatch = nenvs * nsteps
nbatch_train = nbatch // nminibatches
make_model = lambda : Model(policy=policy, ob_space=ob_space, ac_space=ac_space, nbatch_act=nenvs, nbatch_train=nbatch_train,
nsteps=nsteps, ent_coef=ent_coef, vf_coef=vf_coef,
max_grad_norm=max_grad_norm)
# make model
model = make_model()
# save model using saver
saver = tf.train.Saver()
logger.info('Restore trained model')
saver.restore(model.sess, callback.model_dir)
print('loading model success')
# load obs_rms
import pickle
with open(callback.directory + '/ob_rms.pkl', 'rb') as output:
ob_rms = pickle.load(output)
print('loading mean', ob_rms.mean)
print('loading var', ob_rms.var)
def filter(ob):
ob = np.clip((ob - ob_rms.mean) / np.sqrt(ob_rms.var + 1e-8), -10, 10)
return ob
obsdict = []
assert len(env.venv.envs) == 1
for e in range(100):
rsum = 0
obsrecord = []
done = False
obs = env.reset()
obs = filter(obs)
env.venv.envs[0].render()
while not done:
actions, values, _, neglogpacs = model.step(obs)
newobs, r, done, _ = env.step(actions)
obsrecord.append(obs)
obs = newobs
obs = filter(obs)
env.venv.envs[0].render()
rsum += r
print('total rewards', rsum)
if callback is not None:
pass
callback(locals(), globals())
def collect_samples_evaluate(*, policy, env, nsteps, total_timesteps, ent_coef, lr,
vf_coef=0.5, max_grad_norm=0.5, gamma=0.99, lam=0.95,
log_interval=10, nminibatches=4, noptepochs=4, cliprange=0.2,
save_interval=0, callback=None):
logger.info('rendering')
if isinstance(lr, float): lr = constfn(lr)
else: assert callable(lr)
if isinstance(cliprange, float): cliprange = constfn(cliprange)
else: assert callable(cliprange)
total_timesteps = int(total_timesteps)
nenvs = 1
ob_space = env.observation_space
ac_space = env.action_space
nbatch = nenvs * nsteps
nbatch_train = nbatch // nminibatches
### define kwng estimator ###
kernel = Gaussian(1, args['log_bandwidth'], dtype=tr.float32, device=device)
estimator = KWNG(kernel,
eps=args['epsilon'],
num_basis=args['num_basis'],
with_diag_mat=args['with_diag_mat'])
make_model = lambda : Model(policy=policy, ob_space=ob_space, ac_space=ac_space, nbatch_act=nenvs, nbatch_train=nbatch_train,
nsteps=nsteps, ent_coef=ent_coef, vf_coef=vf_coef,
max_grad_norm=max_grad_norm, KWNG_estimator=estimator)
# make model
model = make_model()
# save model using saver
saver = tf.train.Saver()
logger.info('Restore trained model')
saver.restore(model.sess, callback.model_dir)
# load obs_rms
import pickle
with open(callback.directory + '/ob_rms.pkl', 'rb') as output:
ob_rms = pickle.load(output)
def filter(ob):
ob = np.clip((ob - ob_rms.mean) / np.sqrt(ob_rms.var + 1e-8), -10, 10)
return ob
obsdict = []
acsdict = []
assert len(env.venv.envs) == 1
env_test = env.venv.envs[0]
for e in range(500):
obsrecord = []
acsrecord = []
done = False
rsum = 0
obs = env_test.reset()
obs_orig = obs.copy()
obsrecord.append(obs_orig)
obs = filter(obs)
#env_test.render()
while not done:
#print(obs.shape)
obs = np.expand_dims(obs.flatten(), axis=0)
actions, values, _, neglogpacs = model.step(obs)
newobs, r, done, _ = env_test.step(actions)
obs = newobs
obs_orig = obs.copy()
obsrecord.append(obs_orig)
acsrecord.append(actions)
obs = filter(obs)
obsdict.append(np.array(obsrecord))
acsdict.append(np.array(acsrecord))
if callback is not None:
callback(locals(), globals())