-
Notifications
You must be signed in to change notification settings - Fork 0
/
Atari_Breakout
339 lines (267 loc) · 10.6 KB
/
Atari_Breakout
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
import gym
from gym.wrappers.monitoring.video_recorder import VideoRecorder
import math
import random
import os, sys
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import pickle
import gc
from collections import namedtuple
from itertools import count
from PIL import Image
from tqdm import tqdm, trange
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision.transforms as T
seed = 1234
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.enabled = False
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
env = gym.make('Breakout-v0')
# env = gym.make('BipedalWalker-v3')
is_ipython = 'inline' in matplotlib.get_backend()
plt.ion()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
num_stack = 4
batch_size = 32
num_episode = 10000
Replay_capacity = 80000
Learning_start = 50000
EPS_START = 1.
EPS_END = 0.1
EPS_DECAY = 1000000
EPS_MEAN = 100
TARGET_EPS = 0.05
TARGET_UPDATE = 10000
GAMMA = 0.99
lr = 0.00025
Transition = namedtuple('Transition',
('current_state', 'action', 'next_state', 'reward'))
class ReplayMemory(object):
def __init__(self, capacity):
self.capacity = capacity
self.memory = []
self.position = 0
def push(self, *args):
"""Saves a transition."""
if len(self.memory) < self.capacity:
self.memory.append(None)
self.memory[self.position] = Transition(*args)
self.position = (self.position + 1) % self.capacity
def sample(self, batch_size):
return random.sample(self.memory, batch_size)
def __len__(self):
return len(self.memory)
def get_screen():
screen = env.render(mode='rgb_array').transpose((2, 0, 1))
screen = screen[:, 32:210 - 15, 8:160 - 8]
screen = torch.from_numpy(screen.astype(np.float))
screen = torch.mean(screen, dim=0, keepdim=True)
resize = T.Compose([T.ToPILImage(),
T.Resize((84, 84), interpolation=Image.BILINEAR),
T.ToTensor()])
return resize(screen)
def stack_screen():
global state_list, screen
state_list.append(screen)
if len(state_list) > num_stack:
state_list = state_list[1:]
state_tensor = torch.zeros(num_stack, 84, 84)
for idx, sc in enumerate(state_list):
state_tensor[idx] = sc
return state_list, state_tensor.unsqueeze(dim=0)
class DQN(nn.Module):
def __init__(self, outputs):
super(DQN, self).__init__()
self.conv1 = nn.Conv2d(4, 32, kernel_size=8, stride=4)
self.relu1 = nn.ReLU()
self.conv2 = nn.Conv2d(32, 64, kernel_size=4, stride=2)
self.relu2 = nn.ReLU()
self.conv3 = nn.Conv2d(64, 64, kernel_size=3, stride=1)
self.relu3 = nn.ReLU()
self.ln1 = nn.Linear(7 * 7 * 64, 512)
self.relu4 = nn.ReLU()
self.ln2 = nn.Linear(512, outputs)
def forward(self, input):
_ = self.conv1(input)
_ = self.relu1(_)
_ = self.conv2(_)
_ = self.relu2(_)
_ = self.conv3(_)
_ = self.relu3(_)
_ = nn.Flatten()(_)
_ = self.ln1(_)
_ = self.relu4(_)
output = self.ln2(_)
return output
def select_action(state, learn_frames, policy='Policy'):
sample = random.random()
eps_threshold = np.clip(EPS_START - (EPS_START - EPS_END) * (learn_frames / EPS_DECAY), 0.1, 1)
if policy == 'Target':
eps_threshold = TARGET_EPS
if sample > eps_threshold:
with torch.no_grad():
# t.max(1) will return largest column value of each row.
# second column on max result is index of where max element was
# found, so we pick action with the larger expected reward.
return policy_net(state.to(device)).max(1)[1].view(1, 1)
else:
return torch.tensor([[random.randrange(n_actions)]], device=device, dtype=torch.long)
def plot_scores():
plt.figure(2)
plt.clf()
scores_t = torch.tensor(episode_scores, dtype=torch.float)
plt.title('Training & Test')
plt.xlabel('Episode')
plt.ylabel('Score')
plt.plot(scores_t.numpy())
# Take 100 episode averages and plot them too
if len(scores_t) >= EPS_MEAN:
means = scores_t.unfold(0, EPS_MEAN, 1).mean(1).view(-1)
means = torch.cat((torch.zeros(EPS_MEAN - 1), means))
plt.plot(means.numpy())
if len(test_scores) > 0:
plt.plot(test_ep, test_scores, 'r*')
plt.pause(0.001) # pause a bit so that plots are updated
# if is_ipython:
# display.clear_output(wait=True)
# display.display(plt.gcf())
memory = ReplayMemory(Replay_capacity)
n_actions = env.action_space.n
policy_net = DQN(n_actions).to(device)
target_net = DQN(n_actions).to(device)
target_net.load_state_dict(policy_net.state_dict())
target_net.eval()
optimizer = optim.RMSprop(policy_net.parameters(), lr=lr, eps=0.001, alpha=0.95)
crit = nn.MSELoss()
learn_frames = 0
episode_scores = []
test_scores = []
test_ep = []
num_frames = 0
print(f'~{Learning_start} case: Making initial replay memory by random policy')
print(
f'{Learning_start}~{Learning_start + EPS_DECAY} frames: Learning by e-greedy policy with decreased linearly from {EPS_START} to {EPS_END}')
print(f'Target policy is e-greedy with e = {TARGET_EPS}')
for i_episode in trange(num_episode + 220):
video_recorder = None
update = False
env.reset()
state_list = []
state_tensor = []
screen = get_screen()
state_list, state_tensor = stack_screen()
score = 0
if i_episode % 100 == 0:
gc.collect()
while True:
if len(state_list) < num_stack:
# env.step(torch.tensor([[random.randrange(n_actions)]], device=device, dtype=torch.long))
env.step(0)
screen = get_screen()
state_list, state_tensor = stack_screen()
continue
num_frames = num_frames + 1
current_state = state_tensor
if num_frames < Learning_start:
action = select_action(state_tensor, 0)
else:
learn_frames = learn_frames + 1
if learn_frames % TARGET_UPDATE == 0 and learn_frames != 0:
update = True
action = select_action(state_tensor, learn_frames)
_, reward, done, _ = env.step(action.item())
if reward != 0:
print(reward)
score = score + reward
screen = get_screen()
env.render()
state_list, state_tensor = stack_screen()
reward = torch.tensor([reward])
if not done:
next_state = state_tensor
else:
next_state = None
memory.push(current_state, action, next_state, reward)
if num_frames == Learning_start:
initset_done_ep = i_episode
print(
f'Learning starts at {Learning_start} case ({i_episode}th iteration(ep.), but not included in a progressive graph).')
# with open('C:\\Users\\YKW\\PycharmProjects\\Reinforcement\\Breakout_v0\\fixed_init_states.pickle',
# 'wb') as f:
# pickle.dump(memory.memory, f)
if num_frames == Learning_start + EPS_DECAY:
print(f'Epsilon decreasing stops with 0.1 at {Learning_start + EPS_DECAY}th, {i_episode}th episode')
if num_frames < Learning_start:
if done:
break
else:
continue
if learn_frames % 4 == 0:
transitions = memory.sample(batch_size)
batch = Transition(*zip(*transitions))
non_final_mask = torch.tensor(tuple(map(lambda s: s is not None,
batch.next_state)), device=device, dtype=torch.bool)
non_final_next_states = torch.cat([s for s in batch.next_state if s is not None]).to(device)
state_batch = torch.cat(batch.current_state).to(device)
action_batch = torch.cat(batch.action).to(device)
reward_batch = torch.cat(batch.reward).to(device)
state_action_values = policy_net(state_batch).gather(1, action_batch)
next_state_values = torch.zeros(batch_size).to(device)
next_state_values[non_final_mask] = target_net(non_final_next_states).max(1)[0].detach()
expected_state_action_values = (next_state_values * GAMMA) + reward_batch
loss = crit(state_action_values, expected_state_action_values.unsqueeze(1))
optimizer.zero_grad()
loss.backward()
for param in policy_net.parameters():
param.grad.data.clamp_(-1, 1)
optimizer.step()
if done:
episode_scores.append(score)
plot_scores()
break
# if i_episode % TARGET_UPDATE == 0 and num_frames >= Learning_start:
if update:
target_net.load_state_dict(policy_net.state_dict())
video_path = f'C:\\Users\\YKW\\PycharmProjects\\Reinforcement\\Breakout_v0\\record\\{str(int(i_episode - initset_done_ep))}.mp4'
video_recorder = VideoRecorder(env, path=video_path, enabled=True)
with torch.no_grad():
env.reset()
test_score = 0
state_list = []
state_tensor = []
screen = get_screen()
state_list, state_tensor = stack_screen()
while True:
if len(state_list) < num_stack:
env.step(0)
screen = get_screen()
state_list, state_tensor = stack_screen()
continue
current_state = state_tensor
action = select_action(state_tensor, learn_frames, policy='Target')
_, reward, done, _ = env.step(action.item())
video_recorder.capture_frame()
test_score = test_score + reward
screen = get_screen()
env.render()
state_list, state_tensor = stack_screen()
if done:
test_scores.append(test_score)
test_ep.append(i_episode - initset_done_ep)
plot_scores()
plt.savefig('C:\\Users\\YKW\\PycharmProjects\\Reinforcement\\Breakout_v0\\progressive.png')
break
video_recorder.close()
video_recorder.enabled = False
os.rename(f'C:\\Users\\YKW\\PycharmProjects\\Reinforcement\\Breakout_v0\\record\\{str(int(i_episode - initset_done_ep))}.mp4',
f'C:\\Users\\YKW\\PycharmProjects\\Reinforcement\\Breakout_v0\\record\\{str(int(test_score))}_{str(int(i_episode - initset_done_ep))}.mp4')