-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSAC_Pendulum_PyTorch.py
More file actions
317 lines (250 loc) · 9.99 KB
/
Copy pathSAC_Pendulum_PyTorch.py
File metadata and controls
317 lines (250 loc) · 9.99 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
# -*- coding: utf-8 -*-
"""
=============================================================================
ELC 5365: Deep Learning, Spring 2026
Dr. Liang Dong, Baylor University
Soft Actor-Critic (SAC) on Pendulum-v1
--------------------------------------
SAC is the state-of-the-art off-policy actor-critic for continuous
control. It maximizes a *maximum-entropy* objective
J(pi) = E_tau [ sum_t r_t + alpha * H( pi(.|s_t) ) ]
so the policy is rewarded for being both rewarding *and* stochastic.
Three pieces make it work in practice:
(1) Squashed-Gaussian policy.
The actor outputs (mu, log_sigma). We sample u ~ N(mu, sigma)
and squash a = tanh(u) * act_high using the reparameterization
trick. The Jacobian of tanh contributes a correction term to
the log-density:
log pi(a|s) = log N(u|mu,sigma) - sum_i log(1 - tanh(u_i)^2)
(the second term is computed in a numerically stable form).
(2) Twin critics with clipped double Q-learning (as in TD3).
(3) Automatic entropy tuning.
Treat alpha as a learnable parameter; minimize
J(alpha) = E[ -alpha * (log pi(a|s) + H_target) ]
with H_target = -dim(A). This adapts the exploration pressure
to the actor's current entropy.
References:
T. Haarnoja, A. Zhou, P. Abbeel, S. Levine, "Soft Actor-Critic:
Off-Policy Maximum Entropy Deep RL with a Stochastic Actor,"
ICML 2018.
T. Haarnoja et al., "Soft Actor-Critic Algorithms and Applications,"
arXiv:1812.05905, 2018.
Install:
pip install gymnasium gymnasium[classic-control] torch numpy matplotlib
=============================================================================
"""
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.distributions import Normal
from collections import deque
import random
import gymnasium as gym
import matplotlib.pyplot as plt
# ------------------------------ environment ---------------------------------
env = gym.make("Pendulum-v1", render_mode=None)
render_env = gym.make("Pendulum-v1", render_mode="human")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Device: {device}")
RENDER_EVERY = 30
N_FINAL_DEMOS = 3
# ---------------------------- hyperparameters -------------------------------
GAMMA = 0.99
TAU = 0.005
LR = 3e-4
BATCH_SIZE = 256
BUFFER_CAP = 100_000
MAX_EPISODES = 150
WARMUP_STEPS = 1000
LOG_SIGMA_MIN = -20.0
LOG_SIGMA_MAX = 2.0
# ---------------------------- networks --------------------------------------
class SquashedGaussianActor(nn.Module):
"""
Outputs mean and log_sigma. Sampling uses the reparameterization
trick; the squashed action is tanh(u) * act_high, with the
standard tanh-Jacobian correction applied to the log density.
"""
def __init__(self, n_obs, n_act, act_high):
super().__init__()
self.act_high = torch.tensor(act_high, dtype=torch.float32, device=device)
self.trunk = nn.Sequential(
nn.Linear(n_obs, 256), nn.ReLU(),
nn.Linear(256, 256), nn.ReLU(),
)
self.mu_head = nn.Linear(256, n_act)
self.log_sigma_head = nn.Linear(256, n_act)
def forward(self, s):
h = self.trunk(s)
mu = self.mu_head(h)
log_sigma = self.log_sigma_head(h).clamp(LOG_SIGMA_MIN, LOG_SIGMA_MAX)
return mu, log_sigma
def sample(self, s):
mu, log_sigma = self.forward(s)
sigma = log_sigma.exp()
dist = Normal(mu, sigma)
u = dist.rsample() # reparameterized
a_t = torch.tanh(u) # in [-1, 1]
a = a_t * self.act_high
# log pi = log N(u) - sum log(1 - tanh(u)^2)
# Numerically stable form: log(1 - tanh(u)^2) = 2*(log 2 - u - softplus(-2u))
log_prob = dist.log_prob(u)
log_prob -= 2.0 * (np.log(2.0) - u - nn.functional.softplus(-2.0 * u))
log_prob = log_prob.sum(dim=-1)
return a, log_prob
class TwinCritic(nn.Module):
def __init__(self, n_obs, n_act):
super().__init__()
self.q1 = nn.Sequential(
nn.Linear(n_obs + n_act, 256), nn.ReLU(),
nn.Linear(256, 256), nn.ReLU(),
nn.Linear(256, 1),
)
self.q2 = nn.Sequential(
nn.Linear(n_obs + n_act, 256), nn.ReLU(),
nn.Linear(256, 256), nn.ReLU(),
nn.Linear(256, 1),
)
def forward(self, s, a):
sa = torch.cat([s, a], dim=-1)
return self.q1(sa).squeeze(-1), self.q2(sa).squeeze(-1)
# ---------------------------- replay buffer ---------------------------------
class ReplayBuffer:
def __init__(self, capacity):
self.buf = deque(maxlen=capacity)
def push(self, *transition):
self.buf.append(transition)
def sample(self, batch_size):
batch = random.sample(self.buf, batch_size)
s, a, r, s_next, done = map(np.array, zip(*batch))
return (
torch.tensor(s, dtype=torch.float32, device=device),
torch.tensor(a, dtype=torch.float32, device=device),
torch.tensor(r, dtype=torch.float32, device=device),
torch.tensor(s_next, dtype=torch.float32, device=device),
torch.tensor(done, dtype=torch.float32, device=device),
)
def __len__(self):
return len(self.buf)
# ---------------------------- setup -----------------------------------------
n_obs = env.observation_space.shape[0]
n_act = env.action_space.shape[0]
act_high = env.action_space.high
act_low = env.action_space.low
actor = SquashedGaussianActor(n_obs, n_act, act_high).to(device)
critic = TwinCritic(n_obs, n_act).to(device)
critic_target = TwinCritic(n_obs, n_act).to(device)
critic_target.load_state_dict(critic.state_dict())
opt_a = optim.Adam(actor.parameters(), lr=LR)
opt_c = optim.Adam(critic.parameters(), lr=LR)
# ----- automatic entropy tuning -----
target_entropy = -float(n_act) # heuristic from the SAC paper
log_alpha = torch.zeros(1, requires_grad=True, device=device)
opt_alpha = optim.Adam([log_alpha], lr=LR)
buffer = ReplayBuffer(BUFFER_CAP)
def polyak(net, target, tau):
for p, p_t in zip(net.parameters(), target.parameters()):
p_t.data.mul_(1.0 - tau).add_(p.data, alpha=tau)
def select_action(state, deterministic=False):
s = torch.tensor(state, dtype=torch.float32, device=device).unsqueeze(0)
with torch.no_grad():
if deterministic:
mu, _ = actor(s)
a = torch.tanh(mu) * actor.act_high
else:
a, _ = actor.sample(s)
return a.cpu().numpy()[0].astype(np.float32)
def update():
if len(buffer) < BATCH_SIZE:
return
s, a, r, s_next, done = buffer.sample(BATCH_SIZE)
alpha = log_alpha.exp().detach()
# ----- target value: r + gamma * (min Q' - alpha * log pi) -----
with torch.no_grad():
a_next, logp_next = actor.sample(s_next)
q1_t, q2_t = critic_target(s_next, a_next)
q_next = torch.min(q1_t, q2_t) - alpha * logp_next
target = r + GAMMA * (1.0 - done) * q_next
# ----- critic update -----
q1, q2 = critic(s, a)
critic_loss = nn.functional.mse_loss(q1, target) + nn.functional.mse_loss(q2, target)
opt_c.zero_grad()
critic_loss.backward()
opt_c.step()
# ----- actor update (reparameterized) -----
a_pi, logp_pi = actor.sample(s)
q1_pi, q2_pi = critic(s, a_pi)
q_pi = torch.min(q1_pi, q2_pi)
actor_loss = (alpha * logp_pi - q_pi).mean()
opt_a.zero_grad()
actor_loss.backward()
opt_a.step()
# ----- temperature update -----
alpha_loss = -(log_alpha * (logp_pi.detach() + target_entropy)).mean()
opt_alpha.zero_grad()
alpha_loss.backward()
opt_alpha.step()
# ----- target critic Polyak update -----
polyak(critic, critic_target, TAU)
def render_demo(label):
"""Deterministic (mean) policy episode in the rendered env."""
s, _ = render_env.reset()
total = 0.0
while True:
a = select_action(s, deterministic=True)
s, r, terminated, truncated, _ = render_env.step(a)
total += r
if terminated or truncated:
break
print(f" [demo {label}] return = {total:7.1f}")
# ---------------------------- training loop ---------------------------------
episode_returns = []
total_steps = 0
print("Demo before training (untrained policy)...")
render_demo(label="ep 0")
for ep in range(MAX_EPISODES):
s, _ = env.reset()
ep_ret = 0.0
while True:
if total_steps < WARMUP_STEPS:
a = env.action_space.sample().astype(np.float32)
else:
a = select_action(s)
s_next, r, terminated, truncated, _ = env.step(a)
done = terminated or truncated
buffer.push(s, a, r, s_next, float(terminated))
s = s_next
ep_ret += r
total_steps += 1
update()
if done:
break
episode_returns.append(ep_ret)
if (ep + 1) % 10 == 0:
recent = episode_returns[-10:]
print(f"Episode {ep+1:3d}/{MAX_EPISODES} "
f"return(last 10) avg = {sum(recent)/len(recent):8.1f} "
f"alpha = {log_alpha.exp().item():.3f}")
if (ep + 1) % RENDER_EVERY == 0:
render_demo(label=f"ep{ep+1:3d}")
print(f"Final showcase ({N_FINAL_DEMOS} rendered episodes)...")
for k in range(N_FINAL_DEMOS):
render_demo(label=f"final {k+1}")
# ---------------------------- plot ------------------------------------------
plt.figure(figsize=(8, 4))
ret = torch.tensor(episode_returns, dtype=torch.float)
plt.plot(ret.numpy(), alpha=0.5, label="episode return")
if len(ret) >= 10:
means = ret.unfold(0, 10, 1).mean(1)
means = torch.cat((torch.zeros(9), means))
plt.plot(means.numpy(), label="10-ep moving average")
plt.xlabel("Episode")
plt.ylabel("Return")
plt.title("SAC on Pendulum-v1")
plt.legend()
plt.tight_layout()
plt.savefig("SAC_Pendulum_curve.png", dpi=120)
plt.show()
env.close()
render_env.close()