-
Notifications
You must be signed in to change notification settings - Fork 0
/
awac.py
125 lines (96 loc) · 4.05 KB
/
awac.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
from copy import deepcopy
import torch
from torch import nn
from torch.nn import functional as F
from modules import Actor, EnsembledCritic
from config import awac_config
from typing import Dict, Union, Any
_Number = Union[float, int]
class AWAC:
def __init__(self,
cfg: awac_config,
actor: Actor,
actor_optimizer: torch.optim.Optimizer,
critic: EnsembledCritic,
critic_optimizer: torch.optim.Optimizer) -> None:
self.cfg = cfg
self.device = cfg.device
self.actor = actor.to(self.device)
self.actor_optimizer = actor_optimizer
self.critic = critic.to(self.device)
self.critic_optimizer = critic_optimizer
self.target_critic = deepcopy(critic).to(self.device)
self.gamma = cfg.gamma
self.tau = cfg.tau
self.awac_lambda = cfg.awac_lambda
self.exp_adv_max = cfg.exp_adv_max
self.total_iterations = 0
def train(self,
states: torch.Tensor,
actions: torch.Tensor,
rewards: torch.Tensor,
next_states: torch.Tensor,
dones: torch.Tensor) -> Dict[str, _Number]:
self.total_iterations += 1
# critic step
with torch.no_grad():
next_action, _ = self.actor(next_states)
q_next = self.target_critic(next_states, next_action).min(0).values
assert q_next.unsqueeze(-1).shape == dones.shape == rewards.shape
tgt_q = rewards + self.gamma * (1 - dones) * q_next.unsqueeze(-1)
current_q = self.critic(states, actions)
critic_loss = F.mse_loss(current_q, tgt_q.squeeze(1))
self.critic_optimizer.zero_grad()
critic_loss.backward()
self.critic_optimizer.step()
# actor step
with torch.no_grad():
policy_action, _ = self.actor(states)
q_pi = self.critic(states, policy_action)
value = q_pi.min(0).values
q_beta = self.critic(states, actions)
q_value = q_beta.min(0).values
advantage = q_value - value
weights = torch.clamp_max(
torch.exp(advantage / self.awac_lambda), self.exp_adv_max
)
action_log_prob = self.actor.log_prob(states, actions)
actor_loss = (-action_log_prob * weights).mean()
self.actor_optimizer.zero_grad()
actor_loss.backward()
self.actor_optimizer.step()
self.soft_critic_update()
return {
"critic_loss": critic_loss.item(),
"actor_loss": actor_loss.item()
}
def actor_loss(self,
states: torch.Tensor,
actions: torch.Tensor) -> torch.Tensor:
with torch.no_grad():
policy_action, _ = self.actor(states)
policy_action = policy_action.detach()
q_pi = self.critic(states, policy_action)
value = q_pi.min(0).values
q_beta = self.critic(states, actions)
q_value = q_beta.min(0).values
advantage = q_value - value
weights = torch.clamp_max(
torch.exp(advantage / self.awac_lambda), self.exp_adv_max
)
action_log_prob = self.actor.log_prob(states, actions)
loss = -action_log_prob * weights
return loss.mean()
def soft_critic_update(self):
for param, tgt_param in zip(self.critic.parameters(), self.target_critic.parameters()):
tgt_param.data.copy_(self.tau * param.data + (1.0 - self.tau) * tgt_param.data)
def state_dict(self) -> Dict[str, Any]:
return {
"actor": self.actor.state_dict(),
"critic": self.critic.state_dict(),
"target_critic": self.target_critic.state_dict()
}
def load_state_dict(self, state_dict: Dict[str, Any]):
self.actor.load_state_dict(state_dict["actor"])
self.critic.load_state_dict(state_dict["critic"])
self.target_critic.load_state_dict(state_dict["target_critic"])