-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_atari.py
296 lines (249 loc) · 10.9 KB
/
train_atari.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
import os
from collections import deque
from typing import List
import numpy as np
import torch
import wandb
from level_replay import utils
from level_replay.algo.buffer import make_buffer
from level_replay.algo.policy import DQNAgent
from level_replay.atari_args import parser
os.environ["OMP_NUM_THREADS"] = "1"
last_checkpoint_time = None
def train(args, seeds):
global last_checkpoint_time
args.cuda = not args.no_cuda and torch.cuda.is_available()
args.device = torch.device("cuda:0" if args.cuda else "cpu")
if "cuda" in args.device.type:
print("Using CUDA\n")
args.optimizer_parameters = {"lr": args.learning_rate, "eps": args.adam_eps}
args.seeds = seeds
args.sge_job_id = int(os.environ.get("JOB_ID", -1))
args.sge_task_id = int(os.environ.get("SGE_TASK_ID", -1))
torch.set_num_threads(1)
utils.seed(args.seed)
wandb.init(
settings=wandb.Settings(start_method="fork"),
project=args.wandb_project,
entity="andyehrenberg",
config=vars(args),
tags=["ddqn", "procgen"] + (args.wandb_tags.split(",") if args.wandb_tags else []),
group=args.wandb_group,
)
wandb.run.name = (
f"dqn-{args.env_name}"
+ f"{'-PER' if args.PER else ''}"
+ f"{'-dueling' if args.dueling else ''}"
+ f"{'-CQL' if args.cql else ''}"
+ f"{'-qrdqn' if args.qrdqn else ''}"
+ f"{'-c51' if args.c51 else ''}"
+ f"{'-noisylayers' if args.noisy_layers else ''}"
)
atari_preprocessing = {
"frame_skip": 4,
"frame_size": 84,
"state_history": 4,
"done_on_life_loss": False,
"reward_clipping": True,
"max_episode_timesteps": 27e3,
}
envs = AtariVecEnv(args.env_name, seeds, args.num_processes, args.device, atari_preprocessing)
agent = DQNAgent(args, envs)
state = envs.reset()
replay_buffer = make_buffer(args, envs, atari=True)
episode_reward = 0
state_deque: List[deque] = [deque(maxlen=args.multi_step) for _ in range(args.num_processes)]
reward_deque: List[deque] = [deque(maxlen=args.multi_step) for _ in range(args.num_processes)]
action_deque: List[deque] = [deque(maxlen=args.multi_step) for _ in range(args.num_processes)]
num_steps = int(args.T_max // args.num_processes)
epsilon_start = args.initial_eps
epsilon_final = args.end_eps
epsilon_decay = args.eps_decay_period
def epsilon(t):
return epsilon_final + (epsilon_start - epsilon_final) * np.exp(
-1.0 * (t - args.start_timesteps) / epsilon_decay
)
episode_reward = 0
episode_num = 0
for t in range(num_steps):
if t < args.start_timesteps:
action = (
torch.LongTensor([envs.action_space.sample() for _ in range(args.num_processes)])
.reshape(-1, 1)
.to(args.device)
)
value = agent.get_value(state)
else:
cur_epsilon = epsilon(t)
action, value = agent.select_action(state)
for i in range(args.num_processes):
if np.random.uniform() < cur_epsilon:
action[i] = torch.LongTensor([envs.action_space.sample()]).to(args.device)
# Perform action and log results
next_state, reward, done, infos, levels = envs.step(action)
for i, info in enumerate(infos):
state_deque[i].append(state[i])
reward_deque[i].append(reward[i])
action_deque[i].append(action[i])
if len(state_deque[i]) == args.multi_step or done[i]:
n_reward = multi_step_reward(reward_deque[i], args.gamma)
n_state = state_deque[i][0]
n_action = action_deque[i][0]
replay_buffer.add(
n_state,
n_action,
next_state[i],
n_reward,
np.uint8(done[i]),
levels[i],
)
if done[i]:
reward_deque_i = list(reward_deque[i])
for j in range(1, len(reward_deque_i)):
n_reward = multi_step_reward(reward_deque_i[j:], args.gamma)
n_state = state_deque[i][j]
n_action = action_deque[i][j]
replay_buffer.add(
n_state,
n_action,
next_state[i],
n_reward,
np.uint8(done[i]),
levels[i],
)
episode_reward = info["return"]
episode_num += 1
wandb.log({"Train Episode Returns": episode_reward}, step=t * args.num_processes)
state_deque[i].clear()
reward_deque[i].clear()
action_deque[i].clear()
state = next_state
# Train agent after collecting sufficient data
if t % args.train_freq == 0 and t >= args.start_timesteps:
loss, grad_magnitude = agent.train(replay_buffer)
wandb.log({"Value Loss": loss, "Gradient magnitude": grad_magnitude}, step=t * args.num_processes)
if t % 10000 == 0:
effective_rank = agent.Q.effective_rank()
wandb.log({"Effective Rank of DQN": effective_rank}, step=t * args.num_processes)
if (t >= args.start_timesteps and (t + 1) % args.eval_freq == 0) or t == num_steps - 1:
train_eval_episode_rewards = eval_policy(args, agent, envs, atari_preprocessing, True)
test_eval_episode_rewards = eval_policy(args, agent, envs, atari_preprocessing, False)
wandb.log(
{
"Train Evaluation Returns": np.mean(train_eval_episode_rewards),
"Test Evaluation Returns": np.mean(test_eval_episode_rewards),
},
step=t * args.num_processes,
)
def eval_policy(args, policy, envs, atari_preprocessing, train_seeds=True, num_episodes=10, num_processes=1):
atari_preprocessing = {
"frame_skip": 4,
"frame_size": 84,
"state_history": 4,
"done_on_life_loss": False,
"reward_clipping": True,
"max_episode_timesteps": 27e3,
}
if train_seeds:
eval_env = AtariVecEnv(envs.env_name, envs.seeds, num_processes, envs.device, atari_preprocessing)
else:
eval_env = AtariVecEnv(
envs.env_name, envs.test_seeds, num_processes, envs.device, atari_preprocessing
)
eval_episode_rewards: List[float] = []
state = eval_env.reset()
while len(eval_episode_rewards) < num_episodes:
if np.random.uniform() < args.eval_eps:
action = (
torch.LongTensor([eval_env.action_space.sample() for _ in range(num_processes)])
.reshape(-1, 1)
.to(args.device)
)
else:
with torch.no_grad():
action, _ = policy.select_action(state, eval=True)
state, _, done, infos, _ = eval_env.step(action)
for info in infos:
if "episode" in info.keys():
eval_episode_rewards.append(info["episode"])
avg_reward = sum(eval_episode_rewards) / len(eval_episode_rewards)
print("---------------------------------------")
print(f"Evaluation over {num_episodes} episodes: {avg_reward}")
print("---------------------------------------")
return eval_episode_rewards
def multi_step_reward(rewards, gamma):
ret = 0.0
for idx, reward in enumerate(rewards):
ret += reward * (gamma ** idx)
return ret
class AtariVecEnv:
def __init__(self, env_name, seeds, num_processes, device, atari_preprocessing):
self.atari_preprocessing = atari_preprocessing
self.replay_action_probs = [(i / 1000.0) / 2 for i in range(1000)]
np.random.shuffle(self.replay_action_probs)
if num_processes == 1:
seeds = [np.argmin(self.replay_action_probs)]
self.seeds = seeds
self.test_seeds = [self.seeds[-1] + i for i in range(1, 1001 - len(self.seeds))]
self.num_processes = num_processes
self.env_name = env_name
self.level_seeds = torch.LongTensor(
[np.random.choice(self.seeds) for _ in range(self.num_processes)]
).unsqueeze(-1)
self.envs = [
utils.make_env(
self.env_name,
self.atari_preprocessing,
self.replay_action_probs[int(self.level_seeds[i].item())],
)[0]
for i in range(self.num_processes)
]
self.device = device
self.observation_space = np.zeros(
(self.atari_preprocessing["state_history"],) + self.envs[0].observation_space.shape
)
self.n_frames = self.atari_preprocessing["state_history"]
self.action_space = self.envs[0].action_space
def reset(self):
self.returns = [0 for i in range(self.num_processes)]
next_states = torch.zeros(
(self.num_processes, self.n_frames, 84, 84), dtype=torch.float32, device=self.device
)
for idx, env in enumerate(self.envs):
next_state = env.reset()
next_state = (torch.FloatTensor(next_state) / 255.0).to(self.device)
next_states[idx, :, :, :] = next_state
return next_states
def step(self, actions):
next_states = torch.zeros(
(self.num_processes, self.n_frames, 84, 84), dtype=torch.float32, device=self.device
)
rewards = np.zeros((self.num_processes, 1))
dones = []
infos: List[dict] = [{} for _ in range(self.num_processes)]
for idx, env in enumerate(self.envs):
next_state, reward, done, info = env.step(actions[idx])
self.returns[idx] += reward
rewards[idx, :] = info[0]
dones.append(done)
if done:
infos[idx]["return"] = self.returns[idx]
self.returns[idx] = 0
self.level_seeds[idx] = np.random.choice(self.seeds)
self.envs[idx] = utils.make_env(
self.env_name, self.atari_preprocessing, self.replay_action_probs[self.level_seeds[idx]]
)[0]
next_state = self.envs[idx].reset()
next_state = (torch.FloatTensor(next_state) / 255.0).to(self.device)
next_states[idx, :, :, :] = next_state
else:
next_state = (torch.FloatTensor(next_state) / 255.0).to(self.device)
next_states[idx, :, :, :] = next_state
return next_states, rewards, dones, infos, self.level_seeds
def generate_seeds(num_seeds, base_seed=0):
return [base_seed + i for i in range(num_seeds)]
if __name__ == "__main__":
args = parser.parse_args()
print(args)
train_seeds = generate_seeds(args.num_train_seeds)
train(args, train_seeds)