-
Notifications
You must be signed in to change notification settings - Fork 0
/
dqn.py
244 lines (176 loc) · 7.25 KB
/
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
import sys
import argparse
import pickle
import gym
import torch
import torch.optim as optim
from torch.autograd import Variable
import numpy as np
from models import DQN
from replay_memory import Transition, ReplayMemory
from utils import preprocess_state
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('-g', '--game', action='store', dest='game', default='Breakout-v0')
parser.add_argument('-w', '--warmstart', action='store_true', dest='warm_start', default=False)
parser.add_argument('-r', '--render', action='store_true', dest='render', default=False)
args = parser.parse_args()
game = args.game
warm_start = args.warm_start
render = args.render
return game, warm_start, render
def initialize(game, model_name, warm_start):
# Initialize environment
env = gym.make(game)
num_actions = env.action_space.n
# Initialize constants
num_frames = 4
capacity = int(1e4)
# Cold start
if not warm_start:
# Initialize model
model = DQN(in_channels=num_frames, num_actions=num_actions)
optimizer = optim.RMSprop(model.parameters(), lr=1.0e-4, weight_decay=0.01)
# Initialize replay memory
memory_buffer = ReplayMemory(capacity)
# Initialize statistics
running_reward = None
running_rewards = []
# Warm start
if warm_start:
data_file = 'results/{}_{}.p'.format(game, model_name)
try:
with open(data_file, 'rb') as f:
running_rewards = pickle.load(f)
running_reward = running_rewards[-1]
prior_eps = len(running_rewards)
model_file = 'saved_models/{}_{}_ep_{}.p'.format(game, model_name, prior_eps)
with open(model_file, 'rb') as f:
saved_model = pickle.load(f)
model, optimizer, memory_buffer = saved_model
except OSError:
print('Saved file not found. Creating new cold start model.')
model = DQN(in_channels=num_frames, num_actions=num_actions)
optimizer = optim.RMSprop(model.parameters(), lr=1.0e-4, weight_decay=0.01)
# Initialize replay memory
memory_buffer = ReplayMemory(capacity)
running_reward = None
running_rewards = []
cuda = torch.cuda.is_available()
if cuda:
model = model.cuda()
criterion = torch.nn.MSELoss()
return env, model, optimizer, criterion, memory_buffer, cuda, running_reward, running_rewards
def select_epilson_greedy_action(model, state, t, cuda):
sample = np.random.rand()
# Anneal epsilon from 1.0 down to 0.05 over 20,000 iterations
epsilon = max(0.05, 1.0 - 0.95 * (t / 2.0e4))
if epsilon > sample:
# Select random Action
action = np.random.randint(model.num_actions)
else:
# Select best action
num_frames, height, width = state.shape
state = torch.FloatTensor(state.reshape(-1, num_frames, height, width))
if cuda:
state = state.cuda()
state = Variable(state)
action = model(state).data.max(1)[1]
return action
def main():
model_name = 'dqn'
# Parse arguments
game, warm_start, render = parse_arguments()
# Initialize enviroment/model
data = initialize(game, model_name, warm_start)
env, model, optimizer, criterion, memory_buffer, cuda, running_reward, running_rewards = data
# Initialize constants
max_episodes = 500000
batch_size = 10
gamma = 0.95
num_frames = 4
for ep in range(max_episodes):
state = env.reset()
state = preprocess_state(state)
state = np.stack([state]*num_frames)
reward_sum = 0.0
while True:
# render frame if render argument was passed
if render:
env.render()
# Select action
action = select_epilson_greedy_action(model, state, ep, cuda)
# Perform step
next_state, reward, done, info = env.step(action)
next_state = preprocess_state(next_state)
next_state = np.stack([next_state]*num_frames)
next_state[1:, :, :] = state[:-1, :, :]
reward_sum += reward
# Add transition to replay memory
transition = Transition(state, action, next_state, reward, done)
memory_buffer.push(transition)
# Update state
state = next_state
# Sample mini-batch from replay memory_buffer
batch = memory_buffer.sample(batch_size, replace=True)
# Compute targets
targets = np.zeros((batch_size,), dtype=float)
for i, transition in enumerate(batch):
targets[i] = transition.reward
if not transition.done:
next_state = transition.next_state
num_frames, height, width = next_state.shape
next_state = next_state.reshape(-1, num_frames, height, width)
next_state = torch.FloatTensor(next_state)
if cuda:
next_state = next_state.cuda()
next_state = Variable(next_state)
targets[i] += gamma * model(next_state).data.max(1)[0]
targets = torch.FloatTensor(targets)
if cuda:
targets = targets.cuda()
targets = Variable(targets)
# Compute predictions
model.zero_grad()
states = [transition.state for transition in batch]
states = torch.FloatTensor(states)
if cuda:
states = states.cuda()
states = Variable(states)
actions = [int(transition.action) for transition in batch]
actions = torch.LongTensor(actions)
if cuda:
actions = actions.cuda()
actions = Variable(actions)
outputs = model(states).gather(1, actions.unsqueeze(1))
# Perform gradient descent step
loss = criterion(outputs.view(batch_size), targets)
loss.backward()
# Clip gradient at 20,000
# torch.nn.utils.clip_grad_norm(model.parameters(), 20000)
optimizer.step()
if done:
break
# Compute/display statistics
if running_reward is None:
running_reward = reward_sum
else:
running_reward = running_reward * 0.99 + reward_sum * 0.01
running_rewards.append(running_reward)
verbose_str = 'Episode {} complete'.format(ep+1)
verbose_str += '\tReward total:{}'.format(reward_sum)
verbose_str += '\tRunning mean: {:.4}'.format(running_reward)
sys.stdout.write('\r' + verbose_str)
sys.stdout.flush()
# Save model every 1000 episodes
if (ep+1) % 1000 == 0:
model_file = 'saved_models/{}_{}_ep_{}.p'.format(game, model_name, ep+1)
with open(model_file, 'wb') as f:
pickle.dump((model.cpu(), optimizer, memory_buffer), f)
if cuda:
model = model.cuda()
data_file = 'results/{}_{}.p'.format(game, model_name)
with open(data_file, 'wb') as f:
pickle.dump(running_rewards, f)
if __name__ == '__main__':
main()