-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathatari.py
1195 lines (994 loc) · 43.9 KB
/
atari.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
import os
import gym
import pdb
import random
import math
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sumtree import SumTree
from collections import namedtuple, OrderedDict
from PIL import Image
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as T
is_ipython = 'inline' in matplotlib.get_backend()
if is_ipython: from IPython import display
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
Experience = namedtuple('Experience', ('state', 'action', 'next_state', 'reward'))
DO_NOTHING = 0
MAX_COLOR_VALUE = 255
SAVED_DATA = 'saved_data'
SAVED_MODELS = 'saved_models'
SAVED_REWARDS = 'saved_rewards'
class DQN(nn.Module):
"""
This class represents a Deep Q-Network with two convolutional layers and
two fully connected layers.
Attributes
----------
conv_layers : nn.Sequential
The convolutional layers used in the DQN.
fc_layers : nn.Sequential
The fully connected layers used in the DQN.
"""
def __init__(self, action_space, frame_height, frame_width, k_frames):
"""
Initializes the DQN layers and their weights.
Parameters
----------
action_space : int
The number of possible actions that the agent can take in its environment.
action_space > 0
frame_height : int
The height of the frames that are used as inputs to the DQN.
frame_height > 0
frame_width : int
The width of the frames that are used as inputs to the DQN.
frame_width > 0
k_frames : int
The number of frames that are stacked in the input.
k_frames > 1
"""
assert action_space > 0 and isinstance(action_space, int), 'action_space should be a positive integer'
assert frame_height > 0 and isinstance(frame_height, int), 'frame_height should be a positive integer'
assert frame_width > 0 and isinstance(frame_width, int), 'frame_width should be a positive integer'
assert k_frames > 1 and isinstance(k_frames, int), 'k_frames should be an integer greater than 1'
super().__init__()
conv1 = nn.Conv2d(in_channels=k_frames, out_channels=32, kernel_size=8, stride=4)
conv1_height = ((frame_height + 2*conv1.padding[0] - conv1.dilation[0]*(conv1.kernel_size[0] - 1) - 1)\
// conv1.stride[0]) + 1
conv1_width = ((frame_width + 2*conv1.padding[1] - conv1.dilation[1]*(conv1.kernel_size[1] - 1) - 1)\
// conv1.stride[1]) + 1
conv2 = nn.Conv2d(in_channels=conv1.out_channels, out_channels=64, kernel_size=4, stride=2)
conv2_height = ((conv1_height + 2*conv2.padding[0] - conv2.dilation[0]*(conv2.kernel_size[0] - 1) - 1)\
// conv2.stride[0]) + 1
conv2_width = ((conv1_width + 2*conv2.padding[1] - conv2.dilation[1]*(conv2.kernel_size[1] - 1) - 1)\
// conv2.stride[1]) + 1
fc1 = nn.Linear(in_features=conv2.out_channels * conv2_height * conv2_width, out_features=512)
fc2 = nn.Linear(in_features=fc1.out_features, out_features=action_space)
softmax = nn.Softmax(dim=1)
self.conv_layers = nn.Sequential(OrderedDict([
('conv1', conv1),
('relu1', nn.ReLU()),
('conv2', conv2),
('relu2', nn.ReLU())
]))
self.fc_layers = nn.Sequential(OrderedDict([
('fc1', fc1),
('relu3', nn.ReLU()),
('fc2', fc2),
('output', softmax)
]))
self.fc_layers.apply(self.__init_weights)
nn.utils.clip_grad_norm_(self.parameters(), 1)
def __init_weights(self, layer):
"""
Randomizes the weights of a fully connected layer. This method is used recursively
with the "apply" method provided by the pytorch library.
"""
if isinstance(layer, nn.Linear):
nn.init.xavier_uniform_(layer.weight)
layer.bias.data.fill_(0.01)
def forward(self, t) -> torch.tensor:
"""
Sends an input tensor through the hidden layers of the DQN.
Parameters
----------
t : torch.tensor
An input tensor representing the current state. The input tensor must have
the following shape: (batch_size, k_frames, frame_height, frame_width) where
each frame is represented as a grayscale image.
Returns
-------
torch.tensor
A tensor holding the Q values for each possible action in the action space.
The output shape is (batch_size, action_space).
"""
t = t.float()
t = self.conv_layers(t)
t = t.flatten(start_dim=1)
t = self.fc_layers(t)
return t
class ReplayMemory:
"""
This class represents replay memory that samples uniformly.
Attributes
----------
capacity : int
The maximum number of experiences that can be stored in memory.
capacity > 0
memory : list
A list that contains the most recent experiences. It cannot contain
more experiences than the capacity attribute permits.
data_ptr : int
The index of the element in the memory list that will be overwitten on
the next push to memory. This value is incremented for each push to memory.
If this value reaches the value for capacity, data_ptr will be set to zero.
0 <= data_ptr < capacity
experience_count : int
The number of experiences currently stored in memory.
0 <= experience_count <= capacity
"""
def __init__(self, capacity):
"""
Initializes the replay memory.
Parameters
----------
capacity : int
The maximum number of experiences that can be stored in memory.
capacity > 0
"""
assert capacity > 0 and isinstance(capacity, int), 'capacity should be a positive integer'
self.capacity = capacity
self.memory = self.capacity * [0]
self.data_ptr = 0
self.experience_count = 0
def __len__(self) -> int:
"""
Returns
-------
int
The current number of experiences stored in memory.
"""
return self.experience_count
def push(self, experience):
"""
Adds an experience to replay memory. If the replay memory is full, then the least recently
added experience is overwritten.
Parameters
----------
experience : namedtuple
The experience to be added to replay memory.
"""
self.memory[self.data_ptr] = experience
self.data_ptr += 1
if self.experience_count < self.capacity:
self.experience_count += 1
if self.data_ptr >= self.capacity:
self.data_ptr = 0
def sample(self, batch_size) -> tuple:
"""
Samples a random batch of experiences. The class method can_provide_sample
should be called before this function.
Parameters
----------
batch_size : int
The number of experiences to sample at once.
Returns
-------
tuple
A tuple containing:
1) The states for the given sampled experiences
2) The actions taken in each of the sampled states
3) The rewards observed from taking the action in each state
4) The next states that are observed
"""
batch = random.sample(self.memory[:self.experience_count], batch_size)
experiences = Experience(*zip(*batch))
states = torch.cat(experiences.state)
actions = torch.cat(experiences.action)
rewards = torch.cat(experiences.reward)
next_states = torch.cat(experiences.next_state)
return states, actions, rewards, next_states
def can_provide_sample(self, batch_size) -> bool:
"""
Determines if a sample of a specific size can be drawn from memory.
Parameters
----------
batch_size : int
The number of experiences that are requested for sampling.
Returns
-------
bool
Returns true if a sample can be drawn, and false otherwise.
"""
return self.__len__() >= batch_size
class PrioritizedReplayMemory:
"""
This class is an implementation of Prioritized Experience Replay. It uses a Sum Tree
to store the experiences observed by the agent.
Attributes
----------
capacity : int
The maximum number of experiences that can be stored in memory.
alpha : float
This is a parameter that quantifies the affect of error on the probability
distribution that is used to sample experiences. If alpha is 0, the distribution
is uniform and error does not affect which experiences get sampled. If alpha is 1,
then error completely determines the probability distribtion of sampling.
0 <= alpha <= 1
beta : float
This is a parameter that controls how much prioritization should be applied for a
given sample. This value is annealed towards 1 as more samples are drawn from memory.
0 <= beta <= 1
beta_increment : float
This value linearly anneals beta towards 1. Each time a sample is drawn from memory,
beta increases by this value.
beta_increment > 0
eps : float
A small values added to the error of an experience to ensure that it has a non-zero
probability that it will be drawn.
eps > 0
tree : SumTree
A SumTree object that contains the experiences and is responsible for sampling.
current_size : int
Represents the number of experiences in memory. Cannot exceed the capacity.
0 <= current_size <= capacity
"""
def __init__(self, capacity, alpha, beta, beta_increment, eps):
"""
Initializes the replay memory.
Parameters
----------
capacity : int
The maximum number of experiences that can be stored in memory.
capacity > 0
alpha : float
This is a parameter that quantifies the affect of error on the probability
distribution that is used to sample experiences. If alpha is 0, the distribution
is uniform and error does not affect which experiences get sampled. If alpha is 1,
then error completely determines the probability distribtion of sampling.
0 <= alpha <= 1
beta : float
This is a parameter that controls how much prioritization should be applied for a
given sample. This value is annealed towards 1 as more samples are drawn from memory.
0 <= beta <= 1
beta_increment : float
This value linearly anneals beta towards 1. Each time a sample is drawn from memory,
beta increases by this value.
beta_increment > 0
eps : float
A small values added to the error of an experience to ensure that it has a non-zero
probability that it will be drawn.
eps > 0
"""
assert capacity > 0 and isinstance(capacity, int), 'capacity should be a positive integer'
assert 0. <= alpha <= 1. and isinstance(alpha, float), 'alpha must be a float in the interval [0,1]'
assert 0. <= beta <= 1. and isinstance(beta, float), 'beta must be a float in the interval [0,1]'
assert beta_increment > 0. and isinstance(beta_increment, float), 'beta_increment must be a positive float'
assert eps > 0. and isinstance(eps, float), 'eps must be a positive float'
self.capacity = capacity
self.alpha = alpha
self.beta = beta
self.beta_increment = beta_increment
self.eps = eps
self.tree = SumTree(capacity)
self.current_size = 0
def __len__(self) -> int:
"""
Returns
-------
int
Returns the number of experiences currently stored in replay memory.
"""
return len(self.tree)
def push(self, experience, policy_net, target_net, done):
"""
Adds an experience to replay memory. If the replay memory is full, then the least recently
added experience is overwritten.
Parameters
----------
experience : namedtuple
The experience to be added to replay memory.
policy_net : DQN
The policy network.
target_net : DQN
The target network.
done : bool
True if this experience is the last experience of an episode,
and false otherwise.
"""
current = QValues.get_QValues(policy_net, experience.state, experience.action).item()
with torch.no_grad():
if done:
target = experience.reward.item()
else:
_, next_action = QValues.get_next(target_net, experience.state)
next_q = QValues.get_QValues(target_net, experience.next_state, next_action)
target = next_q.item() + experience.reward.item()
error = np.abs(current - target)
priority = (error + self.eps) ** self.alpha
self.tree.add(priority, experience)
def sample(self, batch_size) -> tuple:
"""
Samples a random batch of experiences. The class method can_provide_sample
should be called before this function.
Parameters
----------
batch_size : int
The number of experiences to sample at once.
Returns
-------
tuple
A tuple containing:
1) The states for the given sampled experiences
2) The actions taken in each of the sampled states
3) The rewards observed from taking the action in each state
4) The next states that are observed
5) The indeces of where each experience is stored in the sum tree
6) The importance sampling weights for each experience
"""
batch = []
idxs = []
segment_len = self.tree.get_total_priority() / batch_size
priorities = []
self.beta = np.min([1., self.beta + self.beta_increment])
for i in range(batch_size):
lower = segment_len * i
upper = segment_len * (i + 1)
s = np.random.uniform(lower, upper)
(idx, priority, experience) = self.tree.get(s)
priorities.append(priority)
batch.append(experience)
idxs.append(idx)
sampling_probabilities = priorities / self.tree.get_total_priority()
is_weight = np.power(self.tree.size * sampling_probabilities, -self.beta)
is_weight /= is_weight.max()
experiences = Experience(*zip(*batch))
states = torch.cat(experiences.state)
actions = torch.cat(experiences.action)
rewards = torch.cat(experiences.reward)
next_states = torch.cat(experiences.next_state)
return states, actions, rewards, next_states, idxs, is_weight
def can_provide_sample(self, batch_size) -> bool:
"""
Determines if a sample of a specific size can be drawn from memory.
Parameters
----------
batch_size : int
The number of experiences that are requested for sampling.
Returns
-------
bool
Returns true if a sample can be drawn, and false otherwise.
"""
return self.__len__() >= batch_size
def update(self, idx, error):
"""
Updates the sum tree when a new experience is added.
Parameters
----------
idx : int
The index of where the new experience is written in the sum tree.
error : float
The error associated with a given experience.
"""
priority = (np.abs(error) + self.eps) ** self.alpha
self.tree.update(idx, priority)
class Agent:
"""
This class represents the agent. The agent is responsible for making decisions in its
environment following an epsilon greedy policy.
Attributes
----------
actions_taken : int
The total number of actions that the agent has taken.
action_space : int
The total number of possible actions that the agent could take in its environment.
action_space > 0
eps_start : float
The starting value for the exploration rate. Usually is 1.0.
0 <= eps_start <= 1.0
eps_end : float
The ending value for the exploration rate.
0 <= eps_end <= eps_start
eps_decay : float
The value for which the exploration rate is linearly annealed towards eps_end.
eps_decay >= 0
"""
def __init__(self, action_space, eps_start, eps_end, eps_decay):
"""
Initializes the agent class.
Parameters
----------
action_space : int
The total number of possible actions that the agent could take in its environment.
action_space > 0
eps_start : float
The starting value for epsilon in the epsilon greedy policy. Usually is 1.0.
0 <= eps_start <= 1.0
eps_end : float
The ending value for epsilon in the epsilon greedy policy.
0 <= eps_end <= eps_start
eps_decay : float
The value for which epsilon is linearlly annealed towards eps_end.
eps_decay >= 0
"""
assert action_space > 0 and isinstance(action_space, int), 'action_space must be a positive integer'
assert 0. <= eps_start <= 1. and isinstance(eps_start, float), 'eps_start must be a float on the interval [0,1]'
assert 0. <= eps_end <= eps_start and isinstance(eps_end, float), 'eps_end must be a float on the interval [0, eps_start]'
assert eps_decay >= 0. and isinstance(eps_decay, float), 'eps_decay must be a non-negative float'
self.actions_taken = 0
self.action_space = action_space
self.eps_start = eps_start
self.eps_end = eps_end
self.eps_decay = eps_decay
def __get_exploration_rate(self) -> float:
"""
Calculates the exploration rate that follows linear decay.
Returns
-------
float
The exploration rate.
"""
return np.max([self.eps_end, self.eps_start - self.actions_taken * self.eps_decay])
def select_action(self, state, policy_net) -> torch.tensor:
"""
Selects the next action that the agent should take based on the epsilon
greedy policy.
Parameters
----------
state : torch.tensor
A tensor containing a stack of grayscale frames representing the current
state. The input shape should be (k_frames, scaled_height, scaled_width).
policy_net : DQN
The policy network that is used to exploit the current environment.
Returns
-------
torch.tensor
Returns a tensor with a single integer that contains the action that the
agent should take.
"""
rate = self.__get_exploration_rate()
self.actions_taken += 1
if rate <= np.random.uniform(0,1):
with torch.no_grad():
q_values = policy_net(state).squeeze().detach().numpy()
action = np.argmax(q_values)
else:
action = np.random.randint(self.action_space)
return torch.tensor([action], dtype=torch.int64).to(device)
class AtariEnv:
"""
This class manages the operations that are performed on the Atari environment, provides
information about the Atari environment, and performs preprocessing on the frames provided
by the Atari environment for training.
Attributes
----------
env : gym.envs.atari.atari_env
The environment representing the Atari game.
scale_height : int
The height that the frames are scaled to during preprocessing.
scale_height > 0
scale_width : int
The width that the frames are scaled to during preprocessing.
scale_width > 0
k_frames : int
The number of frames that are stack together to form one state.
k_frames > 1
enable_rendering : bool
If true, the Atari environment will be rendered during training.
state : list
A list of arrays that represent each frame for the current state.
prev_frame : np.array
The previous frame that the agent experienced in the Atari environment.
xform : torchvision.transforms.Compose
A torchvision object that performs preprocessing on a frame. It first converts the frame
to a PIL image, converts it to a grayscale image, and finally resizes the frame to a specified
height and width.
done : bool
If true, the current state is a terminating state for the current episode.
info : list
A list that contains information about the Atari environment.
"""
def __init__(self, gym_env, scale_height, scale_width, k_frames, enable_rendering):
"""
Initializes the AtariEnv object.
Parameters
----------
gym_env : str
A string that represents the Atari environment. Go to the following link to see the Atari games
and their strings: https://gym.openai.com/envs/#atari
scale_height : int
The height that the frames are scaled to during preprocessing.
scale_height > 0
scale_width : int
The width that the frames are scaled to during preprocessing.
scale_width > 0
k_frames : int
The number of frames that are stack together to form one state.
k_frames > 1
enable_rendering : bool
If true, the Atari environment will be rendered during training.
"""
assert scale_height > 0 and isinstance(scale_height, int), 'scale_height should be a positive integer'
assert scale_width > 0 and isinstance(scale_width, int), 'scale_width should be a positive integer'
assert k_frames > 1 and isinstance(k_frames, int), 'k_frames should be an integer greater than 1'
self.env = gym.make(gym_env).unwrapped
self.scale_height = scale_height
self.scale_width = scale_width
self.k_frames = k_frames
self.enable_rendering = enable_rendering
self.state = []
self.prev_frame = np.zeros((self.scale_height, self.scale_width), dtype=np.float32)
self.xform = T.Compose([
T.ToPILImage(),
T.Resize((self.scale_height, self.scale_width)),
T.Grayscale()
])
self.reset()
def reset(self):
"""
Resets the Atari environment and the current state.
"""
self.done = False
self.info = {}
self.env.reset()
self.state = []
def close(self):
"""
Closes the Atari environment.
"""
self.env.close()
def render(self, mode='human'):
"""
Renders the Atari environment.
Parameters
----------
mode : str
Renders the Atari environment according to a specified mode.
From the Gym Documentation:
- human: render to the current display or terminal and
return nothing. Usually for human consumption.
- rgb_array: Return an numpy.ndarray with shape (x, y, 3),
representing RGB values for an x-by-y pixel image, suitable
for turning into a video.
- ansi: Return a string (str) or StringIO.StringIO containing a
terminal-style text representation. The text can include newlines
and ANSI escape sequences (e.g. for colors).
Returns
-------
None, np.array, str
The return value varies depending on the specifed mode. See the above.
"""
return self.env.render(mode)
def in_initial_state(self) -> bool:
"""
Returns
-------
bool
Returns true if the Atari is in an initial state, and false otherwise.
"""
return self.state == []
def get_unscaled_height(self) -> int:
"""
Returns
-------
int
Returns the height of the unscaled frame from the Atari environment.
"""
return self.render('rgb_array').shape[0]
def get_unscaled_width(self) -> int:
"""
Returns
-------
int
Returns the width of the unscaled frame from the Atari environment.
"""
return self.render('rgb_array').shape[1]
def get_action_space(self) -> int:
"""
Returns
-------
int
Returns the total number of possible actions that the agent can take in the
Atari environment.
"""
return self.env.action_space.n
def get_state(self) -> torch.tensor:
"""
Returns
-------
torch.tensor
Returns a tensor representing the current state of the Atari environment. It
contains stacked grayscaled frames to represent one state.
"""
if self.in_initial_state() or self.done:
return torch.zeros(self.k_frames, self.scale_height, self.scale_width)
return torch.tensor(self.state).to(device)
def get_info(self) -> list:
"""
Returns
-------
list
Returns information about the current state of the Atari environment.
"""
return self.info
def execute_action(self, action) -> torch.tensor:
"""
Executes a specified action on the last frame (the k_frame-th frame of the stack).
The step method in atari gym environments automatically implement frame skipping,
so it is not explicitely coded here. Each frame in the state is the difference
between the current frame and the previous frame.
Parameters
----------
action : torch.tensor
A tensor with a single integer value that represents the action that the agent
will execute.
Returns
-------
torch.tensor
Returns a tensor with a single element that represents the reward observed by
the agent as a result of executing the specified action.
"""
reward = 0
if self.in_initial_state():
for _ in range(self.k_frames - 1):
self.__step(DO_NOTHING)
reward = self.__step(action.item())
if self.done:
self.state = []
self.prev_frame = np.zeros((self.scale_height, self.scale_width), dtype=np.float32)
return torch.tensor([reward / self.k_frames], device=device)
def __step(self, action) -> torch.tensor:
"""
Executes a specified action for a few frames and renders the current frame.
Parameters
----------
action : int
The action to be taken.
Returns
-------
torch.tensor
Returns a tensor that contains a single element representing the reward observed
by the agent as a result of taking the specified action.
"""
next_frame, reward, self.done, self.info = self.env.step(action)
if self.enable_rendering:
next_frame = self.render('rgb_array')
next_frame = self.__process_frame(next_frame)
if len(self.state) == self.k_frames:
del self.state[0]
self.state.append(next_frame - self.prev_frame)
self.prev_frame = next_frame
return reward
def __process_frame(self, frame) -> np.array:
"""
Processes a frame to make it appropriate for training.
The frame is converted to a grayscale image, its dimensions are scaled
and the intensity values of the grayscale image are normalized.
Parameters
----------
frame : np.array
The current frame to be processed.
Returns
-------
np.array
Returns an array representing the modified frame.
"""
xframe = np.array(self.xform(frame), dtype=np.float32) / MAX_COLOR_VALUE
return xframe
class QValues:
"""
This class holds static methods that are used to calculate Q values.
"""
@staticmethod
def get_QValues(policy_net, states, actions) -> torch.tensor:
"""
Gets the Q values associated with each state-action pair. This method supports mini batching.
For example, if the mini batch size was 32, then the "states" tensor contains 32 states, and
the "actions" tensor contains 32 actions. In other words, the size of the first dimension of
"states" and "actions" should be the same.
Parameters
----------
policy_net : DQN
The policy network.
states : torch.tensor
A tensor containing multiple states to be processed.
actions : torch.tensor
A tensor containing multiple actions associated with each state.
Returns
-------
torch.tensor
A tensor containing the Q value for each state-action pair.
"""
return policy_net(states).gather(dim=1, index=actions.unsqueeze(-1))
@staticmethod
def get_next(target_net, next_states) -> torch.tensor:
"""
Gets the maximum Q values and actions associated with the next states that are observed.
Parameters
----------
target_net : DQN
The target network.
next_states : torch.tensor
A tensor containing the next states that are observed.
Returns
-------
tuple
A tuple containing a tensor with the maximum Q values and action associated with
the maximum Q values.
"""
max_values, max_actions = torch.max(target_net(next_states), dim=1)
return max_values, max_actions
class AtariAI:
@staticmethod
def plot(values):
"""
Plots the mean reward for each episode.
Parameters
----------
values : list
A list containing the mean reward for each episode, where the index of each element
in the list corresponds to the episode that the mean reward belongs to.
"""
plt.clf()
plt.title('Mean Reward vs Episode')
plt.xlabel('Episode')
plt.ylabel('Mean Reward')
plt.plot(values)
plt.show()
if is_ipython: display.clear_output(wait=True)
@staticmethod
def save_rewards(filename, rewards):
"""
Saves the total rewards for each episode on disk.
Parameters
----------
filename : str
The name of the file.
rewards : list
A list containing the mean rewards for each episode.
"""
if not os.path.exists(os.path.join(SAVED_DATA, SAVED_REWARDS)):
os.makedirs(os.path.join(SAVED_DATA, SAVED_REWARDS))
reward_data = pd.DataFrame(rewards, columns=['Reward'])
reward_data.to_csv(os.path.join(SAVED_DATA, SAVED_REWARDS, filename + '.csv'), index=False)
@staticmethod
def save_state_dict(gym_env, target_net, scaled_height, scaled_width, k_frames):
"""
Saves the target network's state dictionary and input shape on disk.
The model is saved to the 'saved_models' directory.
Parameters
----------
gym_env : str
The name of the Atari game environment that the model is trained in.
target_net : DQN
The target network containing the state dictionary to be saved.
scaled_height : int
The height of the frames that are used as inputs to the DQN.
frame_height > 0
scaled_width : int
The width of the frames that are used as inputs to the DQN.
frame_width > 0
k_frames : int
The number of frames that are stacked in the input.
k_frames > 1
"""
if not os.path.exists(os.path.join(SAVED_DATA, SAVED_MODELS)):
os.makedirs(os.path.join(SAVED_DATA, SAVED_MODELS))
dqn_data = pd.DataFrame([[scaled_height, scaled_width, k_frames]],
columns=['scaled_height', 'scaled_width', 'k_frames'])
dqn_data.to_csv(os.path.join(SAVED_DATA, SAVED_MODELS, gym_env + '.csv'), index=False)
torch.save(target_net.state_dict(), os.path.join(SAVED_DATA, SAVED_MODELS, gym_env + '.pth'))
def train_DQN(gym_env,
scaled_height=84,
scaled_width=84,
k_frames=4,
memory_size=10000,
greedy_start=1.,
greedy_end=0.01,
greedy_decay=1e-5,
num_episodes=100,
max_timesteps=200,
discount=0.99,
batch_size=32,
target_update=10,
optim_lr=2.5e-4,
optim_eps=1e-2,
render=False,
plot_reward=False,
save_rewards=False,
save_model=False
):
"""
This function trains an agent to play an Atari game using a DQN and returns
the target network.
"""
atarienv = AtariEnv(gym_env, scaled_height, scaled_width, k_frames, render)
agent = Agent(atarienv.get_action_space(), greedy_start, greedy_end, greedy_decay)
memory = ReplayMemory(memory_size)
policy_net = DQN(atarienv.get_action_space(), scaled_height, scaled_width, k_frames)
target_net = DQN(atarienv.get_action_space(), scaled_height, scaled_width, k_frames)
target_net.load_state_dict(policy_net.state_dict())
policy_net.train()
target_net.eval()
optimizer = optim.Adam(params=policy_net.parameters(), lr=optim_lr, eps=optim_eps)
loss_fcn = nn.MSELoss(reduction='none')
episode_rewards = []
for episode in range(num_episodes):
#Initialize starting state
total_reward = 0
atarienv.reset()
state = atarienv.get_state().unsqueeze(0)
for timestep in range(max_timesteps):
#Select an action to take
action = agent.select_action(state, policy_net)
#Execute the action, the observe the next state and reward
reward = atarienv.execute_action(action)
next_state = atarienv.get_state().unsqueeze(0)
total_reward += reward.item()
#Store the experience in replay memory
memory.push(Experience(state, action, next_state, reward))