-
Notifications
You must be signed in to change notification settings - Fork 131
/
cql.py
987 lines (852 loc) · 35.3 KB
/
cql.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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
# source: https://github.com/young-geng/CQL/tree/934b0e8354ca431d6c083c4e3a29df88d4b0a24d
# https://arxiv.org/pdf/2006.04779.pdf
import os
import random
import uuid
from copy import deepcopy
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple, Union
import d4rl
import gym
import numpy as np
import pyrallis
import torch
import torch.nn as nn
import torch.nn.functional as F
import wandb
from torch.distributions import Normal, TanhTransform, TransformedDistribution
TensorBatch = List[torch.Tensor]
@dataclass
class TrainConfig:
# Experiment
device: str = "cuda"
env: str = "halfcheetah-medium-expert-v2" # OpenAI gym environment name
seed: int = 0 # Sets Gym, PyTorch and Numpy seeds
eval_freq: int = int(5e3) # How often (time steps) we evaluate
n_episodes: int = 10 # How many episodes run during evaluation
max_timesteps: int = int(1e6) # Max time steps to run environment
checkpoints_path: Optional[str] = None # Save path
load_model: str = "" # Model load file name, "" doesn't load
# CQL
buffer_size: int = 2_000_000 # Replay buffer size
batch_size: int = 256 # Batch size for all networks
discount: float = 0.99 # Discount factor
alpha_multiplier: float = 1.0 # Multiplier for alpha in loss
use_automatic_entropy_tuning: bool = True # Tune entropy
backup_entropy: bool = False # Use backup entropy
policy_lr: float = 3e-5 # Policy learning rate
qf_lr: float = 3e-4 # Critics learning rate
soft_target_update_rate: float = 5e-3 # Target network update rate
target_update_period: int = 1 # Frequency of target nets updates
cql_n_actions: int = 10 # Number of sampled actions
cql_importance_sample: bool = True # Use importance sampling
cql_lagrange: bool = False # Use Lagrange version of CQL
cql_target_action_gap: float = -1.0 # Action gap
cql_temp: float = 1.0 # CQL temperature
cql_alpha: float = 10.0 # Minimal Q weight
cql_max_target_backup: bool = False # Use max target backup
cql_clip_diff_min: float = -np.inf # Q-function lower loss clipping
cql_clip_diff_max: float = np.inf # Q-function upper loss clipping
orthogonal_init: bool = True # Orthogonal initialization
normalize: bool = True # Normalize states
normalize_reward: bool = False # Normalize reward
q_n_hidden_layers: int = 3 # Number of hidden layers in Q networks
reward_scale: float = 1.0 # Reward scale for normalization
reward_bias: float = 0.0 # Reward bias for normalization
# AntMaze hacks
bc_steps: int = int(0) # Number of BC steps at start
reward_scale: float = 5.0
reward_bias: float = -1.0
policy_log_std_multiplier: float = 1.0
# Wandb logging
project: str = "CORL"
group: str = "CQL-D4RL"
name: str = "CQL"
def __post_init__(self):
self.name = f"{self.name}-{self.env}-{str(uuid.uuid4())[:8]}"
if self.checkpoints_path is not None:
self.checkpoints_path = os.path.join(self.checkpoints_path, self.name)
def soft_update(target: nn.Module, source: nn.Module, tau: float):
for target_param, source_param in zip(target.parameters(), source.parameters()):
target_param.data.copy_((1 - tau) * target_param.data + tau * source_param.data)
def compute_mean_std(states: np.ndarray, eps: float) -> Tuple[np.ndarray, np.ndarray]:
mean = states.mean(0)
std = states.std(0) + eps
return mean, std
def normalize_states(states: np.ndarray, mean: np.ndarray, std: np.ndarray):
return (states - mean) / std
def wrap_env(
env: gym.Env,
state_mean: Union[np.ndarray, float] = 0.0,
state_std: Union[np.ndarray, float] = 1.0,
reward_scale: float = 1.0,
) -> gym.Env:
# PEP 8: E731 do not assign a lambda expression, use a def
def normalize_state(state):
return (
state - state_mean
) / state_std # epsilon should be already added in std.
def scale_reward(reward):
# Please be careful, here reward is multiplied by scale!
return reward_scale * reward
env = gym.wrappers.TransformObservation(env, normalize_state)
if reward_scale != 1.0:
env = gym.wrappers.TransformReward(env, scale_reward)
return env
class ReplayBuffer:
def __init__(
self,
state_dim: int,
action_dim: int,
buffer_size: int,
device: str = "cpu",
):
self._buffer_size = buffer_size
self._pointer = 0
self._size = 0
self._states = torch.zeros(
(buffer_size, state_dim), dtype=torch.float32, device=device
)
self._actions = torch.zeros(
(buffer_size, action_dim), dtype=torch.float32, device=device
)
self._rewards = torch.zeros((buffer_size, 1), dtype=torch.float32, device=device)
self._next_states = torch.zeros(
(buffer_size, state_dim), dtype=torch.float32, device=device
)
self._dones = torch.zeros((buffer_size, 1), dtype=torch.float32, device=device)
self._device = device
def _to_tensor(self, data: np.ndarray) -> torch.Tensor:
return torch.tensor(data, dtype=torch.float32, device=self._device)
# Loads data in d4rl format, i.e. from Dict[str, np.array].
def load_d4rl_dataset(self, data: Dict[str, np.ndarray]):
if self._size != 0:
raise ValueError("Trying to load data into non-empty replay buffer")
n_transitions = data["observations"].shape[0]
if n_transitions > self._buffer_size:
raise ValueError(
"Replay buffer is smaller than the dataset you are trying to load!"
)
self._states[:n_transitions] = self._to_tensor(data["observations"])
self._actions[:n_transitions] = self._to_tensor(data["actions"])
self._rewards[:n_transitions] = self._to_tensor(data["rewards"][..., None])
self._next_states[:n_transitions] = self._to_tensor(data["next_observations"])
self._dones[:n_transitions] = self._to_tensor(data["terminals"][..., None])
self._size += n_transitions
self._pointer = min(self._size, n_transitions)
print(f"Dataset size: {n_transitions}")
def sample(self, batch_size: int) -> TensorBatch:
indices = np.random.randint(0, min(self._size, self._pointer), size=batch_size)
states = self._states[indices]
actions = self._actions[indices]
rewards = self._rewards[indices]
next_states = self._next_states[indices]
dones = self._dones[indices]
return [states, actions, rewards, next_states, dones]
def add_transition(self):
# Use this method to add new data into the replay buffer during fine-tuning.
# I left it unimplemented since now we do not do fine-tuning.
raise NotImplementedError
def set_seed(
seed: int, env: Optional[gym.Env] = None, deterministic_torch: bool = False
):
if env is not None:
env.seed(seed)
env.action_space.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
random.seed(seed)
torch.manual_seed(seed)
torch.use_deterministic_algorithms(deterministic_torch)
def wandb_init(config: dict) -> None:
wandb.init(
config=config,
project=config["project"],
group=config["group"],
name=config["name"],
id=str(uuid.uuid4()),
)
wandb.run.save()
@torch.no_grad()
def eval_actor(
env: gym.Env, actor: nn.Module, device: str, n_episodes: int, seed: int
) -> np.ndarray:
env.seed(seed)
actor.eval()
episode_rewards = []
for _ in range(n_episodes):
state, done = env.reset(), False
episode_reward = 0.0
while not done:
action = actor.act(state, device)
state, reward, done, _ = env.step(action)
episode_reward += reward
episode_rewards.append(episode_reward)
actor.train()
return np.asarray(episode_rewards)
def return_reward_range(dataset: Dict, max_episode_steps: int) -> Tuple[float, float]:
returns, lengths = [], []
ep_ret, ep_len = 0.0, 0
for r, d in zip(dataset["rewards"], dataset["terminals"]):
ep_ret += float(r)
ep_len += 1
if d or ep_len == max_episode_steps:
returns.append(ep_ret)
lengths.append(ep_len)
ep_ret, ep_len = 0.0, 0
lengths.append(ep_len) # but still keep track of number of steps
assert sum(lengths) == len(dataset["rewards"])
return min(returns), max(returns)
def modify_reward(
dataset: Dict,
env_name: str,
max_episode_steps: int = 1000,
reward_scale: float = 1.0,
reward_bias: float = 0.0,
):
if any(s in env_name for s in ("halfcheetah", "hopper", "walker2d")):
min_ret, max_ret = return_reward_range(dataset, max_episode_steps)
dataset["rewards"] /= max_ret - min_ret
dataset["rewards"] *= max_episode_steps
dataset["rewards"] = dataset["rewards"] * reward_scale + reward_bias
def extend_and_repeat(tensor: torch.Tensor, dim: int, repeat: int) -> torch.Tensor:
return tensor.unsqueeze(dim).repeat_interleave(repeat, dim=dim)
def init_module_weights(module: torch.nn.Sequential, orthogonal_init: bool = False):
# Specific orthgonal initialization for inner layers
# If orthogonal init is off, we do not change default initialization
if orthogonal_init:
for submodule in module[:-1]:
if isinstance(submodule, nn.Linear):
nn.init.orthogonal_(submodule.weight, gain=np.sqrt(2))
nn.init.constant_(submodule.bias, 0.0)
# Lasy layers should be initialzied differently as well
if orthogonal_init:
nn.init.orthogonal_(module[-1].weight, gain=1e-2)
else:
nn.init.xavier_uniform_(module[-1].weight, gain=1e-2)
nn.init.constant_(module[-1].bias, 0.0)
class ReparameterizedTanhGaussian(nn.Module):
def __init__(
self, log_std_min: float = -20.0, log_std_max: float = 2.0, no_tanh: bool = False
):
super().__init__()
self.log_std_min = log_std_min
self.log_std_max = log_std_max
self.no_tanh = no_tanh
def log_prob(
self, mean: torch.Tensor, log_std: torch.Tensor, sample: torch.Tensor
) -> torch.Tensor:
log_std = torch.clamp(log_std, self.log_std_min, self.log_std_max)
std = torch.exp(log_std)
if self.no_tanh:
action_distribution = Normal(mean, std)
else:
action_distribution = TransformedDistribution(
Normal(mean, std), TanhTransform(cache_size=1)
)
return torch.sum(action_distribution.log_prob(sample), dim=-1)
def forward(
self, mean: torch.Tensor, log_std: torch.Tensor, deterministic: bool = False
) -> Tuple[torch.Tensor, torch.Tensor]:
log_std = torch.clamp(log_std, self.log_std_min, self.log_std_max)
std = torch.exp(log_std)
if self.no_tanh:
action_distribution = Normal(mean, std)
else:
action_distribution = TransformedDistribution(
Normal(mean, std), TanhTransform(cache_size=1)
)
if deterministic:
action_sample = torch.tanh(mean)
else:
action_sample = action_distribution.rsample()
log_prob = torch.sum(action_distribution.log_prob(action_sample), dim=-1)
return action_sample, log_prob
class TanhGaussianPolicy(nn.Module):
def __init__(
self,
state_dim: int,
action_dim: int,
max_action: float,
log_std_multiplier: float = 1.0,
log_std_offset: float = -1.0,
orthogonal_init: bool = False,
no_tanh: bool = False,
):
super().__init__()
self.observation_dim = state_dim
self.action_dim = action_dim
self.max_action = max_action
self.orthogonal_init = orthogonal_init
self.no_tanh = no_tanh
self.base_network = nn.Sequential(
nn.Linear(state_dim, 256),
nn.ReLU(),
nn.Linear(256, 256),
nn.ReLU(),
nn.Linear(256, 256),
nn.ReLU(),
nn.Linear(256, 2 * action_dim),
)
init_module_weights(self.base_network)
self.log_std_multiplier = Scalar(log_std_multiplier)
self.log_std_offset = Scalar(log_std_offset)
self.tanh_gaussian = ReparameterizedTanhGaussian(no_tanh=no_tanh)
def log_prob(
self, observations: torch.Tensor, actions: torch.Tensor
) -> torch.Tensor:
if actions.ndim == 3:
observations = extend_and_repeat(observations, 1, actions.shape[1])
base_network_output = self.base_network(observations)
mean, log_std = torch.split(base_network_output, self.action_dim, dim=-1)
log_std = self.log_std_multiplier() * log_std + self.log_std_offset()
_, log_probs = self.tanh_gaussian(mean, log_std, False)
return log_probs
def forward(
self,
observations: torch.Tensor,
deterministic: bool = False,
repeat: bool = None,
) -> Tuple[torch.Tensor, torch.Tensor]:
if repeat is not None:
observations = extend_and_repeat(observations, 1, repeat)
base_network_output = self.base_network(observations)
mean, log_std = torch.split(base_network_output, self.action_dim, dim=-1)
log_std = self.log_std_multiplier() * log_std + self.log_std_offset()
actions, log_probs = self.tanh_gaussian(mean, log_std, deterministic)
return self.max_action * actions, log_probs
@torch.no_grad()
def act(self, state: np.ndarray, device: str = "cpu"):
state = torch.tensor(state.reshape(1, -1), device=device, dtype=torch.float32)
with torch.no_grad():
actions, _ = self(state, not self.training)
return actions.cpu().data.numpy().flatten()
class FullyConnectedQFunction(nn.Module):
def __init__(
self,
observation_dim: int,
action_dim: int,
orthogonal_init: bool = False,
n_hidden_layers: int = 3,
):
super().__init__()
self.observation_dim = observation_dim
self.action_dim = action_dim
self.orthogonal_init = orthogonal_init
layers = [
nn.Linear(observation_dim + action_dim, 256),
nn.ReLU(),
]
for _ in range(n_hidden_layers - 1):
layers.append(nn.Linear(256, 256))
layers.append(nn.ReLU())
layers.append(nn.Linear(256, 1))
self.network = nn.Sequential(*layers)
init_module_weights(self.network, orthogonal_init)
def forward(self, observations: torch.Tensor, actions: torch.Tensor) -> torch.Tensor:
multiple_actions = False
batch_size = observations.shape[0]
if actions.ndim == 3 and observations.ndim == 2:
multiple_actions = True
observations = extend_and_repeat(observations, 1, actions.shape[1]).reshape(
-1, observations.shape[-1]
)
actions = actions.reshape(-1, actions.shape[-1])
input_tensor = torch.cat([observations, actions], dim=-1)
q_values = torch.squeeze(self.network(input_tensor), dim=-1)
if multiple_actions:
q_values = q_values.reshape(batch_size, -1)
return q_values
class Scalar(nn.Module):
def __init__(self, init_value: float):
super().__init__()
self.constant = nn.Parameter(torch.tensor(init_value, dtype=torch.float32))
def forward(self) -> nn.Parameter:
return self.constant
class ContinuousCQL:
def __init__(
self,
critic_1,
critic_1_optimizer,
critic_2,
critic_2_optimizer,
actor,
actor_optimizer,
target_entropy: float,
discount: float = 0.99,
alpha_multiplier: float = 1.0,
use_automatic_entropy_tuning: bool = True,
backup_entropy: bool = False,
policy_lr: bool = 3e-4,
qf_lr: bool = 3e-4,
soft_target_update_rate: float = 5e-3,
bc_steps=100000,
target_update_period: int = 1,
cql_n_actions: int = 10,
cql_importance_sample: bool = True,
cql_lagrange: bool = False,
cql_target_action_gap: float = -1.0,
cql_temp: float = 1.0,
cql_alpha: float = 5.0,
cql_max_target_backup: bool = False,
cql_clip_diff_min: float = -np.inf,
cql_clip_diff_max: float = np.inf,
device: str = "cpu",
):
super().__init__()
self.discount = discount
self.target_entropy = target_entropy
self.alpha_multiplier = alpha_multiplier
self.use_automatic_entropy_tuning = use_automatic_entropy_tuning
self.backup_entropy = backup_entropy
self.policy_lr = policy_lr
self.qf_lr = qf_lr
self.soft_target_update_rate = soft_target_update_rate
self.bc_steps = bc_steps
self.target_update_period = target_update_period
self.cql_n_actions = cql_n_actions
self.cql_importance_sample = cql_importance_sample
self.cql_lagrange = cql_lagrange
self.cql_target_action_gap = cql_target_action_gap
self.cql_temp = cql_temp
self.cql_alpha = cql_alpha
self.cql_max_target_backup = cql_max_target_backup
self.cql_clip_diff_min = cql_clip_diff_min
self.cql_clip_diff_max = cql_clip_diff_max
self._device = device
self.total_it = 0
self.critic_1 = critic_1
self.critic_2 = critic_2
self.target_critic_1 = deepcopy(self.critic_1).to(device)
self.target_critic_2 = deepcopy(self.critic_2).to(device)
self.actor = actor
self.actor_optimizer = actor_optimizer
self.critic_1_optimizer = critic_1_optimizer
self.critic_2_optimizer = critic_2_optimizer
if self.use_automatic_entropy_tuning:
self.log_alpha = Scalar(0.0)
self.alpha_optimizer = torch.optim.Adam(
self.log_alpha.parameters(),
lr=self.policy_lr,
)
else:
self.log_alpha = None
self.log_alpha_prime = Scalar(1.0)
self.alpha_prime_optimizer = torch.optim.Adam(
self.log_alpha_prime.parameters(),
lr=self.qf_lr,
)
self.total_it = 0
def update_target_network(self, soft_target_update_rate: float):
soft_update(self.target_critic_1, self.critic_1, soft_target_update_rate)
soft_update(self.target_critic_2, self.critic_2, soft_target_update_rate)
def _alpha_and_alpha_loss(self, observations: torch.Tensor, log_pi: torch.Tensor):
if self.use_automatic_entropy_tuning:
alpha_loss = -(
self.log_alpha() * (log_pi + self.target_entropy).detach()
).mean()
alpha = self.log_alpha().exp() * self.alpha_multiplier
else:
alpha_loss = observations.new_tensor(0.0)
alpha = observations.new_tensor(self.alpha_multiplier)
return alpha, alpha_loss
def _policy_loss(
self,
observations: torch.Tensor,
actions: torch.Tensor,
new_actions: torch.Tensor,
alpha: torch.Tensor,
log_pi: torch.Tensor,
) -> torch.Tensor:
if self.total_it <= self.bc_steps:
log_probs = self.actor.log_prob(observations, actions)
policy_loss = (alpha * log_pi - log_probs).mean()
else:
q_new_actions = torch.min(
self.critic_1(observations, new_actions),
self.critic_2(observations, new_actions),
)
policy_loss = (alpha * log_pi - q_new_actions).mean()
return policy_loss
def _q_loss(
self,
observations: torch.Tensor,
actions: torch.Tensor,
next_observations: torch.Tensor,
rewards: torch.Tensor,
dones: torch.Tensor,
alpha: torch.Tensor,
log_dict: Dict,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
q1_predicted = self.critic_1(observations, actions)
q2_predicted = self.critic_2(observations, actions)
if self.cql_max_target_backup:
new_next_actions, next_log_pi = self.actor(
next_observations, repeat=self.cql_n_actions
)
target_q_values, max_target_indices = torch.max(
torch.min(
self.target_critic_1(next_observations, new_next_actions),
self.target_critic_2(next_observations, new_next_actions),
),
dim=-1,
)
next_log_pi = torch.gather(
next_log_pi, -1, max_target_indices.unsqueeze(-1)
).squeeze(-1)
else:
new_next_actions, next_log_pi = self.actor(next_observations)
target_q_values = torch.min(
self.target_critic_1(next_observations, new_next_actions),
self.target_critic_2(next_observations, new_next_actions),
)
if self.backup_entropy:
target_q_values = target_q_values - alpha * next_log_pi
target_q_values = target_q_values.unsqueeze(-1)
td_target = rewards + (1.0 - dones) * self.discount * target_q_values.detach()
td_target = td_target.squeeze(-1)
qf1_loss = F.mse_loss(q1_predicted, td_target.detach())
qf2_loss = F.mse_loss(q2_predicted, td_target.detach())
# CQL
batch_size = actions.shape[0]
action_dim = actions.shape[-1]
cql_random_actions = actions.new_empty(
(batch_size, self.cql_n_actions, action_dim), requires_grad=False
).uniform_(-1, 1)
cql_current_actions, cql_current_log_pis = self.actor(
observations, repeat=self.cql_n_actions
)
cql_next_actions, cql_next_log_pis = self.actor(
next_observations, repeat=self.cql_n_actions
)
cql_current_actions, cql_current_log_pis = (
cql_current_actions.detach(),
cql_current_log_pis.detach(),
)
cql_next_actions, cql_next_log_pis = (
cql_next_actions.detach(),
cql_next_log_pis.detach(),
)
cql_q1_rand = self.critic_1(observations, cql_random_actions)
cql_q2_rand = self.critic_2(observations, cql_random_actions)
cql_q1_current_actions = self.critic_1(observations, cql_current_actions)
cql_q2_current_actions = self.critic_2(observations, cql_current_actions)
cql_q1_next_actions = self.critic_1(observations, cql_next_actions)
cql_q2_next_actions = self.critic_2(observations, cql_next_actions)
cql_cat_q1 = torch.cat(
[
cql_q1_rand,
torch.unsqueeze(q1_predicted, 1),
cql_q1_next_actions,
cql_q1_current_actions,
],
dim=1,
)
cql_cat_q2 = torch.cat(
[
cql_q2_rand,
torch.unsqueeze(q2_predicted, 1),
cql_q2_next_actions,
cql_q2_current_actions,
],
dim=1,
)
cql_std_q1 = torch.std(cql_cat_q1, dim=1)
cql_std_q2 = torch.std(cql_cat_q2, dim=1)
if self.cql_importance_sample:
random_density = np.log(0.5**action_dim)
cql_cat_q1 = torch.cat(
[
cql_q1_rand - random_density,
cql_q1_next_actions - cql_next_log_pis.detach(),
cql_q1_current_actions - cql_current_log_pis.detach(),
],
dim=1,
)
cql_cat_q2 = torch.cat(
[
cql_q2_rand - random_density,
cql_q2_next_actions - cql_next_log_pis.detach(),
cql_q2_current_actions - cql_current_log_pis.detach(),
],
dim=1,
)
cql_qf1_ood = torch.logsumexp(cql_cat_q1 / self.cql_temp, dim=1) * self.cql_temp
cql_qf2_ood = torch.logsumexp(cql_cat_q2 / self.cql_temp, dim=1) * self.cql_temp
"""Subtract the log likelihood of data"""
cql_qf1_diff = torch.clamp(
cql_qf1_ood - q1_predicted,
self.cql_clip_diff_min,
self.cql_clip_diff_max,
).mean()
cql_qf2_diff = torch.clamp(
cql_qf2_ood - q2_predicted,
self.cql_clip_diff_min,
self.cql_clip_diff_max,
).mean()
if self.cql_lagrange:
alpha_prime = torch.clamp(
torch.exp(self.log_alpha_prime()), min=0.0, max=1000000.0
)
cql_min_qf1_loss = (
alpha_prime
* self.cql_alpha
* (cql_qf1_diff - self.cql_target_action_gap)
)
cql_min_qf2_loss = (
alpha_prime
* self.cql_alpha
* (cql_qf2_diff - self.cql_target_action_gap)
)
self.alpha_prime_optimizer.zero_grad()
alpha_prime_loss = (-cql_min_qf1_loss - cql_min_qf2_loss) * 0.5
alpha_prime_loss.backward(retain_graph=True)
self.alpha_prime_optimizer.step()
else:
cql_min_qf1_loss = cql_qf1_diff * self.cql_alpha
cql_min_qf2_loss = cql_qf2_diff * self.cql_alpha
alpha_prime_loss = observations.new_tensor(0.0)
alpha_prime = observations.new_tensor(0.0)
qf_loss = qf1_loss + qf2_loss + cql_min_qf1_loss + cql_min_qf2_loss
log_dict.update(
dict(
qf1_loss=qf1_loss.item(),
qf2_loss=qf2_loss.item(),
alpha=alpha.item(),
average_qf1=q1_predicted.mean().item(),
average_qf2=q2_predicted.mean().item(),
average_target_q=target_q_values.mean().item(),
)
)
log_dict.update(
dict(
cql_std_q1=cql_std_q1.mean().item(),
cql_std_q2=cql_std_q2.mean().item(),
cql_q1_rand=cql_q1_rand.mean().item(),
cql_q2_rand=cql_q2_rand.mean().item(),
cql_min_qf1_loss=cql_min_qf1_loss.mean().item(),
cql_min_qf2_loss=cql_min_qf2_loss.mean().item(),
cql_qf1_diff=cql_qf1_diff.mean().item(),
cql_qf2_diff=cql_qf2_diff.mean().item(),
cql_q1_current_actions=cql_q1_current_actions.mean().item(),
cql_q2_current_actions=cql_q2_current_actions.mean().item(),
cql_q1_next_actions=cql_q1_next_actions.mean().item(),
cql_q2_next_actions=cql_q2_next_actions.mean().item(),
alpha_prime_loss=alpha_prime_loss.item(),
alpha_prime=alpha_prime.item(),
)
)
return qf_loss, alpha_prime, alpha_prime_loss
def train(self, batch: TensorBatch) -> Dict[str, float]:
(
observations,
actions,
rewards,
next_observations,
dones,
) = batch
self.total_it += 1
new_actions, log_pi = self.actor(observations)
alpha, alpha_loss = self._alpha_and_alpha_loss(observations, log_pi)
""" Policy loss """
policy_loss = self._policy_loss(
observations, actions, new_actions, alpha, log_pi
)
log_dict = dict(
log_pi=log_pi.mean().item(),
policy_loss=policy_loss.item(),
alpha_loss=alpha_loss.item(),
alpha=alpha.item(),
)
""" Q function loss """
qf_loss, alpha_prime, alpha_prime_loss = self._q_loss(
observations, actions, next_observations, rewards, dones, alpha, log_dict
)
if self.use_automatic_entropy_tuning:
self.alpha_optimizer.zero_grad()
alpha_loss.backward()
self.alpha_optimizer.step()
self.actor_optimizer.zero_grad()
policy_loss.backward()
self.actor_optimizer.step()
self.critic_1_optimizer.zero_grad()
self.critic_2_optimizer.zero_grad()
qf_loss.backward(retain_graph=True)
self.critic_1_optimizer.step()
self.critic_2_optimizer.step()
if self.total_it % self.target_update_period == 0:
self.update_target_network(self.soft_target_update_rate)
return log_dict
def state_dict(self) -> Dict[str, Any]:
return {
"actor": self.actor.state_dict(),
"critic1": self.critic_1.state_dict(),
"critic2": self.critic_2.state_dict(),
"critic1_target": self.target_critic_1.state_dict(),
"critic2_target": self.target_critic_2.state_dict(),
"critic_1_optimizer": self.critic_1_optimizer.state_dict(),
"critic_2_optimizer": self.critic_2_optimizer.state_dict(),
"actor_optim": self.actor_optimizer.state_dict(),
"sac_log_alpha": self.log_alpha,
"sac_log_alpha_optim": self.alpha_optimizer.state_dict(),
"cql_log_alpha": self.log_alpha_prime,
"cql_log_alpha_optim": self.alpha_prime_optimizer.state_dict(),
"total_it": self.total_it,
}
def load_state_dict(self, state_dict: Dict[str, Any]):
self.actor.load_state_dict(state_dict=state_dict["actor"])
self.critic_1.load_state_dict(state_dict=state_dict["critic1"])
self.critic_2.load_state_dict(state_dict=state_dict["critic2"])
self.target_critic_1.load_state_dict(state_dict=state_dict["critic1_target"])
self.target_critic_2.load_state_dict(state_dict=state_dict["critic2_target"])
self.critic_1_optimizer.load_state_dict(
state_dict=state_dict["critic_1_optimizer"]
)
self.critic_2_optimizer.load_state_dict(
state_dict=state_dict["critic_2_optimizer"]
)
self.actor_optimizer.load_state_dict(state_dict=state_dict["actor_optim"])
self.log_alpha = state_dict["sac_log_alpha"]
self.alpha_optimizer.load_state_dict(
state_dict=state_dict["sac_log_alpha_optim"]
)
self.log_alpha_prime = state_dict["cql_log_alpha"]
self.alpha_prime_optimizer.load_state_dict(
state_dict=state_dict["cql_log_alpha_optim"]
)
self.total_it = state_dict["total_it"]
@pyrallis.wrap()
def train(config: TrainConfig):
env = gym.make(config.env)
state_dim = env.observation_space.shape[0]
action_dim = env.action_space.shape[0]
dataset = d4rl.qlearning_dataset(env)
if config.normalize_reward:
modify_reward(
dataset,
config.env,
reward_scale=config.reward_scale,
reward_bias=config.reward_bias,
)
if config.normalize:
state_mean, state_std = compute_mean_std(dataset["observations"], eps=1e-3)
else:
state_mean, state_std = 0, 1
dataset["observations"] = normalize_states(
dataset["observations"], state_mean, state_std
)
dataset["next_observations"] = normalize_states(
dataset["next_observations"], state_mean, state_std
)
env = wrap_env(env, state_mean=state_mean, state_std=state_std)
replay_buffer = ReplayBuffer(
state_dim,
action_dim,
config.buffer_size,
config.device,
)
replay_buffer.load_d4rl_dataset(dataset)
max_action = float(env.action_space.high[0])
if config.checkpoints_path is not None:
print(f"Checkpoints path: {config.checkpoints_path}")
os.makedirs(config.checkpoints_path, exist_ok=True)
with open(os.path.join(config.checkpoints_path, "config.yaml"), "w") as f:
pyrallis.dump(config, f)
# Set seeds
seed = config.seed
set_seed(seed, env)
critic_1 = FullyConnectedQFunction(
state_dim,
action_dim,
config.orthogonal_init,
config.q_n_hidden_layers,
).to(config.device)
critic_2 = FullyConnectedQFunction(state_dim, action_dim, config.orthogonal_init).to(
config.device
)
critic_1_optimizer = torch.optim.Adam(list(critic_1.parameters()), config.qf_lr)
critic_2_optimizer = torch.optim.Adam(list(critic_2.parameters()), config.qf_lr)
actor = TanhGaussianPolicy(
state_dim,
action_dim,
max_action,
log_std_multiplier=config.policy_log_std_multiplier,
orthogonal_init=config.orthogonal_init,
).to(config.device)
actor_optimizer = torch.optim.Adam(actor.parameters(), config.policy_lr)
kwargs = {
"critic_1": critic_1,
"critic_2": critic_2,
"critic_1_optimizer": critic_1_optimizer,
"critic_2_optimizer": critic_2_optimizer,
"actor": actor,
"actor_optimizer": actor_optimizer,
"discount": config.discount,
"soft_target_update_rate": config.soft_target_update_rate,
"device": config.device,
# CQL
"target_entropy": -np.prod(env.action_space.shape).item(),
"alpha_multiplier": config.alpha_multiplier,
"use_automatic_entropy_tuning": config.use_automatic_entropy_tuning,
"backup_entropy": config.backup_entropy,
"policy_lr": config.policy_lr,
"qf_lr": config.qf_lr,
"bc_steps": config.bc_steps,
"target_update_period": config.target_update_period,
"cql_n_actions": config.cql_n_actions,
"cql_importance_sample": config.cql_importance_sample,
"cql_lagrange": config.cql_lagrange,
"cql_target_action_gap": config.cql_target_action_gap,
"cql_temp": config.cql_temp,
"cql_alpha": config.cql_alpha,
"cql_max_target_backup": config.cql_max_target_backup,
"cql_clip_diff_min": config.cql_clip_diff_min,
"cql_clip_diff_max": config.cql_clip_diff_max,
}
print("---------------------------------------")
print(f"Training CQL, Env: {config.env}, Seed: {seed}")
print("---------------------------------------")
# Initialize actor
trainer = ContinuousCQL(**kwargs)
if config.load_model != "":
policy_file = Path(config.load_model)
trainer.load_state_dict(torch.load(policy_file))
actor = trainer.actor
wandb_init(asdict(config))
evaluations = []
for t in range(int(config.max_timesteps)):
batch = replay_buffer.sample(config.batch_size)
batch = [b.to(config.device) for b in batch]
log_dict = trainer.train(batch)
wandb.log(log_dict, step=trainer.total_it)
# Evaluate episode
if (t + 1) % config.eval_freq == 0:
print(f"Time steps: {t + 1}")
eval_scores = eval_actor(
env,
actor,
device=config.device,
n_episodes=config.n_episodes,
seed=config.seed,
)
eval_score = eval_scores.mean()
normalized_eval_score = env.get_normalized_score(eval_score) * 100.0
evaluations.append(normalized_eval_score)
print("---------------------------------------")
print(
f"Evaluation over {config.n_episodes} episodes: "
f"{eval_score:.3f} , D4RL score: {normalized_eval_score:.3f}"
)
print("---------------------------------------")
if config.checkpoints_path:
torch.save(
trainer.state_dict(),
os.path.join(config.checkpoints_path, f"checkpoint_{t}.pt"),
)
wandb.log(
{"d4rl_normalized_score": normalized_eval_score},
step=trainer.total_it,
)
if __name__ == "__main__":
train()