-
Notifications
You must be signed in to change notification settings - Fork 813
/
training.py
1158 lines (1041 loc) · 45.6 KB
/
training.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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding=utf-8
# Copyright 2024 The Trax Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Classes for RL training in Trax."""
import contextlib
import functools
import os
import pickle
import time
import gin
import numpy as np
import tensorflow as tf
from trax import data
from trax import fastmath
from trax import jaxboard
from trax import layers as tl
from trax import models
from trax import shapes
from trax import supervised
from trax.fastmath import numpy as jnp
from trax.optimizers import adam
from trax.rl import advantages
from trax.rl import distributions
from trax.rl import normalization # So gin files see it. # pylint: disable=unused-import
from trax.rl import policy_tasks
from trax.rl import task as rl_task
from trax.supervised import lr_schedules as lr
class Agent:
"""Abstract class for RL agents, presenting the required API."""
def __init__(self,
task: rl_task.RLTask,
n_trajectories_per_epoch=None,
n_interactions_per_epoch=None,
n_eval_episodes=0,
eval_steps=None,
eval_temperatures=(0.0,),
only_eval=False,
output_dir=None,
timestep_to_np=None):
"""Configures the Agent.
Note that subclasses can have many more arguments, which will be configured
using defaults and gin. But task and output_dir are passed explicitly.
Args:
task: RLTask instance, which defines the environment to train on.
n_trajectories_per_epoch: How many new trajectories to collect in each
epoch.
n_interactions_per_epoch: How many interactions to collect in each epoch.
n_eval_episodes: Number of episodes to play with policy at
temperature 0 in each epoch -- used for evaluation only.
eval_steps: an optional list of max_steps to use for evaluation
(defaults to task.max_steps).
eval_temperatures: we always train with temperature 1 and evaluate with
temperature specified in the eval_temperatures list
(defaults to [0.0, 0.5])
only_eval: If set to True, then trajectories are collected only for
for evaluation purposes, but they are not recorded.
output_dir: Path telling where to save outputs such as checkpoints.
timestep_to_np: Timestep-to-numpy function to override in the task.
"""
if n_trajectories_per_epoch is None == n_interactions_per_epoch is None:
raise ValueError(
'Exactly one of n_trajectories_per_epoch or '
'n_interactions_per_epoch should be specified.'
)
self._epoch = 0
self._task = task
self._eval_steps = eval_steps or [task.max_steps]
if timestep_to_np is not None:
self._task.timestep_to_np = timestep_to_np
self._n_trajectories_per_epoch = n_trajectories_per_epoch
self._n_interactions_per_epoch = n_interactions_per_epoch
self._only_eval = only_eval
self._output_dir = output_dir
self._avg_returns = []
self._n_eval_episodes = n_eval_episodes
self._eval_temperatures = eval_temperatures
self._avg_returns_temperatures = {
eval_t: {step: [] for step in self._eval_steps
} for eval_t in eval_temperatures
}
if self._output_dir is not None:
self.init_from_file()
@property
def current_epoch(self):
"""Returns current step number in this training session."""
return self._epoch
@property
def task(self):
"""Returns the task."""
return self._task
@property
def avg_returns(self):
return self._avg_returns
def save_gin(self, summary_writer=None):
assert self._output_dir is not None
config_path = os.path.join(self._output_dir, 'config.gin')
config_str = gin.operative_config_str()
with tf.io.gfile.GFile(config_path, 'w') as f:
f.write(config_str)
if summary_writer is not None:
summary_writer.text(
'gin_config', jaxboard.markdownify_operative_config_str(config_str)
)
def save_to_file(self, file_name='rl.pkl',
task_file_name='trajectories.pkl'):
"""Save current epoch number and average returns to file."""
assert self._output_dir is not None
task_path = os.path.join(self._output_dir, task_file_name)
self._task.save_to_file(task_path)
file_path = os.path.join(self._output_dir, file_name)
dictionary = {'epoch': self._epoch, 'avg_returns': self._avg_returns}
for eval_t in self._eval_temperatures:
dictionary['avg_returns_temperature_{}'.format(
eval_t)] = self._avg_returns_temperatures[eval_t]
with tf.io.gfile.GFile(file_path, 'wb') as f:
pickle.dump(dictionary, f)
def init_from_file(self, file_name='rl.pkl',
task_file_name='trajectories.pkl'):
"""Initialize epoch number and average returns from file."""
assert self._output_dir is not None
task_path = os.path.join(self._output_dir, task_file_name)
if tf.io.gfile.exists(task_path):
self._task.init_from_file(task_path)
file_path = os.path.join(self._output_dir, file_name)
if not tf.io.gfile.exists(file_path):
return
with tf.io.gfile.GFile(file_path, 'rb') as f:
dictionary = pickle.load(f)
self._epoch = dictionary['epoch']
self._avg_returns = dictionary['avg_returns']
for eval_t in self._eval_temperatures:
self._avg_returns_temperatures[eval_t] = dictionary[
'avg_returns_temperature_{}'.format(eval_t)]
def _collect_trajectories(self):
return self.task.collect_trajectories(
self.policy,
n_trajectories=self._n_trajectories_per_epoch,
n_interactions=self._n_interactions_per_epoch,
only_eval=self._only_eval,
epoch_id=self._epoch
)
def policy(self, trajectory, temperature=1.0):
"""Policy function that allows to play using this trainer.
Args:
trajectory: an instance of trax.rl.task.Trajectory
temperature: temperature used to sample from the policy (default=1.0)
Returns:
a pair (action, dist_inputs) where action is the action taken and
dist_inputs is the parameters of the policy distribution, that will later
be used for training.
"""
raise NotImplementedError
def train_epoch(self):
"""Trains this Agent for one epoch -- main RL logic goes here."""
raise NotImplementedError
@contextlib.contextmanager
def _open_summary_writer(self):
"""Opens the Jaxboard summary writer wrapped by a context manager.
Yields:
A Jaxboard summary writer wrapped in a GeneratorContextManager object.
Elements of the lists correspond to the training and evaluation task
directories created during initialization. If there is no output_dir
provided, yields None.
"""
if self._output_dir is not None:
writer = jaxboard.SummaryWriter(os.path.join(self._output_dir, 'rl'))
try:
yield writer
finally:
writer.close()
else:
yield None
def run(self, n_epochs=1, n_epochs_is_total_epochs=False):
"""Runs this loop for n epochs.
Args:
n_epochs: Stop training after completing n steps.
n_epochs_is_total_epochs: if True, consider n_epochs as the total
number of epochs to train, including previously trained ones
"""
with self._open_summary_writer() as sw:
n_epochs_to_run = n_epochs
if n_epochs_is_total_epochs:
n_epochs_to_run -= self._epoch
cur_n_interactions = 0
for _ in range(n_epochs_to_run):
self._epoch += 1
cur_time = time.time()
avg_return = self._collect_trajectories()
self._avg_returns.append(avg_return)
if self._n_trajectories_per_epoch:
supervised.trainer_lib.log(
'Collecting %d episodes took %.2f seconds.'
% (self._n_trajectories_per_epoch, time.time() - cur_time))
else:
supervised.trainer_lib.log(
'Collecting %d interactions took %.2f seconds.'
% (self._n_interactions_per_epoch, time.time() - cur_time))
supervised.trainer_lib.log(
'Average return in epoch %d was %.2f.' % (self._epoch, avg_return))
if self._n_eval_episodes > 0:
for steps in self._eval_steps:
for eval_t in self._eval_temperatures:
avg_return_temperature = self.task.collect_trajectories(
functools.partial(self.policy, temperature=eval_t),
n_trajectories=self._n_eval_episodes,
max_steps=steps,
only_eval=True)
supervised.trainer_lib.log(
'Eval return in epoch %d with temperature %.2f was %.2f.'
% (self._epoch, eval_t, avg_return_temperature))
self._avg_returns_temperatures[eval_t][steps].append(
avg_return_temperature)
if sw is not None:
sw.scalar('timing/collect', time.time() - cur_time,
step=self._epoch)
sw.scalar('rl/avg_return', avg_return, step=self._epoch)
if self._n_eval_episodes > 0:
for steps in self._eval_steps:
for eval_t in self._eval_temperatures:
sw.scalar(
'rl/avg_return_temperature%.2f_steps%d' % (eval_t, steps),
self._avg_returns_temperatures[eval_t][steps][-1],
step=self._epoch)
sw.scalar('rl/n_interactions', self.task.n_interactions(),
step=self._epoch)
sw.scalar('rl/n_interactions_per_second',
(self.task.n_interactions() - cur_n_interactions)/ \
(time.time() - cur_time),
step=self._epoch)
cur_n_interactions = self.task.n_interactions()
sw.scalar('rl/n_trajectories', self.task.n_trajectories(),
step=self._epoch)
sw.flush()
cur_time = time.time()
self.train_epoch()
supervised.trainer_lib.log(
'RL training took %.2f seconds.' % (time.time() - cur_time))
if self._output_dir is not None and self._epoch == 1:
self.save_gin(sw)
if self._output_dir is not None:
self.save_to_file()
def close(self):
pass
class PolicyAgent(Agent):
"""Agent that uses a deep learning model for policy.
Many deep RL methods, such as policy gradient (REINFORCE) or actor-critic fall
into this category, so a lot of classes will be subclasses of this one. But
some methods only have a value or Q function, these are different.
"""
def __init__(self, task, policy_model=None, policy_optimizer=None,
policy_lr_schedule=lr.multifactor, policy_batch_size=64,
policy_train_steps_per_epoch=500, policy_evals_per_epoch=1,
policy_eval_steps=1, n_eval_episodes=0,
only_eval=False, max_slice_length=1, output_dir=None, **kwargs):
"""Configures the policy trainer.
Args:
task: RLTask instance, which defines the environment to train on.
policy_model: Trax layer, representing the policy model.
functions and eval functions (a.k.a. metrics) are considered to be
outside the core model, taking core model output and data labels as
their two inputs.
policy_optimizer: the optimizer to use to train the policy model.
policy_lr_schedule: learning rate schedule to use to train the policy.
policy_batch_size: batch size used to train the policy model.
policy_train_steps_per_epoch: how long to train policy in each RL epoch.
policy_evals_per_epoch: number of policy trainer evaluations per RL epoch
- only affects metric reporting.
policy_eval_steps: number of policy trainer steps per evaluation - only
affects metric reporting.
n_eval_episodes: number of episodes to play with policy at
temperature 0 in each epoch -- used for evaluation only
only_eval: If set to True, then trajectories are collected only for
for evaluation purposes, but they are not recorded.
max_slice_length: the maximum length of trajectory slices to use.
output_dir: Path telling where to save outputs (evals and checkpoints).
**kwargs: arguments for the superclass Agent.
"""
super().__init__(
task,
n_eval_episodes=n_eval_episodes,
output_dir=output_dir,
**kwargs
)
self._policy_batch_size = policy_batch_size
self._policy_train_steps_per_epoch = policy_train_steps_per_epoch
self._policy_evals_per_epoch = policy_evals_per_epoch
self._policy_eval_steps = policy_eval_steps
self._only_eval = only_eval
self._max_slice_length = max_slice_length
self._policy_dist = distributions.create_distribution(task.action_space)
# Inputs to the policy model are produced by self._policy_batches_stream.
self._policy_inputs = data.inputs.Inputs(
train_stream=lambda _: self.policy_batches_stream())
policy_model = functools.partial(
policy_model,
policy_distribution=self._policy_dist,
)
# This is the policy Trainer that will be used to train the policy model.
# * inputs to the trainer come from self.policy_batches_stream
# * outputs, targets and weights are passed to self.policy_loss
self._policy_trainer = supervised.Trainer(
model=policy_model,
optimizer=policy_optimizer,
lr_schedule=policy_lr_schedule(),
loss_fn=self.policy_loss,
inputs=self._policy_inputs,
output_dir=output_dir,
metrics=self.policy_metrics,
)
self._policy_collect_model = tl.Accelerate(
policy_model(mode='collect'), n_devices=1)
policy_batch = next(self.policy_batches_stream())
self._policy_collect_model.init(shapes.signature(policy_batch))
self._policy_eval_model = tl.Accelerate(
policy_model(mode='eval'), n_devices=1) # Not collecting stats
self._policy_eval_model.init(shapes.signature(policy_batch))
@property
def policy_loss(self):
"""Policy loss."""
return NotImplementedError
@property
def policy_metrics(self):
return {'policy_loss': self.policy_loss}
def policy_batches_stream(self):
"""Use self.task to create inputs to the policy model."""
return NotImplementedError
def policy(self, trajectory, temperature=1.0):
"""Chooses an action to play after a trajectory."""
model = self._policy_collect_model
if temperature != 1.0: # When evaluating (t != 1.0), don't collect stats
model = self._policy_eval_model
model.state = self._policy_collect_model.state
model.replicate_weights(self._policy_trainer.model_weights)
tr_slice = trajectory.suffix(self._max_slice_length)
trajectory_np = tr_slice.to_np(timestep_to_np=self.task.timestep_to_np)
# Add batch dimension to trajectory_np and run the model.
pred = model(trajectory_np.observation[None, ...])
# Pick element 0 from the batch (the only one), last (current) timestep.
pred = pred[0, -1, :]
sample = self._policy_dist.sample(pred, temperature=temperature)
result = (sample, pred)
if fastmath.is_backend(fastmath.Backend.JAX):
result = fastmath.nested_map(lambda x: x.copy(), result)
return result
def train_epoch(self):
"""Trains RL for one epoch."""
# When restoring, calculate how many evals are remaining.
n_evals = remaining_evals(
self._policy_trainer.step,
self._epoch,
self._policy_train_steps_per_epoch,
self._policy_evals_per_epoch)
for _ in range(n_evals):
self._policy_trainer.train_epoch(
self._policy_train_steps_per_epoch // self._policy_evals_per_epoch,
self._policy_eval_steps)
def close(self):
self._policy_trainer.close()
super().close()
def remaining_evals(cur_step, epoch, train_steps_per_epoch, evals_per_epoch):
"""Helper function to calculate remaining evaluations for a trainer.
Args:
cur_step: current step of the supervised trainer
epoch: current epoch of the RL trainer
train_steps_per_epoch: supervised trainer steps per RL epoch
evals_per_epoch: supervised trainer evals per RL epoch
Returns:
number of remaining evals to do this epoch
Raises:
ValueError if the provided numbers indicate a step mismatch
"""
if epoch < 1:
raise ValueError('Epoch must be at least 1, got %d' % epoch)
prev_steps = (epoch - 1) * train_steps_per_epoch
done_steps_this_epoch = cur_step - prev_steps
if done_steps_this_epoch < 0:
raise ValueError('Current step (%d) < previously done steps (%d).'
% (cur_step, prev_steps))
train_steps_per_eval = train_steps_per_epoch // evals_per_epoch
if done_steps_this_epoch % train_steps_per_eval != 0:
raise ValueError('Done steps (%d) must divide train steps per eval (%d).'
% (done_steps_this_epoch, train_steps_per_eval))
return evals_per_epoch - (done_steps_this_epoch // train_steps_per_eval)
class LoopPolicyAgent(Agent):
"""Base class for policy-only Agents based on Loop."""
def __init__(
self,
task,
model_fn,
value_fn,
weight_fn,
n_replay_epochs,
n_train_steps_per_epoch,
advantage_normalization,
optimizer=adam.Adam,
lr_schedule=lr.multifactor,
batch_size=64,
network_eval_at=None,
n_eval_batches=1,
max_slice_length=1,
trajectory_stream_preprocessing_fn=None,
**kwargs
):
"""Initializes LoopPolicyAgent.
Args:
task: Instance of trax.rl.task.RLTask.
model_fn: Function (policy_distribution, mode) -> policy_model.
value_fn: Function TimeStepBatch -> array (batch_size, seq_len)
calculating the baseline for advantage calculation.
weight_fn: Function float -> float to apply to advantages when calculating
policy loss.
n_replay_epochs: Number of last epochs to take into the replay buffer;
only makes sense for off-policy algorithms.
n_train_steps_per_epoch: Number of steps to train the policy network for
in each epoch.
advantage_normalization: Whether to normalize the advantages before
passing them to weight_fn.
optimizer: Optimizer for network training.
lr_schedule: Learning rate schedule for network training.
batch_size: Batch size for network training.
network_eval_at: Function step -> bool indicating the training steps, when
network evaluation should be performed.
n_eval_batches: Number of batches to run during network evaluation.
max_slice_length: The length of trajectory slices to run the network on.
trajectory_stream_preprocessing_fn: Function to apply to the trajectory
stream before batching. Can be used e.g. to filter trajectories.
**kwargs: Keyword arguments passed to the superclass.
"""
self._n_train_steps_per_epoch = n_train_steps_per_epoch
super().__init__(task, **kwargs)
task.set_n_replay_epochs(n_replay_epochs)
self._max_slice_length = max_slice_length
trajectory_batch_stream = task.trajectory_batch_stream(
batch_size,
epochs=[-(ep + 1) for ep in range(n_replay_epochs)],
max_slice_length=self._max_slice_length,
sample_trajectories_uniformly=True,
trajectory_stream_preprocessing_fn=trajectory_stream_preprocessing_fn,
)
self._policy_dist = distributions.create_distribution(task.action_space)
train_task = policy_tasks.PolicyTrainTask(
trajectory_batch_stream,
optimizer(),
lr_schedule(),
self._policy_dist,
# Without a value network it doesn't make a lot of sense to use
# a better advantage estimator than MC.
advantage_estimator=advantages.monte_carlo(task.gamma, margin=0),
advantage_normalization=advantage_normalization,
value_fn=value_fn,
weight_fn=weight_fn,
)
eval_task = policy_tasks.PolicyEvalTask(train_task, n_eval_batches)
model_fn = functools.partial(
model_fn,
policy_distribution=self._policy_dist,
)
if self._output_dir is not None:
policy_output_dir = os.path.join(self._output_dir, 'policy')
else:
policy_output_dir = None
# Checkpoint every epoch.
checkpoint_at = lambda step: step % n_train_steps_per_epoch == 0
self._loop = supervised.training.Loop(
model=model_fn(mode='train'),
tasks=[train_task],
eval_model=model_fn(mode='eval'),
eval_tasks=[eval_task],
output_dir=policy_output_dir,
eval_at=network_eval_at,
checkpoint_at=checkpoint_at,
)
self._collect_model = model_fn(mode='collect')
self._collect_model.init(shapes.signature(train_task.sample_batch))
# Validate the restored checkpoints.
# TODO(pkozakowski): Move this to the base class once all Agents use Loop.
if self._loop.step != self._epoch * self._n_train_steps_per_epoch:
raise ValueError(
'The number of Loop steps must equal the number of Agent epochs '
'times the number of steps per epoch, got {}, {} and {}.'.format(
self._loop.step, self._epoch, self._n_train_steps_per_epoch
)
)
@property
def loop(self):
"""Loop exposed for testing."""
return self._loop
def train_epoch(self):
"""Trains RL for one epoch."""
# Copy policy state accumulated during data collection to the trainer.
self._loop.update_weights_and_state(state=self._collect_model.state)
# Train for the specified number of steps.
self._loop.run(n_steps=self._n_train_steps_per_epoch)
class PolicyGradient(LoopPolicyAgent):
"""Trains a policy model using policy gradient on the given RLTask."""
def __init__(self, task, model_fn, **kwargs):
"""Initializes PolicyGradient.
Args:
task: Instance of trax.rl.task.RLTask.
model_fn: Function (policy_distribution, mode) -> policy_model.
**kwargs: Keyword arguments passed to the superclass.
"""
super().__init__(
task, model_fn,
# We're on-policy, so we can only use data from the last epoch.
n_replay_epochs=1,
# Each gradient computation needs a new data sample, so we do 1 step
# per epoch.
n_train_steps_per_epoch=1,
# Very simple baseline: mean return across trajectories.
value_fn=self._value_fn,
# Weights are just advantages.
weight_fn=(lambda x: x),
# Normalize advantages, because this makes optimization nicer.
advantage_normalization=True,
**kwargs
)
def policy(self, trajectory, temperature=1.0):
"""Policy function that samples from the trained network."""
tr_slice = trajectory.suffix(self._max_slice_length)
trajectory_np = tr_slice.to_np(timestep_to_np=self.task.timestep_to_np)
return network_policy(
collect_model=self._collect_model,
policy_distribution=self._policy_dist,
loop=self.loop,
trajectory_np=trajectory_np,
temperature=temperature,
)
@staticmethod
def _value_fn(trajectory_batch):
# Estimate the value of every state as the mean return across trajectories
# and timesteps in a batch.
value = np.mean(trajectory_batch.return_)
return np.broadcast_to(value, trajectory_batch.return_.shape)
@gin.configurable
def sharpened_network_policy(
temperature,
temperature_multiplier=1.0,
**kwargs
):
"""Expert function that runs a policy network with lower temperature.
Args:
temperature: Temperature passed from the Agent.
temperature_multiplier: Multiplier to apply to the temperature to "sharpen"
the policy distribution. Should be <= 1, but this is not a requirement.
**kwargs: Keyword arguments passed to network_policy.
Returns:
Pair (action, dist_inputs) where action is the action taken and dist_inputs
is the parameters of the policy distribution, that will later be used for
training.
"""
return network_policy(
temperature=(temperature_multiplier * temperature),
**kwargs
)
class ExpertIteration(LoopPolicyAgent):
"""Trains a policy model using expert iteration with a given expert."""
def __init__(
self, task, model_fn,
expert_policy_fn=sharpened_network_policy,
quantile=0.9,
n_replay_epochs=10,
n_train_steps_per_epoch=1000,
filter_buffer_size=256,
**kwargs
):
"""Initializes ExpertIteration.
Args:
task: Instance of trax.rl.task.RLTask.
model_fn: Function (policy_distribution, mode) -> policy_model.
expert_policy_fn: Function of the same signature as `network_policy`, to
be used as an expert. The policy will be trained to mimic the expert on
the "solved" trajectories.
quantile: Quantile of best trajectories to be marked as "solved". They
will be used to train the policy.
n_replay_epochs: Number of last epochs to include in the replay buffer.
n_train_steps_per_epoch: Number of policy training steps to run in each
epoch.
filter_buffer_size: Number of trajectories in the trajectory filter
buffer, used to select the best trajectories based on the quantile.
**kwargs: Keyword arguments passed to the superclass.
"""
self._expert_policy_fn = expert_policy_fn
self._quantile = quantile
self._filter_buffer_size = filter_buffer_size
super().__init__(
task, model_fn,
# Don't use a baseline - it's not useful in our weights.
value_fn=(lambda batch: jnp.zeros_like(batch.return_)),
# Don't weight trajectories - the training signal is provided by
# filtering trajectories.
weight_fn=jnp.ones_like,
# Filter trajectories based on the quantile.
trajectory_stream_preprocessing_fn=self._filter_trajectories,
# Advantage normalization is a no-op here.
advantage_normalization=False,
n_replay_epochs=n_replay_epochs,
n_train_steps_per_epoch=n_train_steps_per_epoch,
**kwargs
)
def policy(self, trajectory, temperature=1.0):
"""Policy function that runs the expert."""
tr_slice = trajectory.suffix(self._max_slice_length)
trajectory_np = tr_slice.to_np(timestep_to_np=self.task.timestep_to_np)
return self._expert_policy_fn(
collect_model=self._collect_model,
policy_distribution=self._policy_dist,
loop=self.loop,
trajectory_np=trajectory_np,
temperature=temperature,
)
def _filter_trajectories(self, trajectory_stream):
"""Filter trajectories based on the quantile."""
def trajectory_return(trajectory):
return trajectory.timesteps[0].return_
trajectory_buffer = []
for trajectory in trajectory_stream:
trajectory_buffer.append(trajectory)
if len(trajectory_buffer) == self._filter_buffer_size:
n_best = int((1 - self._quantile) * self._filter_buffer_size) or 1
trajectory_buffer.sort(key=trajectory_return, reverse=True)
yield from trajectory_buffer[:n_best]
trajectory_buffer.clear()
def network_policy(
collect_model,
policy_distribution,
loop,
trajectory_np,
head_index=0,
temperature=1.0,
):
"""Policy function powered by a neural network.
Used to implement Agent.policy() in policy-based agents.
Args:
collect_model: the model used for collecting trajectories
policy_distribution: an instance of trax.rl.distributions.Distribution
loop: trax.supervised.training.Loop used to train the policy network
trajectory_np: an instance of trax.rl.task.TimeStepBatch
head_index: index of the policy head a multihead model.
temperature: temperature used to sample from the policy (default=1.0)
Returns:
a pair (action, dist_inputs) where action is the action taken and
dist_inputs is the parameters of the policy distribution, that will later
be used for training.
"""
if temperature == 1.0:
model = collect_model
else:
# When evaluating (t != 1.0), use the evaluation model instead of the
# collection model - some models accumulate normalization statistics
# during data collection, and we don't want to do it in eval to avoid data
# leakage.
model = loop.eval_model
model.state = collect_model.state
# Copying weights from loop.model should work, because the raw model's
# weights should be updated automatically during training, but it doesn't.
# TODO(pkozakowski): Debug.
acc = loop._trainer_per_task[0].accelerated_model_with_loss # pylint: disable=protected-access
model.weights = acc._unreplicate(acc.weights[0]) # pylint: disable=protected-access
# Add batch dimension to trajectory_np and run the model.
pred = model(trajectory_np.observation[None, ...])
if isinstance(pred, (tuple, list)):
# For multihead models, extract the policy head output.
pred = pred[head_index]
assert pred.shape == (
1, trajectory_np.observation.shape[0], policy_distribution.n_inputs
)
# Pick element 0 from the batch (the only one), last (current) timestep.
pred = pred[0, -1, :]
sample = policy_distribution.sample(pred, temperature=temperature)
result = (sample, pred)
if fastmath.is_backend(fastmath.Backend.JAX):
# The result is composed of mutable numpy arrays. We copy them to avoid
# accidental modification.
result = fastmath.nested_map(lambda x: x.copy(), result)
return result
class ValueAgent(Agent):
"""Trainer that uses a deep learning model for value function.
Compute the loss using variants of the Bellman equation.
"""
def __init__(self, task,
value_body=None,
value_optimizer=None,
value_lr_schedule=lr.multifactor,
value_batch_size=64,
value_train_steps_per_epoch=500,
value_evals_per_epoch=1,
value_eval_steps=1,
exploration_rate=functools.partial(
lr.multifactor,
factors='constant * decay_every',
constant=1., # pylint: disable=redefined-outer-name
decay_factor=0.99,
steps_per_decay=1,
minimum=0.1),
n_eval_episodes=0,
only_eval=False,
n_replay_epochs=1,
max_slice_length=1,
sync_freq=1000,
scale_value_targets=True,
output_dir=None,
**kwargs):
"""Configures the value trainer.
Args:
task: RLTask instance, which defines the environment to train on.
value_body: Trax layer, representing the body of the value model.
functions and eval functions (a.k.a. metrics) are considered to be
outside the core model, taking core model output and data labels as
their two inputs.
value_optimizer: the optimizer to use to train the policy model.
value_lr_schedule: learning rate schedule to use to train the policy.
value_batch_size: batch size used to train the policy model.
value_train_steps_per_epoch: how long to train policy in each RL epoch.
value_evals_per_epoch: number of policy trainer evaluations per RL epoch
- only affects metric reporting.
value_eval_steps: number of policy trainer steps per evaluation - only
affects metric reporting.
exploration_rate: exploration rate schedule - used in the policy method.
n_eval_episodes: number of episodes to play with policy at
temperature 0 in each epoch -- used for evaluation only
only_eval: If set to True, then trajectories are collected only for
for evaluation purposes, but they are not recorded.
n_replay_epochs: Number of last epochs to take into the replay buffer;
only makes sense for off-policy algorithms.
max_slice_length: the maximum length of trajectory slices to use; it is
the second dimenions of the value network output:
(batch, max_slice_length, number of actions)
Higher max_slice_length implies that the network has to predict more
values into the future.
sync_freq: frequency when to synchronize the target
network with the trained network. This is necessary for training the
network on bootstrapped targets, e.g. using n-step returns.
scale_value_targets: If `True`, scale value function targets by
`1 / (1 - gamma)`. We are trying to fix the problem with very large
returns in some games in a way which does not introduce an additional
hyperparameters.
output_dir: Path telling where to save outputs (evals and checkpoints).
**kwargs: arguments for the superclass RLTrainer.
"""
super(ValueAgent, self).__init__(
task,
n_eval_episodes=n_eval_episodes,
output_dir=output_dir,
**kwargs
)
self._value_batch_size = value_batch_size
self._value_train_steps_per_epoch = value_train_steps_per_epoch
self._value_evals_per_epoch = value_evals_per_epoch
self._value_eval_steps = value_eval_steps
self._only_eval = only_eval
self._max_slice_length = max_slice_length
self._policy_dist = distributions.create_distribution(task.action_space)
self._n_replay_epochs = n_replay_epochs
self._exploration_rate = exploration_rate()
self._sync_at = (lambda step: step % sync_freq == 0)
if scale_value_targets:
self._value_network_scale = 1 / (1 - self._task.gamma)
else:
self._value_network_scale = 1
value_model = functools.partial(
models.Quality,
body=value_body,
n_actions=self.task.action_space.n)
self._value_eval_model = value_model(mode='eval')
self._value_eval_model.init(self._value_model_signature)
self._value_eval_jit = tl.jit_forward(
self._value_eval_model.pure_fn, fastmath.local_device_count(),
do_mean=False)
# Inputs to the value model are produced by self._values_batches_stream.
self._inputs = data.inputs.Inputs(
train_stream=lambda _: self.value_batches_stream())
# This is the value Trainer that will be used to train the value model.
# * inputs to the trainer come from self.value_batches_stream
# * outputs, targets and weights are passed to self.value_loss
self._value_trainer = supervised.Trainer(
model=value_model,
optimizer=value_optimizer,
lr_schedule=value_lr_schedule(),
loss_fn=self.value_loss,
inputs=self._inputs,
output_dir=output_dir,
metrics={'value_loss': self.value_loss,
'value_mean': self.value_mean,
'returns_mean': self.returns_mean}
)
value_batch = next(self.value_batches_stream())
self._eval_model = tl.Accelerate(
value_model(mode='collect'), n_devices=1)
self._eval_model.init(shapes.signature(value_batch))
@property
def _value_model_signature(self):
obs_sig = shapes.signature(self._task.observation_space)
target_sig = mask_sig = shapes.ShapeDtype(
shape=(1, 1, self._task.action_space),
)
inputs_sig = obs_sig.replace(shape=(1, 1) + obs_sig.shape)
return (inputs_sig, target_sig, mask_sig)
def value_batches_stream(self):
"""Use self.task to create inputs to the policy model."""
raise NotImplementedError
def policy(self, trajectory, temperature=1):
"""Chooses an action to play after a trajectory."""
raise NotImplementedError
def train_epoch(self):
"""Trains RL for one epoch."""
# Update the target value network.
self._value_eval_model.weights = self._value_trainer.model_weights
self._value_eval_model.state = self._value_trainer.model_state
# When restoring, calculate how many evals are remaining.
n_evals = remaining_evals(
self._value_trainer.step,
self._epoch,
self._value_train_steps_per_epoch,
self._value_evals_per_epoch)
for _ in range(n_evals):
self._value_trainer.train_epoch(
self._value_train_steps_per_epoch // self._value_evals_per_epoch,
self._value_eval_steps)
value_metrics = dict(
{'exploration_rate': self._exploration_rate(self._epoch)})
self._value_trainer.log_metrics(value_metrics,
self._value_trainer._train_sw, 'dqn') # pylint: disable=protected-access
# Update the target value network.
# TODO(henrykm) a bit tricky if sync_at does not coincide with epochs
if self._sync_at(self._value_trainer.step):
self._value_eval_model.weights = self._value_trainer.model_weights
self._value_eval_model.state = self._value_trainer.model_state
def close(self):
self._value_trainer.close()
super().close()
@property
def value_mean(self):
"""The mean value of actions selected by the behavioral policy."""
raise NotImplementedError
@property
def returns_mean(self):
"""The mean value of actions selected by the behavioral policy."""
def f(values, index_max, returns, mask):
del values, index_max
return jnp.sum(returns) / jnp.sum(mask)
return tl.Fn('ReturnsMean', f)
class DQN(ValueAgent):
r"""Trains a value model using DQN on the given RLTask.
Notice that the algorithm and the parameters signficantly diverge from
the original DQN paper. In particular we have separated learning and data
collection.
The Bellman loss is computed in the value_loss method. The formula takes
the state-action values tensors Q and n-step returns R:
.. math::
L(s,a) = Q(s,a) - R(s,a)
where R is computed in value_batches_stream. In the simplest case of the
1-step returns we are getting
.. math::
L(s,a) = Q(s,a) - r(s,a) - gamma * \max_{a'} Q'(s',a')
where s' is the state reached after taking action a in state s, Q' is
the target network, gamma is the discount factor and the maximum is taken
with respect to all actions avaliable in the state s'. The tensor Q' is
updated using the sync_freq parameter.
In code the maximum is visible in the policy method where we take
sample = jnp.argmax(values). The epsilon-greedy policy is taking a random
move with probability epsilon and oterhwise in state s it takes the
action argmax_a Q(s,a).
"""
def __init__(self,
task,
advantage_estimator=advantages.monte_carlo,
max_slice_length=1,
smoothl1loss=True,
double_dqn=False,
**kwargs):
self._max_slice_length = max_slice_length
self._margin = max_slice_length-1
# Our default choice of learning targets for DQN are n-step targets
# implemented in the method td_k. We set the slice used for computation
# of td_k to max_slice_length and we set the "margin" in td_k
# to self._max_slice_length-1; in turn it implies that the shape of the
# returned tensor of n-step targets is
# values[:, :-(self.margin)] = values[:, :1]
self._advantage_estimator = advantage_estimator(
gamma=task.gamma, margin=self._margin)