-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path0_DQN.py
303 lines (257 loc) · 9.46 KB
/
0_DQN.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
###########################################################################################
# Implementation of Deep Q-Learning Networks (DQN)
# Author for codes: Chu Kun(kun_chu@outlook.com)
# Paper: https://www.nature.com/articles/nature14236
# Reference: https://github.com/sungyubkim/Deep_RL_with_pytorch
###########################################################################################
import gym
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from replay_memory import ReplayBuffer, PrioritizedReplayBuffer
import random
import os
import pickle
import time
from collections import deque
import matplotlib.pyplot as plt
from wrappers import wrap, wrap_cover, SubprocVecEnv
import argparse
parser = argparse.ArgumentParser(description='Some settings of the experiment.')
parser.add_argument('games', type=str, nargs=1, help='name of the games. for example: Breakout')
args = parser.parse_args()
args.games = "".join(args.games)
'''DQN settings'''
# sequential images to define state
STATE_LEN = 4
# target policy sync interval
TARGET_REPLACE_ITER = 1
# simulator steps for start learning
LEARN_START = int(1e+3)
# (prioritized) experience replay memory size
MEMORY_CAPACITY = int(1e+5)
# simulator steps for learning interval
LEARN_FREQ = 4
'''Environment Settings'''
# number of environments for DQN
N_ENVS = 16
# Total simulation step
STEP_NUM = int(1e+8)
# gamma for MDP
GAMMA = 0.99
# visualize for agent playing
RENDERING = False
# openai gym env name
ENV_NAME = args.games+'NoFrameskip-v4'
env = SubprocVecEnv([wrap_cover(ENV_NAME) for i in range(N_ENVS)])
N_ACTIONS = env.action_space.n
N_STATES = env.observation_space.shape
'''Training settings'''
# check GPU usage
USE_GPU = torch.cuda.is_available()
print('USE GPU: '+str(USE_GPU))
# mini-batch size
BATCH_SIZE = 64
# learning rage
LR = 1e-4
# epsilon-greedy
EPSILON = 1.0
'''Save&Load Settings'''
# check save/load
SAVE = True
LOAD = False
# save frequency
SAVE_FREQ = int(1e+3)
# paths for predction net, target net, result log
PRED_PATH = './data/model/dqn_pred_net_o_'+args.games+'.pkl'
TARGET_PATH = './data/model/dqn_target_net_o_'+args.games+'.pkl'
RESULT_PATH = './data/plots/dqn_result_o_'+args.games+'.pkl'
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.feature_extraction = nn.Sequential(
# Conv2d(Input channels, Output channels, kernel_size, stride)
nn.Conv2d(STATE_LEN, 32, kernel_size=8, stride=4),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=4, stride=2),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, stride=1),
nn.ReLU(),
)
self.fc = nn.Linear(7 * 7 * 64, 512)
# action value
self.fc_q = nn.Linear(512, N_ACTIONS)
# Initialization
for m in self.modules():
if isinstance(m, nn.Conv2d):
# nn.init.orthogonal_(m.weight, gain = np.sqrt(2))
nn.init.xavier_normal_(m.weight)
if m.bias is not None:
nn.init.constant_(m.bias, 0.0)
elif isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight)
if m.bias is not None:
nn.init.constant_(m.bias, 0.0)
def forward(self, x):
# x.size(0) : minibatch size
mb_size = x.size(0)
x = self.feature_extraction(x / 255.0) # (m, 7 * 7 * 64)
x = x.view(x.size(0), -1)
x = F.relu(self.fc(x))
action_value = self.fc_q(x)
return action_value
def save(self, PATH):
torch.save(self.state_dict(),PATH)
def load(self, PATH):
self.load_state_dict(torch.load(PATH))
class DQN(object):
def __init__(self):
self.pred_net, self.target_net = ConvNet(), ConvNet()
# sync evac target
self.update_target(self.target_net, self.pred_net, 1.0)
# use gpu
if USE_GPU:
self.pred_net.cuda()
self.target_net.cuda()
# simulator step counter
self.memory_counter = 0
# target network step counter
self.learn_step_counter = 0
# loss function
self.loss_function = nn.MSELoss()
# ceate the replay buffer
self.replay_buffer = ReplayBuffer(MEMORY_CAPACITY)
# define optimizer
self.optimizer = torch.optim.Adam(self.pred_net.parameters(), lr=LR)
def update_target(self, target, pred, update_rate):
# update target network parameters using predcition network
for target_param, pred_param in zip(target.parameters(), pred.parameters()):
target_param.data.copy_((1.0 - update_rate) \
* target_param.data + update_rate*pred_param.data)
def save_model(self):
# save prediction network and target network
self.pred_net.save(PRED_PATH)
self.target_net.save(TARGET_PATH)
def load_model(self):
# load prediction network and target network
self.pred_net.load(PRED_PATH)
self.target_net.load(TARGET_PATH)
def choose_action(self, x, EPSILON):
# x:state
x = torch.FloatTensor(x)
# print(x.shape)
if USE_GPU:
x = x.cuda()
# epsilon-greedy policy
if np.random.uniform() >= EPSILON:
# greedy case
action_value = self.pred_net(x) # (N_ENVS, N_ACTIONS, N_QUANT)
action = torch.argmax(action_value, dim=1).data.cpu().numpy()
else:
# random exploration case
action = np.random.randint(0, N_ACTIONS, (x.size(0)))
return action
def store_transition(self, s, a, r, s_, done):
self.memory_counter += 1
self.replay_buffer.add(s, a, r, s_, float(done))
def learn(self):
self.learn_step_counter += 1
# target parameter update
if self.learn_step_counter % TARGET_REPLACE_ITER == 0:
self.update_target(self.target_net, self.pred_net, 1e-2)
b_s, b_a, b_r, b_s_, b_d = self.replay_buffer.sample(BATCH_SIZE)
# b_w, b_idxes = np.ones_like(b_r), None
b_s = torch.FloatTensor(b_s)
b_a = torch.LongTensor(b_a)
b_r = torch.FloatTensor(b_r)
b_s_ = torch.FloatTensor(b_s_)
b_d = torch.FloatTensor(b_d)
if USE_GPU:
b_s, b_a, b_r, b_s_, b_d = b_s.cuda(), b_a.cuda(), b_r.cuda(), b_s_.cuda(), b_d.cuda()
# action value for current state
q_eval = self.pred_net(b_s)
mb_size = q_eval.size(0)
q_eval = torch.stack([q_eval[i][b_a[i]] for i in range(mb_size)])
# optimal action value for current state
q_next = self.target_net(b_s_)
# best_actions = q_next.argmax(dim=1)
# q_next = torch.stack([q_next[i][best_actions[i]] for i in range(mb_size)])
q_next = torch.max(q_next, -1)[0]
q_target = b_r + GAMMA * (1. - b_d) * q_next
q_target = q_target.detach()
# loss
loss = self.loss_function(q_eval, q_target)
# backprop loss
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
return loss
dqn = DQN()
# model load with check
if LOAD and os.path.isfile(PRED_PATH) and os.path.isfile(TARGET_PATH):
dqn.load_model()
pkl_file = open(RESULT_PATH,'rb')
result = pickle.load(pkl_file)
pkl_file.close()
print('Load complete!')
else:
result = []
print('Initialize results!')
print('Collecting experience...')
# episode step for accumulate reward
epinfobuf = deque(maxlen=100)
# check learning time
start_time = time.time()
# env reset
s = np.array(env.reset())
# print(s.shape)
# for step in tqdm(range(1, STEP_NUM//N_ENVS+1)):
for step in range(1, STEP_NUM//N_ENVS+1):
a = dqn.choose_action(s, EPSILON)
# print('a',a)
# take action and get next state
s_, r, done, infos = env.step(a)
# log arrange
for info in infos:
maybeepinfo = info.get('episode')
if maybeepinfo: epinfobuf.append(maybeepinfo)
s_ = np.array(s_)
# clip rewards for numerical stability
clip_r = np.sign(r)
# store the transition
for i in range(N_ENVS):
dqn.store_transition(s[i], a[i], clip_r[i], s_[i], done[i])
# annealing the epsilon(exploration strategy)
if step <= int(1e+4):
# linear annealing to 0.9 until million step
EPSILON -= 0.9/1e+4
elif step <= int(2e+4):
# else:
# linear annealing to 0.99 until the end
EPSILON -= 0.09/1e+4
# if memory fill 50K and mod 4 = 0(for speed issue), learn pred net
if (LEARN_START <= dqn.memory_counter) and (dqn.memory_counter % LEARN_FREQ == 0):
loss = dqn.learn()
# print log and save
if step % SAVE_FREQ == 0:
# check time interval
time_interval = round(time.time() - start_time, 2)
# calc mean return
mean_100_ep_return = round(np.mean([epinfo['r'] for epinfo in epinfobuf]),2)
result.append(mean_100_ep_return)
# print log
print('Used Step: ',dqn.memory_counter,
'| EPS: ', round(EPSILON, 3),
# '| Loss: ', loss,
'| Mean ep 100 return: ', mean_100_ep_return,
'| Used Time:',time_interval)
# save model
dqn.save_model()
pkl_file = open(RESULT_PATH, 'wb')
pickle.dump(np.array(result), pkl_file)
pkl_file.close()
s = s_
if RENDERING:
env.render()
print("The training is done!")