-
Notifications
You must be signed in to change notification settings - Fork 0
/
sac.py
141 lines (114 loc) · 4.6 KB
/
sac.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
import numpy as np
import torch
import torch.nn as nn
from copy import deepcopy
class SACPolicy(nn.Module):
def __init__(
self,
actor,
critic1,
critic2,
actor_optim,
critic1_optim,
critic2_optim,
action_space,
dist,
tau=0.005,
gamma=0.99,
alpha=0.2,
device="cpu"
):
super().__init__()
self.actor = actor
self.critic1, self.critic1_old = critic1, deepcopy(critic1)
self.critic1_old.eval()
self.critic2, self.critic2_old = critic2, deepcopy(critic2)
self.critic2_old.eval()
self.actor_optim = actor_optim
self.critic1_optim = critic1_optim
self.critic2_optim = critic2_optim
self.action_space = action_space
self.dist = dist
self._tau = tau
self._gamma = gamma
self._is_auto_alpha = False
if isinstance(alpha, tuple):
self._is_auto_alpha = True
self._target_entropy, self._log_alpha, self._alpha_optim = alpha
self._alpha = self._log_alpha.detach().exp()
else:
self._alpha = alpha
self.__eps = np.finfo(np.float32).eps.item()
self._device = device
def train(self):
self.actor.train()
self.critic1.train()
self.critic2.train()
def eval(self):
self.actor.eval()
self.critic1.eval()
self.critic2.eval()
def _sync_weight(self):
for o, n in zip(self.critic1_old.parameters(), self.critic1.parameters()):
o.data.copy_(o.data * (1.0 - self._tau) + n.data * self._tau)
for o, n in zip(self.critic2_old.parameters(), self.critic2.parameters()):
o.data.copy_(o.data * (1.0 - self._tau) + n.data * self._tau)
def forward(self, obs, deterministic=False):
dist = self.actor.get_dist(obs)
if deterministic:
action = dist.mode()
else:
action = dist.rsample()
log_prob = dist.log_prob(action)
action_scale = torch.tensor((self.action_space.high - self.action_space.low) / 2, device=action.device)
squashed_action = torch.tanh(action)
log_prob = log_prob - torch.log(action_scale * (1 - squashed_action.pow(2)) + self.__eps).sum(-1, keepdim=True)
return squashed_action, log_prob
def sample_action(self, obs, deterministic=False):
action, _ = self(obs, deterministic)
return action.cpu().detach().numpy()
def learn(self, data):
obs, actions, next_obs, terminals, rewards = data["observations"], \
data["actions"], data["next_observations"], data["terminals"], data["rewards"]
rewards = torch.as_tensor(rewards).to(self._device)
terminals = torch.as_tensor(terminals).to(self._device)
# update critic
q1, q2 = self.critic1(obs, actions).flatten(), self.critic2(obs, actions).flatten()
with torch.no_grad():
next_actions, next_log_probs = self(next_obs)
next_q = torch.min(
self.critic1_old(next_obs, next_actions), self.critic2_old(next_obs, next_actions)
) - self._alpha * next_log_probs
target_q = rewards.flatten() + self._gamma * (1 - terminals.flatten()) * next_q.flatten()
critic1_loss = ((q1 - target_q).pow(2)).mean()
self.critic1_optim.zero_grad()
critic1_loss.backward()
self.critic1_optim.step()
critic2_loss = ((q2 - target_q).pow(2)).mean()
self.critic2_optim.zero_grad()
critic2_loss.backward()
self.critic2_optim.step()
# update actor
a, log_probs = self(obs)
q1a, q2a = self.critic1(obs, a).flatten(), self.critic2(obs, a).flatten()
actor_loss = (self._alpha * log_probs.flatten() - torch.min(q1a, q2a)).mean()
self.actor_optim.zero_grad()
actor_loss.backward()
self.actor_optim.step()
if self._is_auto_alpha:
log_probs = log_probs.detach() + self._target_entropy
alpha_loss = -(self._log_alpha * log_probs).mean()
self._alpha_optim.zero_grad()
alpha_loss.backward()
self._alpha_optim.step()
self._alpha = self._log_alpha.detach().exp()
self._sync_weight()
result = {
"loss/actor": actor_loss.item(),
"loss/critic1": critic1_loss.item(),
"loss/critic2": critic2_loss.item()
}
if self._is_auto_alpha:
result["loss/alpha"] = alpha_loss.item()
result["alpha"] = self._alpha.item()
return result