-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
learning_pytorch.py
executable file
·359 lines (289 loc) · 10.7 KB
/
learning_pytorch.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
#!/usr/bin/env python3
# E. Culurciello, L. Mueller, Z. Boztoprak
# December 2020
import itertools as it
import os
import random
from collections import deque
from time import sleep, time
import numpy as np
import skimage.transform
import torch
import torch.nn as nn
import torch.optim as optim
from tqdm import trange
import vizdoom as vzd
# Q-learning settings
learning_rate = 0.00025
discount_factor = 0.99
train_epochs = 5
learning_steps_per_epoch = 2000
replay_memory_size = 10000
# NN learning settings
batch_size = 64
# Training regime
test_episodes_per_epoch = 100
# Other parameters
frame_repeat = 12
resolution = (30, 45)
episodes_to_watch = 10
model_savefile = "./model-doom.pth"
save_model = True
load_model = False
skip_learning = False
# Configuration file path
config_file_path = os.path.join(vzd.scenarios_path, "simpler_basic.cfg")
# config_file_path = os.path.join(vzd.scenarios_path, "rocket_basic.cfg")
# config_file_path = os.path.join(vzd.scenarios_path, "basic.cfg")
# Uses GPU if available
if torch.cuda.is_available():
DEVICE = torch.device("cuda")
torch.backends.cudnn.benchmark = True
else:
DEVICE = torch.device("cpu")
def preprocess(img):
"""Down samples image to resolution"""
img = skimage.transform.resize(img, resolution)
img = img.astype(np.float32)
img = np.expand_dims(img, axis=0)
return img
def create_simple_game():
print("Initializing doom...")
game = vzd.DoomGame()
game.load_config(config_file_path)
game.set_window_visible(False)
game.set_mode(vzd.Mode.PLAYER)
game.set_screen_format(vzd.ScreenFormat.GRAY8)
game.set_screen_resolution(vzd.ScreenResolution.RES_640X480)
game.init()
print("Doom initialized.")
return game
def test(game, agent):
"""Runs a test_episodes_per_epoch episodes and prints the result"""
print("\nTesting...")
test_scores = []
for test_episode in trange(test_episodes_per_epoch, leave=False):
game.new_episode()
while not game.is_episode_finished():
state = preprocess(game.get_state().screen_buffer)
best_action_index = agent.get_action(state)
game.make_action(actions[best_action_index], frame_repeat)
r = game.get_total_reward()
test_scores.append(r)
test_scores = np.array(test_scores)
print(
"Results: mean: {:.1f} +/- {:.1f},".format(
test_scores.mean(), test_scores.std()
),
"min: %.1f" % test_scores.min(),
"max: %.1f" % test_scores.max(),
)
def run(game, agent, actions, num_epochs, frame_repeat, steps_per_epoch=2000):
"""
Run num epochs of training episodes.
Skip frame_repeat number of frames after each action.
"""
start_time = time()
for epoch in range(num_epochs):
game.new_episode()
train_scores = []
global_step = 0
print(f"\nEpoch #{epoch + 1}")
for _ in trange(steps_per_epoch, leave=False):
state = preprocess(game.get_state().screen_buffer)
action = agent.get_action(state)
reward = game.make_action(actions[action], frame_repeat)
done = game.is_episode_finished()
if not done:
next_state = preprocess(game.get_state().screen_buffer)
else:
next_state = np.zeros((1, 30, 45)).astype(np.float32)
agent.append_memory(state, action, reward, next_state, done)
if global_step > agent.batch_size:
agent.train()
if done:
train_scores.append(game.get_total_reward())
game.new_episode()
global_step += 1
agent.update_target_net()
train_scores = np.array(train_scores)
print(
"Results: mean: {:.1f} +/- {:.1f},".format(
train_scores.mean(), train_scores.std()
),
"min: %.1f," % train_scores.min(),
"max: %.1f," % train_scores.max(),
)
test(game, agent)
if save_model:
print("Saving the network weights to:", model_savefile)
torch.save(agent.q_net, model_savefile)
print("Total elapsed time: %.2f minutes" % ((time() - start_time) / 60.0))
game.close()
return agent, game
class DuelQNet(nn.Module):
"""
This is Duel DQN architecture.
see https://arxiv.org/abs/1511.06581 for more information.
"""
def __init__(self, available_actions_count):
super().__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(1, 8, kernel_size=3, stride=2, bias=False),
nn.BatchNorm2d(8),
nn.ReLU(),
)
self.conv2 = nn.Sequential(
nn.Conv2d(8, 8, kernel_size=3, stride=2, bias=False),
nn.BatchNorm2d(8),
nn.ReLU(),
)
self.conv3 = nn.Sequential(
nn.Conv2d(8, 8, kernel_size=3, stride=1, bias=False),
nn.BatchNorm2d(8),
nn.ReLU(),
)
self.conv4 = nn.Sequential(
nn.Conv2d(8, 16, kernel_size=3, stride=1, bias=False),
nn.BatchNorm2d(16),
nn.ReLU(),
)
self.state_fc = nn.Sequential(nn.Linear(96, 64), nn.ReLU(), nn.Linear(64, 1))
self.advantage_fc = nn.Sequential(
nn.Linear(96, 64), nn.ReLU(), nn.Linear(64, available_actions_count)
)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = x.view(-1, 192)
x1 = x[:, :96] # input for the net to calculate the state value
x2 = x[:, 96:] # relative advantage of actions in the state
state_value = self.state_fc(x1).reshape(-1, 1)
advantage_values = self.advantage_fc(x2)
x = state_value + (
advantage_values - advantage_values.mean(dim=1).reshape(-1, 1)
)
return x
class DQNAgent:
def __init__(
self,
action_size,
memory_size,
batch_size,
discount_factor,
lr,
load_model,
epsilon=1,
epsilon_decay=0.9996,
epsilon_min=0.1,
):
self.action_size = action_size
self.epsilon = epsilon
self.epsilon_decay = epsilon_decay
self.epsilon_min = epsilon_min
self.batch_size = batch_size
self.discount = discount_factor
self.lr = lr
self.memory = deque(maxlen=memory_size)
self.criterion = nn.MSELoss()
if load_model:
print("Loading model from: ", model_savefile)
self.q_net = torch.load(model_savefile)
self.target_net = torch.load(model_savefile)
self.epsilon = self.epsilon_min
else:
print("Initializing new model")
self.q_net = DuelQNet(action_size).to(DEVICE)
self.target_net = DuelQNet(action_size).to(DEVICE)
self.opt = optim.SGD(self.q_net.parameters(), lr=self.lr)
def get_action(self, state):
if np.random.uniform() < self.epsilon:
return random.choice(range(self.action_size))
else:
state = np.expand_dims(state, axis=0)
state = torch.from_numpy(state).float().to(DEVICE)
action = torch.argmax(self.q_net(state)).item()
return action
def update_target_net(self):
self.target_net.load_state_dict(self.q_net.state_dict())
def append_memory(self, state, action, reward, next_state, done):
self.memory.append((state, action, reward, next_state, done))
def train(self):
batch = random.sample(self.memory, self.batch_size)
batch = np.array(batch, dtype=object)
states = np.stack(batch[:, 0]).astype(float)
actions = batch[:, 1].astype(int)
rewards = batch[:, 2].astype(float)
next_states = np.stack(batch[:, 3]).astype(float)
dones = batch[:, 4].astype(bool)
not_dones = ~dones
row_idx = np.arange(self.batch_size) # used for indexing the batch
# value of the next states with double q learning
# see https://arxiv.org/abs/1509.06461 for more information on double q learning
with torch.no_grad():
next_states = torch.from_numpy(next_states).float().to(DEVICE)
idx = row_idx, np.argmax(self.q_net(next_states).cpu().data.numpy(), 1)
next_state_values = self.target_net(next_states).cpu().data.numpy()[idx]
next_state_values = next_state_values[not_dones]
# this defines y = r + discount * max_a q(s', a)
q_targets = rewards.copy()
q_targets[not_dones] += self.discount * next_state_values
q_targets = torch.from_numpy(q_targets).float().to(DEVICE)
# this selects only the q values of the actions taken
idx = row_idx, actions
states = torch.from_numpy(states).float().to(DEVICE)
action_values = self.q_net(states)[idx].float().to(DEVICE)
self.opt.zero_grad()
td_error = self.criterion(q_targets, action_values)
td_error.backward()
self.opt.step()
if self.epsilon > self.epsilon_min:
self.epsilon *= self.epsilon_decay
else:
self.epsilon = self.epsilon_min
if __name__ == "__main__":
# Initialize game and actions
game = create_simple_game()
n = game.get_available_buttons_size()
actions = [list(a) for a in it.product([0, 1], repeat=n)]
# Initialize our agent with the set parameters
agent = DQNAgent(
len(actions),
lr=learning_rate,
batch_size=batch_size,
memory_size=replay_memory_size,
discount_factor=discount_factor,
load_model=load_model,
)
# Run the training for the set number of epochs
if not skip_learning:
agent, game = run(
game,
agent,
actions,
num_epochs=train_epochs,
frame_repeat=frame_repeat,
steps_per_epoch=learning_steps_per_epoch,
)
print("======================================")
print("Training finished. It's time to watch!")
# Reinitialize the game with window visible
game.close()
game.set_window_visible(True)
game.set_mode(vzd.Mode.ASYNC_PLAYER)
game.init()
for _ in range(episodes_to_watch):
game.new_episode()
while not game.is_episode_finished():
state = preprocess(game.get_state().screen_buffer)
best_action_index = agent.get_action(state)
# Instead of make_action(a, frame_repeat) in order to make the animation smooth
game.set_action(actions[best_action_index])
for _ in range(frame_repeat):
game.advance_action()
# Sleep between episodes
sleep(1.0)
score = game.get_total_reward()
print("Total score: ", score)