-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanrl_rainbow_atari.py
More file actions
641 lines (546 loc) · 24.6 KB
/
cleanrl_rainbow_atari.py
File metadata and controls
641 lines (546 loc) · 24.6 KB
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# docs and experiment results can be found at https://docs.cleanrl.dev/rl-algorithms/rainbow/#rainbow_ataripy
import collections
import math
import os
import random
import time
from collections import deque
from dataclasses import dataclass
from typing import SupportsFloat
import ale_py
import gymnasium as gym
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import tyro
from torch.utils.tensorboard import SummaryWriter
class NoopResetEnv(gym.Wrapper[np.ndarray, int, np.ndarray, int]):
"""
Sample initial states by taking random number of no-ops on reset.
No-op is assumed to be action 0.
:param env: Environment to wrap
:param noop_max: Maximum value of no-ops to run
"""
def __init__(self, env: gym.Env, noop_max: int = 30) -> None:
super().__init__(env)
self.noop_max = noop_max
self.override_num_noops = None
self.noop_action = 0
assert env.unwrapped.get_action_meanings()[0] == "NOOP" # type: ignore[attr-defined]
def reset(self, **kwargs):
self.env.reset(**kwargs)
if self.override_num_noops is not None:
noops = self.override_num_noops
else:
noops = self.unwrapped.np_random.integers(1, self.noop_max + 1)
assert noops > 0
obs = np.zeros(0)
info: dict = {}
for _ in range(noops):
obs, _, terminated, truncated, info = self.env.step(self.noop_action)
if terminated or truncated:
obs, info = self.env.reset(**kwargs)
return obs, info
class EpisodicLifeEnv(gym.Wrapper[np.ndarray, int, np.ndarray, int]):
"""
Make end-of-life == end-of-episode, but only reset on true game over.
Done by DeepMind for the DQN and co. since it helps value estimation.
:param env: Environment to wrap
"""
def __init__(self, env: gym.Env) -> None:
super().__init__(env)
self.lives = 0
self.was_real_done = True
def step(self, action: int):
obs, reward, terminated, truncated, info = self.env.step(action)
self.was_real_done = terminated or truncated
# check current lives, make loss of life terminal,
# then update lives to handle bonus lives
lives = self.env.unwrapped.ale.lives() # type: ignore[attr-defined]
if 0 < lives < self.lives:
# for Qbert sometimes we stay in lives == 0 condition for a few frames
# so its important to keep lives > 0, so that we only reset once
# the environment advertises done.
terminated = True
self.lives = lives
return obs, reward, terminated, truncated, info
def reset(self, **kwargs):
"""
Calls the Gym environment reset, only when lives are exhausted.
This way all states are still reachable even though lives are episodic,
and the learner need not know about any of this behind-the-scenes.
:param kwargs: Extra keywords passed to env.reset() call
:return: the first observation of the environment
"""
if self.was_real_done:
obs, info = self.env.reset(**kwargs)
else:
# no-op step to advance from terminal/lost life state
obs, _, terminated, truncated, info = self.env.step(0)
# The no-op step can lead to a game over, so we need to check it again
# to see if we should reset the environment and avoid the
# monitor.py `RuntimeError: Tried to step environment that needs reset`
if terminated or truncated:
obs, info = self.env.reset(**kwargs)
self.lives = self.env.unwrapped.ale.lives() # type: ignore[attr-defined]
return obs, info
class FireResetEnv(gym.Wrapper[np.ndarray, int, np.ndarray, int]):
"""
Take action on reset for environments that are fixed until firing.
:param env: Environment to wrap
"""
def __init__(self, env: gym.Env) -> None:
super().__init__(env)
assert env.unwrapped.get_action_meanings()[1] == "FIRE" # type: ignore[attr-defined]
assert len(env.unwrapped.get_action_meanings()) >= 3 # type: ignore[attr-defined]
def reset(self, **kwargs):
self.env.reset(**kwargs)
obs, _, terminated, truncated, _ = self.env.step(1)
if terminated or truncated:
self.env.reset(**kwargs)
obs, _, terminated, truncated, _ = self.env.step(2)
if terminated or truncated:
self.env.reset(**kwargs)
return obs, {}
class ClipRewardEnv(gym.RewardWrapper):
"""
Clip the reward to {+1, 0, -1} by its sign.
:param env: Environment to wrap
"""
def __init__(self, env: gym.Env) -> None:
super().__init__(env)
def reward(self, reward: SupportsFloat) -> float:
"""
Bin reward to {+1, 0, -1} by its sign.
:param reward:
:return:
"""
return np.sign(float(reward))
@dataclass
class Args:
exp_name: str = os.path.basename(__file__)[: -len(".py")]
"""the name of this experiment"""
seed: int = 1
"""seed of the experiment"""
torch_deterministic: bool = True
"""if toggled, `torch.backends.cudnn.deterministic=False`"""
cuda: bool = True
"""if toggled, cuda will be enabled by default"""
track: bool = False
"""if toggled, this experiment will be tracked with Weights and Biases"""
wandb_project_name: str = "cleanRL"
"""the wandb's project name"""
wandb_entity: str|None = None
"""the entity (team) of wandb's project"""
capture_video: bool = False
"""whether to capture videos of the agent performances (check out `videos` folder)"""
save_model: bool = False
"""whether to save model into the `runs/{run_name}` folder"""
upload_model: bool = False
"""whether to upload the saved model to huggingface"""
hf_entity: str = ""
"""the user or org name of the model repository from the Hugging Face Hub"""
env_id: str = "BreakoutNoFrameskip-v4"
"""the id of the environment"""
total_timesteps: int = 10000000
"""total timesteps of the experiments"""
learning_rate: float = 0.0000625
"""the learning rate of the optimizer"""
num_envs: int = 1
"""the number of parallel game environments"""
buffer_size: int = 1000000
"""the replay memory buffer size"""
gamma: float = 0.99
"""the discount factor gamma"""
tau: float = 1.0
"""the target network update rate"""
target_network_frequency: int = 8000
"""the timesteps it takes to update the target network"""
batch_size: int = 32
"""the batch size of sample from the reply memory"""
start_e: float = 1
"""the starting epsilon for exploration"""
end_e: float = 0.01
"""the ending epsilon for exploration"""
exploration_fraction: float = 0.10
"""the fraction of `total-timesteps` it takes from start-e to go end-e"""
learning_starts: int = 80000
"""timestep to start learning"""
train_frequency: int = 4
"""the frequency of training"""
n_step: int = 3
"""the number of steps to look ahead for n-step Q learning"""
prioritized_replay_alpha: float = 0.5
"""alpha parameter for prioritized replay buffer"""
prioritized_replay_beta: float = 0.4
"""beta parameter for prioritized replay buffer"""
prioritized_replay_eps: float = 1e-6
"""epsilon parameter for prioritized replay buffer"""
n_atoms: int = 51
"""the number of atoms"""
v_min: float = -10
"""the return lower bound"""
v_max: float = 10
"""the return upper bound"""
def make_env(env_id, seed, idx, capture_video, run_name):
def thunk():
gym.register_envs(ale_py)
if capture_video and idx == 0:
env = gym.make(env_id, render_mode="rgb_array")
env = gym.wrappers.RecordVideo(env, f"videos/{run_name}")
else:
env = gym.make(env_id)
env = gym.wrappers.RecordEpisodeStatistics(env)
env = NoopResetEnv(env, noop_max=30)
env = gym.wrappers.MaxAndSkipObservation(env, skip=4)
env = EpisodicLifeEnv(env)
if "FIRE" in env.unwrapped.get_action_meanings():
env = FireResetEnv(env)
env = ClipRewardEnv(env)
env = gym.wrappers.ResizeObservation(env, (84, 84))
env = gym.wrappers.GrayscaleObservation(env)
env = gym.wrappers.FrameStackObservation(env, 4)
env.action_space.seed(seed)
return env
return thunk
class NoisyLinear(nn.Module):
def __init__(self, in_features, out_features, std_init=0.5):
super().__init__()
self.in_features = in_features
self.out_features = out_features
self.std_init = std_init
self.weight_mu = nn.Parameter(torch.FloatTensor(out_features, in_features))
self.weight_sigma = nn.Parameter(torch.FloatTensor(out_features, in_features))
self.register_buffer("weight_epsilon", torch.FloatTensor(out_features, in_features))
self.bias_mu = nn.Parameter(torch.FloatTensor(out_features))
self.bias_sigma = nn.Parameter(torch.FloatTensor(out_features))
self.register_buffer("bias_epsilon", torch.FloatTensor(out_features))
# factorized gaussian noise
self.reset_parameters()
self.reset_noise()
def reset_parameters(self):
mu_range = 1 / math.sqrt(self.in_features)
self.weight_mu.data.uniform_(-mu_range, mu_range)
self.weight_sigma.data.fill_(self.std_init / math.sqrt(self.in_features))
self.bias_mu.data.uniform_(-mu_range, mu_range)
self.bias_sigma.data.fill_(self.std_init / math.sqrt(self.out_features))
def reset_noise(self):
self.weight_epsilon.normal_()
self.bias_epsilon.normal_()
def forward(self, input):
if self.training:
weight = self.weight_mu + self.weight_sigma * self.weight_epsilon
bias = self.bias_mu + self.bias_sigma * self.bias_epsilon
else:
weight = self.weight_mu
bias = self.bias_mu
return F.linear(input, weight, bias)
# ALGO LOGIC: initialize agent here:
class NoisyDuelingDistributionalNetwork(nn.Module):
def __init__(self, env, n_atoms, v_min, v_max):
super().__init__()
self.n_atoms = n_atoms
self.v_min = v_min
self.v_max = v_max
self.delta_z = (v_max - v_min) / (n_atoms - 1)
self.n_actions = env.single_action_space.n
self.register_buffer("support", torch.linspace(v_min, v_max, n_atoms))
self.network = nn.Sequential(
nn.Conv2d(4, 32, 8, stride=4),
nn.ReLU(),
nn.Conv2d(32, 64, 4, stride=2),
nn.ReLU(),
nn.Conv2d(64, 64, 3, stride=1),
nn.ReLU(),
nn.Flatten(),
)
conv_output_size = 3136
self.value_head = nn.Sequential(NoisyLinear(conv_output_size, 512), nn.ReLU(), NoisyLinear(512, n_atoms))
self.advantage_head = nn.Sequential(
NoisyLinear(conv_output_size, 512), nn.ReLU(), NoisyLinear(512, n_atoms * self.n_actions)
)
def forward(self, x):
h = self.network(x / 255.0)
value = self.value_head(h).view(-1, 1, self.n_atoms)
advantage = self.advantage_head(h).view(-1, self.n_actions, self.n_atoms)
q_atoms = value + advantage - advantage.mean(dim=1, keepdim=True)
q_dist = F.softmax(q_atoms, dim=2)
return q_dist
def reset_noise(self):
for layer in self.value_head:
if isinstance(layer, NoisyLinear):
layer.reset_noise()
for layer in self.advantage_head:
if isinstance(layer, NoisyLinear):
layer.reset_noise()
PrioritizedBatch = collections.namedtuple(
"PrioritizedBatch", ["observations", "actions", "rewards", "next_observations", "dones", "indices", "weights"]
)
# adapted from: https://github.com/openai/baselines/blob/master/baselines/common/segment_tree.py
class SumSegmentTree:
def __init__(self, capacity):
self.capacity = capacity
self.tree_size = 2 * capacity - 1
self.tree = np.zeros(self.tree_size, dtype=np.float32)
def _propagate(self, idx):
parent = (idx - 1) // 2
while parent >= 0:
self.tree[parent] = self.tree[parent * 2 + 1] + self.tree[parent * 2 + 2]
parent = (parent - 1) // 2
def update(self, idx, value):
tree_idx = idx + self.capacity - 1
self.tree[tree_idx] = value
self._propagate(tree_idx)
def total(self):
return self.tree[0]
def retrieve(self, value):
idx = 0
while idx * 2 + 1 < self.tree_size:
left = idx * 2 + 1
right = left + 1
if value <= self.tree[left]:
idx = left
else:
value -= self.tree[left]
idx = right
return idx - (self.capacity - 1)
# adapted from: https://github.com/openai/baselines/blob/master/baselines/common/segment_tree.py
class MinSegmentTree:
def __init__(self, capacity):
self.capacity = capacity
self.tree_size = 2 * capacity - 1
self.tree = np.full(self.tree_size, float("inf"), dtype=np.float32)
def _propagate(self, idx):
parent = (idx - 1) // 2
while parent >= 0:
self.tree[parent] = min(self.tree[parent * 2 + 1], self.tree[parent * 2 + 2])
parent = (parent - 1) // 2
def update(self, idx, value):
tree_idx = idx + self.capacity - 1
self.tree[tree_idx] = value
self._propagate(tree_idx)
def min(self):
return self.tree[0]
class PrioritizedReplayBuffer:
def __init__(self, capacity, obs_shape, device, n_step, gamma, alpha=0.6, beta=0.4, eps=1e-6):
self.capacity = capacity
self.device = device
self.n_step = n_step
self.gamma = gamma
self.alpha = alpha
self.beta = beta
self.eps = eps
self.buffer_obs = np.zeros((capacity,) + obs_shape, dtype=np.uint8)
self.buffer_next_obs = np.zeros((capacity,) + obs_shape, dtype=np.uint8)
self.buffer_actions = np.zeros(capacity, dtype=np.int64)
self.buffer_rewards = np.zeros(capacity, dtype=np.float32)
self.buffer_dones = np.zeros(capacity, dtype=np.bool_)
self.pos = 0
self.size = 0
self.max_priority = 1.0
self.sum_tree = SumSegmentTree(capacity)
self.min_tree = MinSegmentTree(capacity)
# For n-step returns
self.n_step_buffer = deque(maxlen=n_step)
def _get_n_step_info(self):
reward = 0.0
next_obs = self.n_step_buffer[-1][3]
done = self.n_step_buffer[-1][4]
for i in range(len(self.n_step_buffer)):
reward += self.gamma**i * self.n_step_buffer[i][2]
if self.n_step_buffer[i][4]:
next_obs = self.n_step_buffer[i][3]
done = True
break
return reward, next_obs, done
def add(self, obs, action, reward, next_obs, done):
self.n_step_buffer.append((obs, action, reward, next_obs, done))
if len(self.n_step_buffer) < self.n_step:
return
reward, next_obs, done = self._get_n_step_info()
obs = self.n_step_buffer[0][0]
action = self.n_step_buffer[0][1]
idx = self.pos
self.buffer_obs[idx] = obs
self.buffer_next_obs[idx] = next_obs
self.buffer_actions[idx] = action.item()
self.buffer_rewards[idx] = reward.item()
self.buffer_dones[idx] = done
priority = self.max_priority**self.alpha
self.sum_tree.update(idx, priority)
self.min_tree.update(idx, priority)
self.pos = (self.pos + 1) % self.capacity
self.size = min(self.size + 1, self.capacity)
if done:
self.n_step_buffer.clear()
def sample(self, batch_size):
indices = []
p_total = self.sum_tree.total()
segment = p_total / batch_size
for i in range(batch_size):
a = segment * i
b = segment * (i + 1)
upperbound = np.random.uniform(a, b)
idx = self.sum_tree.retrieve(upperbound)
indices.append(idx)
samples = {
"observations": torch.from_numpy(self.buffer_obs[indices]).to(self.device),
"actions": torch.from_numpy(self.buffer_actions[indices]).to(self.device).unsqueeze(1),
"rewards": torch.from_numpy(self.buffer_rewards[indices]).to(self.device).unsqueeze(1),
"next_observations": torch.from_numpy(self.buffer_next_obs[indices]).to(self.device),
"dones": torch.from_numpy(self.buffer_dones[indices]).to(self.device).unsqueeze(1),
}
probs = np.array([self.sum_tree.tree[idx + self.capacity - 1] for idx in indices])
weights = (self.size * probs / (p_total + 1e-8)) ** -self.beta
weights = weights / weights.max()
samples["weights"] = torch.from_numpy(weights).to(self.device).unsqueeze(1)
samples["indices"] = indices
return PrioritizedBatch(**samples)
def update_priorities(self, indices, priorities):
priorities = np.abs(priorities) + self.eps
self.max_priority = max(self.max_priority, priorities.max())
for idx, priority in zip(indices, priorities):
priority = priority**self.alpha
self.sum_tree.update(idx, priority)
self.min_tree.update(idx, priority)
if __name__ == "__main__":
args = tyro.cli(Args)
assert args.num_envs == 1, "vectorized envs are not supported at the moment"
run_name = f"{args.env_id}__{args.exp_name}__{args.seed}__{int(time.time())}"
if args.track:
import wandb
wandb.init(
project=args.wandb_project_name,
entity=args.wandb_entity,
sync_tensorboard=True,
config=vars(args),
name=run_name,
monitor_gym=True,
save_code=True,
)
writer = SummaryWriter(f"runs/{run_name}")
writer.add_text(
"hyperparameters",
"|param|value|\n|-|-|\n%s" % ("\n".join([f"|{key}|{value}|" for key, value in vars(args).items()])),
)
# TRY NOT TO MODIFY: seeding
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.backends.cudnn.deterministic = args.torch_deterministic
device = torch.device("mps" if torch.mps.is_available() else "cpu")
# env setup
envs = gym.vector.SyncVectorEnv(
[make_env(args.env_id, args.seed + i, i, args.capture_video, run_name) for i in range(args.num_envs)]
)
assert isinstance(envs.single_action_space, gym.spaces.Discrete), "only discrete action space is supported"
q_network = NoisyDuelingDistributionalNetwork(envs, args.n_atoms, args.v_min, args.v_max).to(device)
optimizer = optim.Adam(q_network.parameters(), lr=args.learning_rate, eps=1.5e-4)
target_network = NoisyDuelingDistributionalNetwork(envs, args.n_atoms, args.v_min, args.v_max).to(device)
target_network.load_state_dict(q_network.state_dict())
rb = PrioritizedReplayBuffer(
args.buffer_size,
envs.single_observation_space.shape,
device,
args.n_step,
args.gamma,
args.prioritized_replay_alpha,
args.prioritized_replay_beta,
args.prioritized_replay_eps,
)
start_time = time.time()
# TRY NOT TO MODIFY: start the game
obs, _ = envs.reset(seed=args.seed)
for global_step in range(args.total_timesteps):
# anneal PER beta to 1
rb.beta = min(
1.0, args.prioritized_replay_beta + global_step * (1.0 - args.prioritized_replay_beta) / args.total_timesteps
)
# ALGO LOGIC: put action logic here
with torch.no_grad():
q_dist = q_network(torch.Tensor(obs).to(device))
q_values = torch.sum(q_dist * q_network.support, dim=2)
actions = torch.argmax(q_values, dim=1).cpu().numpy()
# TRY NOT TO MODIFY: execute the game and log data.
next_obs, rewards, terminations, truncations, infos = envs.step(actions)
if "episode" in infos:
done_mask = infos.get("_episode", None)
if done_mask is not None:
rs = infos["episode"]["r"]
ls = infos["episode"]["l"]
for i, done in enumerate(done_mask):
if done:
ep_r = float(rs[i])
ep_l = int(ls[i])
# print(f"global_step={global_step}, episodic_return={ep_r}")
writer.add_scalar("train/episode_return", ep_r, global_step)
writer.add_scalar("train/episode_length", ep_l, global_step)
# TRY NOT TO MODIFY: save data to reply buffer; handle `final_observation`
real_next_obs = next_obs.copy()
for idx, trunc in enumerate(truncations):
if trunc:
real_next_obs[idx] = infos["final_observation"][idx]
rb.add(obs, actions, rewards, real_next_obs, terminations)
# TRY NOT TO MODIFY: CRUCIAL step easy to overlook
obs = next_obs
# ALGO LOGIC: training.
if global_step > args.learning_starts:
if global_step % args.train_frequency == 0:
# reset the noise for both networks
q_network.reset_noise()
target_network.reset_noise()
data = rb.sample(args.batch_size)
with torch.no_grad():
next_dist = target_network(data.next_observations) # [B, num_actions, n_atoms]
support = target_network.support # [n_atoms]
next_q_values = torch.sum(next_dist * support, dim=2) # [B, num_actions]
# double q-learning
next_dist_online = q_network(data.next_observations) # [B, num_actions, n_atoms]
next_q_online = torch.sum(next_dist_online * support, dim=2) # [B, num_actions]
best_actions = torch.argmax(next_q_online, dim=1) # [B]
next_pmfs = next_dist[torch.arange(args.batch_size), best_actions] # [B, n_atoms]
# compute the n-step Bellman update.
gamma_n = args.gamma**args.n_step
next_atoms = data.rewards + gamma_n * support * (1 - data.dones.float())
tz = next_atoms.clamp(q_network.v_min, q_network.v_max)
# projection
delta_z = q_network.delta_z
b = (tz - q_network.v_min) / delta_z # shape: [B, n_atoms]
l = b.floor().clamp(0, args.n_atoms - 1)
u = b.ceil().clamp(0, args.n_atoms - 1)
# (l == u).float() handles the case where bj is exactly an integer
# example bj = 1, then the upper ceiling should be uj= 2, and lj= 1
d_m_l = (u.float() + (l == b).float() - b) * next_pmfs # [B, n_atoms]
d_m_u = (b - l) * next_pmfs # [B, n_atoms]
target_pmfs = torch.zeros_like(next_pmfs)
for i in range(target_pmfs.size(0)):
target_pmfs[i].index_add_(0, l[i].long(), d_m_l[i])
target_pmfs[i].index_add_(0, u[i].long(), d_m_u[i])
dist = q_network(data.observations) # [B, num_actions, n_atoms]
pred_dist = dist.gather(1, data.actions.unsqueeze(-1).expand(-1, -1, args.n_atoms)).squeeze(1)
log_pred = torch.log(pred_dist.clamp(min=1e-5, max=1 - 1e-5))
loss_per_sample = -(target_pmfs * log_pred).sum(dim=1)
loss = (loss_per_sample * data.weights.squeeze()).mean()
# update priorities
new_priorities = loss_per_sample.detach().cpu().numpy()
rb.update_priorities(data.indices, new_priorities)
if global_step % 100 == 0:
writer.add_scalar("losses/td_loss", loss.item(), global_step)
q_values = (pred_dist * q_network.support).sum(dim=1) # [B]
writer.add_scalar("losses/q_values", q_values.mean().item(), global_step)
sps = int(global_step / (time.time() - start_time))
print("SPS:", sps)
writer.add_scalar("charts/SPS", sps, global_step)
writer.add_scalar("charts/beta", rb.beta, global_step)
# optimize the model
optimizer.zero_grad()
loss.backward()
optimizer.step()
# update target network
if global_step % args.target_network_frequency == 0:
for target_param, param in zip(target_network.parameters(), q_network.parameters()):
target_param.data.copy_(args.tau * param.data + (1.0 - args.tau) * target_param.data)
envs.close()
writer.close()