-
Notifications
You must be signed in to change notification settings - Fork 0
/
cqn.py
673 lines (595 loc) · 21.8 KB
/
cqn.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
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
from typing import Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
import utils
from cqn_utils import (
random_action_if_within_delta,
zoom_in,
encode_action,
decode_action,
)
class RandomShiftsAug(nn.Module):
def __init__(self, pad):
super().__init__()
self.pad = pad
def forward(self, x):
n, c, h, w = x.size()
assert h == w
padding = tuple([self.pad] * 4)
x = F.pad(x, padding, "replicate")
eps = 1.0 / (h + 2 * self.pad)
arange = torch.linspace(
-1.0 + eps, 1.0 - eps, h + 2 * self.pad, device=x.device, dtype=x.dtype
)[:h]
arange = arange.unsqueeze(0).repeat(h, 1).unsqueeze(2)
base_grid = torch.cat([arange, arange.transpose(1, 0)], dim=2)
base_grid = base_grid.unsqueeze(0).repeat(n, 1, 1, 1)
shift = torch.randint(
0, 2 * self.pad + 1, size=(n, 1, 1, 2), device=x.device, dtype=x.dtype
)
shift *= 2.0 / (h + 2 * self.pad)
grid = base_grid + shift
return F.grid_sample(x, grid, padding_mode="zeros", align_corners=False)
class ImgChLayerNorm(nn.Module):
def __init__(self, num_channels, eps: float = 1e-5):
super().__init__()
self.weight = nn.Parameter(torch.ones(num_channels))
self.bias = nn.Parameter(torch.zeros(num_channels))
self.eps = eps
def forward(self, x):
# x: [B, C, H, W]
u = x.mean(1, keepdim=True)
s = (x - u).pow(2).mean(1, keepdim=True)
x = (x - u) / torch.sqrt(s + self.eps)
x = self.weight[:, None, None] * x + self.bias[:, None, None]
return x
class MultiViewCNNEncoder(nn.Module):
def __init__(self, obs_shape):
super().__init__()
assert len(obs_shape) == 4
self.num_views = obs_shape[0]
self.repr_dim = self.num_views * 256 * 5 * 5 # for 84,84. hard-coded
self.conv_nets = nn.ModuleList()
for _ in range(self.num_views):
conv_net = nn.Sequential(
nn.Conv2d(obs_shape[1], 32, 4, stride=2, padding=1),
ImgChLayerNorm(32),
nn.SiLU(),
nn.Conv2d(32, 64, 4, stride=2, padding=1),
ImgChLayerNorm(64),
nn.SiLU(),
nn.Conv2d(64, 128, 4, stride=2, padding=1),
ImgChLayerNorm(128),
nn.SiLU(),
nn.Conv2d(128, 256, 4, stride=2, padding=1),
ImgChLayerNorm(256),
nn.SiLU(),
)
self.conv_nets.append(conv_net)
self.apply(utils.weight_init)
def forward(self, obs: torch.Tensor):
# obs: [B, V, C, H, W]
obs = obs / 255.0 - 0.5
hs = []
for v in range(self.num_views):
h = self.conv_nets[v](obs[:, v])
h = h.view(h.shape[0], -1)
hs.append(h)
h = torch.cat(hs, -1)
return h
class C2FCriticNetwork(nn.Module):
def __init__(
self,
repr_dim: int,
low_dim: int,
action_shape: Tuple,
feature_dim: int,
hidden_dim: int,
levels: int,
bins: int,
atoms: int,
):
super().__init__()
self._levels = levels
self._actor_dim = action_shape[0]
self._bins = bins
# Advantage stream in Dueling network
self.adv_rgb_encoder = nn.Sequential(
nn.Linear(repr_dim, feature_dim, bias=False),
nn.LayerNorm(feature_dim),
nn.Tanh(),
)
self.adv_low_dim_encoder = nn.Sequential(
nn.Linear(low_dim, feature_dim, bias=False),
nn.LayerNorm(feature_dim),
nn.Tanh(),
)
self.adv_net = nn.Sequential(
nn.Linear(
feature_dim * 2 + self._actor_dim + levels, hidden_dim, bias=False
),
nn.LayerNorm(hidden_dim),
nn.SiLU(),
nn.Linear(hidden_dim, hidden_dim, bias=False),
nn.LayerNorm(hidden_dim),
nn.SiLU(),
)
self.adv_head = nn.Linear(hidden_dim, self._actor_dim * bins * atoms)
self.adv_output_shape = (self._actor_dim, bins, atoms)
# Value stream in Dueling network
self.value_rgb_encoder = nn.Sequential(
nn.Linear(repr_dim, feature_dim, bias=False),
nn.LayerNorm(feature_dim),
nn.Tanh(),
)
self.value_low_dim_encoder = nn.Sequential(
nn.Linear(low_dim, feature_dim, bias=False),
nn.LayerNorm(feature_dim),
nn.Tanh(),
)
self.value_net = nn.Sequential(
nn.Linear(
feature_dim * 2 + self._actor_dim + levels, hidden_dim, bias=False
),
nn.LayerNorm(hidden_dim),
nn.SiLU(),
nn.Linear(hidden_dim, hidden_dim, bias=False),
nn.LayerNorm(hidden_dim),
nn.SiLU(),
)
self.value_head = nn.Linear(hidden_dim, self._actor_dim * 1 * atoms)
self.value_output_shape = (self._actor_dim, 1, atoms)
self.apply(utils.weight_init)
self.adv_head.weight.data.fill_(0.0)
self.adv_head.bias.data.fill_(0.0)
self.value_head.weight.data.fill_(0.0)
self.value_head.bias.data.fill_(0.0)
def forward(
self, level: int, rgb_obs: torch.Tensor, low_dim_obs, prev_action: torch.Tensor
):
"""
Inputs:
- level: level index
- obs: features from visual encoder
- low_dim_obs: low-dimensional observations
- prev_action: actions from previous level
Outputs:
- q_logits: (batch_size, action_dim, bins, atoms)
"""
level_id = (
torch.eye(self._levels, device=rgb_obs.device, dtype=rgb_obs.dtype)[level]
.unsqueeze(0)
.repeat_interleave(rgb_obs.shape[0], 0)
)
value_h = torch.cat(
[self.value_rgb_encoder(rgb_obs), self.value_low_dim_encoder(low_dim_obs)],
-1,
)
value_x = torch.cat([value_h, prev_action, level_id], -1)
values = self.value_head(self.value_net(value_x)).view(
-1, *self.value_output_shape
)
adv_h = torch.cat(
[self.adv_rgb_encoder(rgb_obs), self.adv_low_dim_encoder(low_dim_obs)], -1
)
adv_x = torch.cat([adv_h, prev_action, level_id], -1)
advs = self.adv_head(self.adv_net(adv_x)).view(-1, *self.adv_output_shape)
q_logits = values + advs - advs.mean(-2, keepdim=True)
return q_logits
class C2FCritic(nn.Module):
def __init__(
self,
action_shape: tuple,
repr_dim: int,
low_dim: int,
feature_dim: int,
hidden_dim: int,
levels: int,
bins: int,
atoms: int,
v_min: float,
v_max: float,
):
super().__init__()
self.levels = levels
self.bins = bins
self.atoms = atoms
self.v_min = v_min
self.v_max = v_max
actor_dim = action_shape[0]
self.initial_low = nn.Parameter(
torch.FloatTensor([-1.0] * actor_dim), requires_grad=False
)
self.initial_high = nn.Parameter(
torch.FloatTensor([1.0] * actor_dim), requires_grad=False
)
self.support = nn.Parameter(
torch.linspace(v_min, v_max, atoms), requires_grad=False
)
self.delta_z = (v_max - v_min) / (atoms - 1)
self.network = C2FCriticNetwork(
repr_dim,
low_dim,
action_shape,
feature_dim,
hidden_dim,
levels,
bins,
atoms,
)
def get_action(self, rgb_obs: torch.Tensor, low_dim_obs: torch.Tensor):
metrics = dict()
low = self.initial_low.repeat(rgb_obs.shape[0], 1).detach()
high = self.initial_high.repeat(rgb_obs.shape[0], 1).detach()
for level in range(self.levels):
q_logits = self.network(level, rgb_obs, low_dim_obs, (low + high) / 2)
q_probs = F.softmax(q_logits, 3)
qs = (q_probs * self.support.expand_as(q_probs).detach()).sum(3)
argmax_q = random_action_if_within_delta(qs)
if argmax_q is None:
argmax_q = qs.max(-1)[1] # [..., D]
# Zoom-in
low, high = zoom_in(low, high, argmax_q, self.bins)
# for logging
qs_a = torch.gather(qs, dim=-1, index=argmax_q.unsqueeze(-1))[
..., 0
] # [..., D]
metrics[f"critic_target_q_level{level}"] = qs_a.mean().item()
continuous_action = (high + low) / 2.0 # [..., D]
return continuous_action, metrics
def forward(
self,
rgb_obs: torch.Tensor,
low_dim_obs: torch.Tensor,
continuous_action: torch.Tensor,
):
"""Compute value distributions for given obs and action.
Args:
obs: [B, repr_dim] shaped feature tensor
low_dim_obs: [B, low_dim] shaped feature tensor
continuous_action: [B, D] shaped action tensor
Return:
q_probs: [B, L, D, bins, atoms] for value distribution at all bins
q_probs_a: [B, L, D, atoms] for value distribution at given bin
log_q_probs: [B, L, D, bins, atoms] with log probabilities
log_q_probs_a: [B, L, D, atoms] with log probabilities
"""
discrete_action = encode_action(
continuous_action,
self.initial_low,
self.initial_high,
self.levels,
self.bins,
)
q_probs_per_level = []
q_probs_a_per_level = []
log_q_probs_per_level = []
log_q_probs_a_per_level = []
low = self.initial_low.repeat(rgb_obs.shape[0], 1).detach()
high = self.initial_high.repeat(rgb_obs.shape[0], 1).detach()
for level in range(self.levels):
q_logits = self.network(level, rgb_obs, low_dim_obs, (low + high) / 2)
argmax_q = discrete_action[..., level, :].long() # [..., L, D] -> [..., D]
# (Log) Probs [..., D, bins, atoms]
# (Log) Probs_a [..., D, atoms]
q_probs = F.softmax(q_logits, 3) # [B, D, bins, atoms]
q_probs_a = torch.gather(
q_probs,
dim=-2,
index=argmax_q.unsqueeze(-1)
.unsqueeze(-1)
.repeat_interleave(self.atoms, -1),
)
q_probs_a = q_probs_a[..., 0, :] # [B, D, atoms]
log_q_probs = F.log_softmax(q_logits, 3) # [B, D, bins, atoms]
log_q_probs_a = torch.gather(
log_q_probs,
dim=-2,
index=argmax_q.unsqueeze(-1)
.unsqueeze(-1)
.repeat_interleave(self.atoms, -1),
)
log_q_probs_a = log_q_probs_a[..., 0, :] # [B, D, atoms]
q_probs_per_level.append(q_probs)
q_probs_a_per_level.append(q_probs_a)
log_q_probs_per_level.append(log_q_probs)
log_q_probs_a_per_level.append(log_q_probs_a)
# Zoom-in
low, high = zoom_in(low, high, argmax_q, self.bins)
q_probs = torch.stack(q_probs_per_level, -4) # [B, L, D, bins, atoms]
q_probs_a = torch.stack(q_probs_a_per_level, -3) # [B, L, D, atoms]
log_q_probs = torch.stack(log_q_probs_per_level, -4)
log_q_probs_a = torch.stack(log_q_probs_a_per_level, -3)
return q_probs, q_probs_a, log_q_probs, log_q_probs_a
def compute_target_q_dist(
self,
next_rgb_obs: torch.Tensor,
next_low_dim_obs: torch.Tensor,
next_continuous_action: torch.Tensor,
reward: torch.Tensor,
discount: torch.Tensor,
):
"""Compute target distribution for distributional critic
based on https://github.com/Kaixhin/Rainbow/blob/master/agent.py implementation
Args:
next_rgb_obs: [B, repr_dim] shaped feature tensor
next_low_dim_obs: [B, low_dim] shaped feature tensor
next_continuous_action: [B, D] shaped action tensor
reward: [B, 1] shaped reward tensor
discount: [B, 1] shaped discount tensor
Return:
m: [B, L, D, atoms] shaped tensor for value distribution
"""
next_q_probs_a = self.forward(
next_rgb_obs, next_low_dim_obs, next_continuous_action
)[1]
shape = next_q_probs_a.shape # [B, L, D, atoms]
next_q_probs_a = next_q_probs_a.view(-1, self.atoms)
batch_size = next_q_probs_a.shape[0]
# Compute Tz for [B, atoms]
Tz = reward + discount * self.support.unsqueeze(0).detach()
Tz = Tz.clamp(min=self.v_min, max=self.v_max)
# Compute L2 projection of Tz onto fixed support z
b = (Tz - self.v_min) / self.delta_z
lower, upper = b.floor().to(torch.int64), b.ceil().to(torch.int64)
# Fix disappearing probability mass when l =b = u (b is int)
lower[(upper > 0) * (lower == upper)] -= 1
upper[(lower < (self.atoms - 1)) * (lower == upper)] += 1
# Repeat Tz for (L * D) times -> [B * L * D, atoms]
multiplier = batch_size // lower.shape[0]
b = torch.repeat_interleave(b, multiplier, 0)
lower = torch.repeat_interleave(lower, multiplier, 0)
upper = torch.repeat_interleave(upper, multiplier, 0)
# Distribute probability of Tz
m = torch.zeros_like(next_q_probs_a)
offset = (
torch.linspace(
0,
((batch_size - 1) * self.atoms),
batch_size,
device=lower.device,
dtype=lower.dtype,
)
.unsqueeze(1)
.expand(batch_size, self.atoms)
)
m.view(-1).index_add_(
0,
(lower + offset).view(-1),
(next_q_probs_a * (upper.float() - b)).view(-1),
) # m_l = m_l + p(s_t+n, a*)(u - b)
m.view(-1).index_add_(
0,
(upper + offset).view(-1),
(next_q_probs_a * (b - lower.float())).view(-1),
) # m_u = m_u + p(s_t+n, a*)(b - l)
m = m.view(*shape) # [B, L, D, atoms]
return m
def encode_decode_action(self, continuous_action: torch.Tensor):
"""Encode and decode actions"""
discrete_action = encode_action(
continuous_action,
self.initial_low,
self.initial_high,
self.levels,
self.bins,
)
continuous_action = decode_action(
discrete_action,
self.initial_low,
self.initial_high,
self.levels,
self.bins,
)
return continuous_action
class CQNAgent:
def __init__(
self,
rgb_obs_shape,
low_dim_obs_shape,
action_shape,
device,
lr,
feature_dim,
hidden_dim,
levels,
bins,
atoms,
v_min,
v_max,
bc_lambda,
bc_margin,
critic_lambda,
critic_target_tau,
weight_decay,
num_expl_steps,
update_every_steps,
stddev_schedule,
use_logger,
):
self.device = device
self.critic_target_tau = critic_target_tau
self.update_every_steps = update_every_steps
self.use_logger = use_logger
self.num_expl_steps = num_expl_steps
self.stddev_schedule = stddev_schedule
self.bc_lambda = bc_lambda
self.bc_margin = bc_margin
self.critic_lambda = critic_lambda
# models
self.encoder = MultiViewCNNEncoder(rgb_obs_shape).to(device)
self.critic = C2FCritic(
action_shape,
self.encoder.repr_dim,
low_dim_obs_shape[-1],
feature_dim,
hidden_dim,
levels,
bins,
atoms,
v_min,
v_max,
).to(device)
self.critic_target = C2FCritic(
action_shape,
self.encoder.repr_dim,
low_dim_obs_shape[-1],
feature_dim,
hidden_dim,
levels,
bins,
atoms,
v_min,
v_max,
).to(device)
self.critic_target.load_state_dict(self.critic.state_dict())
# optimizers
self.encoder_opt = torch.optim.AdamW(
self.encoder.parameters(), lr=lr, weight_decay=weight_decay
)
self.critic_opt = torch.optim.AdamW(
self.critic.parameters(), lr=lr, weight_decay=weight_decay
)
# data augmentation
self.aug = RandomShiftsAug(pad=4)
self.train()
self.critic_target.eval()
print(self.encoder)
print(self.critic)
def train(self, training=True):
self.training = training
self.encoder.train(training)
self.critic.train(training)
def act(self, rgb_obs, low_dim_obs, step, eval_mode):
rgb_obs = torch.as_tensor(rgb_obs, device=self.device).unsqueeze(0)
low_dim_obs = torch.as_tensor(low_dim_obs, device=self.device).unsqueeze(0)
rgb_obs = self.encoder(rgb_obs)
stddev = utils.schedule(self.stddev_schedule, step)
action, _ = self.critic_target.get_action(
rgb_obs, low_dim_obs
) # use critic_target
stddev = torch.ones_like(action) * stddev
dist = utils.TruncatedNormal(action, stddev)
if eval_mode:
action = dist.mean
else:
action = dist.sample(clip=None)
if step < self.num_expl_steps:
action.uniform_(-1.0, 1.0)
action = self.critic.encode_decode_action(action)
return action.cpu().numpy()[0]
def update_critic(
self,
rgb_obs,
low_dim_obs,
action,
reward,
discount,
next_rgb_obs,
next_low_dim_obs,
demos,
):
metrics = dict()
with torch.no_grad():
next_action, mets = self.critic.get_action(next_rgb_obs, next_low_dim_obs)
target_q_probs_a = self.critic_target.compute_target_q_dist(
next_rgb_obs, next_low_dim_obs, next_action, reward, discount
)
if self.use_logger:
metrics.update(**mets)
# Cross entropy loss for C51
q_probs, q_probs_a, log_q_probs, log_q_probs_a = self.critic(
rgb_obs, low_dim_obs, action
)
q_critic_loss = -torch.sum(target_q_probs_a * log_q_probs_a, 3).mean()
critic_loss = self.critic_lambda * q_critic_loss
if self.use_logger:
metrics["q_critic_loss"] = q_critic_loss.item()
if self.bc_lambda > 0.0:
qs = None
demos = demos.float().squeeze(1) # [B,]
if self.use_logger:
metrics["ratio_of_demos"] = demos.mean().item()
if torch.sum(demos) > 0:
# q_probs: [B, L, D, bins, atoms], q_probs_a: [B, L, D, atoms]
q_probs_cdf = torch.cumsum(q_probs, -1)
q_probs_a_cdf = torch.cumsum(q_probs_a, -1)
# q_probs_{a_{i}} is stochastically dominant over q_probs_{a_{-i}}
bc_fosd_loss = (
(q_probs_a_cdf.unsqueeze(-2) - q_probs_cdf)
.clamp(min=0)
.sum(-1)
.mean([-1, -2, -3])
)
bc_fosd_loss = (bc_fosd_loss * demos).sum() / demos.sum()
critic_loss = critic_loss + self.bc_lambda * bc_fosd_loss
if self.use_logger:
metrics["bc_fosd_loss"] = bc_fosd_loss.item()
if self.bc_margin > 0:
qs = (q_probs * self.critic.support.expand_as(q_probs)).sum(-1)
qs_a = (q_probs_a * self.critic.support.expand_as(q_probs_a)).sum(
-1
)
margin_loss = torch.clamp(
self.bc_margin - (qs_a.unsqueeze(-1) - qs), min=0
).mean([-1, -2, -3])
margin_loss = (margin_loss * demos).sum() / demos.sum()
critic_loss = critic_loss + self.bc_lambda * margin_loss
if self.use_logger:
metrics["bc_margin_loss"] = margin_loss.item()
# optimize encoder and critic
self.encoder_opt.zero_grad(set_to_none=True)
self.critic_opt.zero_grad(set_to_none=True)
critic_loss.backward()
self.critic_opt.step()
self.encoder_opt.step()
return metrics
def update(self, replay_iter, step):
metrics = dict()
if step % self.update_every_steps != 0:
return metrics
batch = next(replay_iter)
(
rgb_obs,
low_dim_obs,
action,
reward,
discount,
next_rgb_obs,
next_low_dim_obs,
demos,
) = utils.to_torch(batch, self.device)
# augment
rgb_obs = rgb_obs.float()
next_rgb_obs = next_rgb_obs.float()
rgb_obs = torch.stack(
[self.aug(rgb_obs[:, v]) for v in range(rgb_obs.shape[1])], 1
)
next_rgb_obs = torch.stack(
[self.aug(next_rgb_obs[:, v]) for v in range(next_rgb_obs.shape[1])], 1
)
# encode
rgb_obs = self.encoder(rgb_obs)
with torch.no_grad():
next_rgb_obs = self.encoder(next_rgb_obs)
if self.use_logger:
metrics["batch_reward"] = reward.mean().item()
# update critic
metrics.update(
self.update_critic(
rgb_obs,
low_dim_obs,
action,
reward,
discount,
next_rgb_obs,
next_low_dim_obs,
demos,
)
)
# update critic target
utils.soft_update_params(
self.critic, self.critic_target, self.critic_target_tau
)
return metrics