-
Notifications
You must be signed in to change notification settings - Fork 0
/
DQNPot.py
425 lines (313 loc) · 11.5 KB
/
DQNPot.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import os
from collections import OrderedDict, deque, namedtuple
from typing import List, Tuple
import gym
import numpy as np
import torch
from pytorch_lightning import LightningModule, Trainer
from pytorch_lightning.utilities import DistributedType
from torch import Tensor, nn
from torch.optim import Adam, Optimizer
from torch.utils.data import DataLoader
from torch.utils.data.dataset import IterableDataset
from pytorch_lightning.loggers import TensorBoardLogger
import csv
from pytorch_lightning.callbacks import Callback
from TetrisWrapperPot import TetrisWrapper
import numpy as np
from bayes_opt import BayesianOptimization
from bayes_opt.logger import JSONLogger
from bayes_opt.event import Events
PATH_DATASETS = os.environ.get("PATH_DATASETS", ".")
# In[2]:
class DQN(nn.Module):
"""Simple MLP network."""
def __init__(self, obs_size: int, n_actions: int, depth, hidden_size: int = 64):
"""
Args:
obs_size: observation/state size of the environment
n_actions: number of discrete actions available in the environment
hidden_size: size of hidden layers
"""
super().__init__()
if depth == 2:
self.net = nn.Sequential(
nn.Linear(obs_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, n_actions)
)
def forward(self, x):
return self.net(x.float())
# In[3]:
# Named tuple for storing experience steps gathered in training
Experience = namedtuple(
"Experience",
field_names=["state", "action", "reward", "done", "new_state"],
)
class ReplayBuffer:
"""Replay Buffer for storing past experiences allowing the agent to learn from them.
Args:
capacity: size of the buffer
"""
def __init__(self, capacity: int) -> None:
self.buffer = deque(maxlen=capacity)
def __len__(self) -> None:
return len(self.buffer)
def append(self, experience: Experience) -> None:
"""Add experience to the buffer.
Args:
experience: tuple (state, action, reward, done, new_state)
"""
self.buffer.append(experience)
def sample(self, batch_size: int) -> Tuple:
indices = np.random.choice(len(self.buffer), batch_size, replace=False)
states, actions, rewards, dones, next_states = zip(*(self.buffer[idx] for idx in indices))
return (
np.array(states),
np.array(actions),
np.array(rewards, dtype=np.float32),
np.array(dones, dtype=np.bool),
np.array(next_states),
)
# In[4]:
class RLDataset(IterableDataset):
"""Iterable Dataset containing the ExperienceBuffer which will be updated with new experiences during training.
Args:
buffer: replay buffer
sample_size: number of experiences to sample at a time
"""
def __init__(self, buffer: ReplayBuffer, sample_size) -> None:
self.buffer = buffer
self.sample_size = sample_size
def __iter__(self):
states, actions, rewards, dones, new_states = self.buffer.sample(self.sample_size)
for i in range(len(dones)):
yield states[i], actions[i], rewards[i], dones[i], new_states[i]
# In[5]:
from pathlib import Path
def pickFileName():
Path("log/trainingvals/").mkdir(parents=True, exist_ok=True)
files = os.listdir('log/trainingvals/')
return '{}.csv'.format(len(files)+1)
import random
class Agent:
"""Base Agent class handeling the interaction with the environment."""
def __init__(self, env: gym.Env, replay_buffer: ReplayBuffer) -> None:
"""
Args:
env: training environment
replay_buffer: replay buffer storing experiences
"""
self.env = env
self.replay_buffer = replay_buffer
self.reset()
self.state = self.env.reset()
def reset(self) -> None:
"""Resents the environment and updates the state."""
self.state = self.env.reset()
def get_action(self, net: nn.Module, epsilon: float) -> int:
if np.random.random() < epsilon:
action = random.randint(0,self.env.action_space.n-1)
#maybe with high epsilon at the start, replay buffer disproportionately fills up with pass, as pass is always a choice?
else:
state = torch.tensor(np.array([self.state]))
q_values = net(state)
_, action = torch.max(q_values, dim=1)
#print("picked : ",action)
action = int(action.item())
return action
@torch.no_grad()
def play_step(
self,
net: nn.Module,
epsilon: float = 0.0,
) -> Tuple[float, bool]:
action = self.get_action(net, epsilon)
# do step in the environment
new_state, reward, done, lines, _ = self.env.step(action)
#print("done , ",done)
exp = Experience(self.state, action, reward, done, new_state)
self.replay_buffer.append(exp)
self.state = new_state
if done:
#print("resetting")
self.reset()
return reward, done, lines
# In[6]:
class DQNLightning(LightningModule):
"""Basic DQN Model."""
def __init__(
self,
batch_size,
lr,
gamma,
sync_rate,
replay_size,
warm_start_steps,
eps_last_frame,
eps_start,
eps_end,
sample_size,
depth,
writer
) -> None:
self.writer = writer
writer = -1
super().__init__()
self.save_hyperparameters()
print("hparams:",self.hparams)
self.env = TetrisWrapper(grid_dims=(10, 10), piece_size=4)
self.env.reset()
obs_size = self.env.observation_space.shape[0]
n_actions = self.env.action_space.n
self.net = DQN(obs_size, n_actions, depth)
self.target_net = DQN(obs_size, n_actions, depth)
self.buffer = ReplayBuffer(self.hparams.replay_size)
self.agent = Agent(self.env, self.buffer)
self.epoch_rewards = []
self.avg_reward = 0
self.ep_reward = 0
self.done = 0
self.populate(self.hparams.warm_start_steps)
def populate(self, steps) -> None:
"""Carries out several random steps through the environment to initially fill up the replay buffer with
experiences.
Args:
steps: number of random steps to populate the buffer with
"""
print("populating...",steps)
for i in range(steps):
_, _, lines = self.agent.play_step(self.net, epsilon=1.0)
#print("Finished populating")
self.env.reset()
def forward(self, x: Tensor) -> Tensor:
"""Passes in a state x through the network and gets the q_values of each action as an output.
Args:
x: environment state
Returns:
q values
"""
output = self.net(x)
return output
def dqn_mse_loss(self, batch: Tuple[Tensor, Tensor]) -> Tensor:
"""Calculates the mse loss using a mini batch from the replay buffer.
Args:
batch: current mini batch of replay data
Returns:
loss
"""
states, actions, rewards, dones, next_states = batch
state_action_values = self.net(states).gather(1, actions.unsqueeze(-1)).squeeze(-1)
with torch.no_grad():
next_state_values = self.target_net(next_states).max(1)[0]
next_state_values[dones] = 0.0
next_state_values = next_state_values.detach()
expected_state_action_values = next_state_values * self.hparams.gamma + rewards
return nn.MSELoss()(state_action_values, expected_state_action_values)
def training_step(self, batch: Tuple[Tensor, Tensor], nb_batch) -> OrderedDict:
#TODO: FIIXXXX EPSILON YOU MONGREL
epsilon = max(
self.hparams.eps_end,
self.hparams.eps_start - self.global_step + 1 / self.hparams.eps_last_frame,
)
# step through environment with agent
reward, self.done, lines = self.agent.play_step(self.net, epsilon)
self.ep_reward += lines
# calculates training loss
loss = self.dqn_mse_loss(batch)
if self.done:
self.epoch_rewards.append(self.ep_reward)
self.ep_reward = 0
# Soft update of target network
if self.global_step % self.hparams.sync_rate == 0:
self.target_net.load_state_dict(self.net.state_dict())
self.log("train_loss", loss, on_step=True, on_epoch=True, prog_bar=True, logger=True)
self.log("epoch_reward", sum(self.epoch_rewards), on_step=True, on_epoch=False, prog_bar=True, logger=True)
self.writer.writerow([self.global_step, self.ep_reward, self.avg_reward])
return loss
def configure_optimizers(self) -> List[Optimizer]:
"""Initialize Adam optimizer."""
optimizer = Adam(self.net.parameters(), lr=self.hparams.lr)
return [optimizer]
def __dataloader(self) -> DataLoader:
"""Initialize the Replay Buffer dataset used for retrieving experiences."""
dataset = RLDataset(self.buffer, self.hparams.sample_size)
dataloader = DataLoader(
dataset=dataset,
batch_size=self.hparams.batch_size,
)
return dataloader
def train_dataloader(self) -> DataLoader:
"""Get train loader."""
return self.__dataloader()
class ReturnCallback(Callback):
def __init__(self ):
self.total = []
def on_train_epoch_end(self, trainer, pl_module):
if not pl_module.done:
pl_module.epoch_rewards.append(pl_module.ep_reward)
pl_module.ep_reward = 0
pl_module.env.reset()
pl_module.log("epoch_reward", sum(pl_module.epoch_rewards), on_step=False, on_epoch=True, prog_bar=True, logger=True)
pl_module.log("avg_ep_reward", sum(pl_module.epoch_rewards)/len(pl_module.epoch_rewards), on_step=False, on_epoch=True, prog_bar=True, logger=True)
pl_module.env.epoch_lines()
pl_module.epoch_rewards.clear()
def get_total(self):
return self.total
num_epochs = 25000
batch_size = 8
sync_rate = 16352
replay_size = 433020
warm_start_steps = 16352
eps_last_frame = replay_size
sample_size = 16352
depth = 2
lr = 5e-4
f = open('log/trainingvals/{}'.format(pickFileName()), 'w+')
writer = csv.writer(f)
model = DQNLightning(
batch_size,
lr,
0.99, #gamma
sync_rate,
replay_size,
warm_start_steps,
eps_last_frame,
1.0, #eps_start
0.01, #eps_end
sample_size,
depth,
writer
)
tb_logger = TensorBoardLogger("log/")
trainer = Trainer(
#accelerator="gpu",
#gpus=[0],
accelerator="cpu",
max_epochs=num_epochs,
val_check_interval=100,
logger=tb_logger,
callbacks=[ReturnCallback()]
)
trainer.fit(model)
f.close()
env = TetrisWrapper(grid_dims=(10, 10), piece_size=4)
totals = []
with torch.no_grad():
for i in range(10):
step = 0
done = 0
total = 0
state = env.reset()
while not done:
q_values = model(torch.Tensor(state))
_, action = torch.max(q_values, dim=0)
state, reward, done, lines, _ = env.step(action.item())
total += lines
totals.append(total)
print("average over games ",np.average(totals))