-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathA2C_CartPole_PyTorch.py
More file actions
203 lines (157 loc) · 6.66 KB
/
Copy pathA2C_CartPole_PyTorch.py
File metadata and controls
203 lines (157 loc) · 6.66 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
# -*- coding: utf-8 -*-
"""
=============================================================================
ELC 5365: Deep Learning, Spring 2026
Dr. Liang Dong, Baylor University
Advantage Actor-Critic (A2C) on CartPole-v1
-------------------------------------------
Two networks share the same trunk:
Actor pi_theta(a|s) : Categorical over actions
Critic V_phi(s) : scalar baseline (state value)
Per transition we compute the one-step TD residual
delta_t = r_{t+1} + gamma * V_phi(s_{t+1}) - V_phi(s_t)
which serves as an unbiased low-variance estimate of the advantage
A^pi(s_t, a_t).
Actor loss : - log pi_theta(a_t|s_t) * delta_t.detach()
Critic loss : 0.5 * delta_t^2
In contrast to REINFORCE, A2C bootstraps from V_phi instead of
waiting until the episode ends -- much lower variance, faster learning,
at the cost of some bias from V_phi's approximation error.
References:
V. Mnih et al., "Asynchronous Methods for Deep RL" (A3C), ICML 2016.
Install:
pip install gymnasium gymnasium[classic-control] torch matplotlib
=============================================================================
"""
import torch
import torch.nn as nn
import torch.optim as optim
from torch.distributions import Categorical
import gymnasium as gym
import matplotlib.pyplot as plt
# ------------------------------ environment ---------------------------------
env = gym.make("CartPole-v1", render_mode=None)
render_env = gym.make("CartPole-v1", render_mode="human")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Device: {device}")
RENDER_EVERY = 100
N_FINAL_DEMOS = 3
# ---------------------------- hyperparameters -------------------------------
GAMMA = 0.99
LR = 3e-4
ENTROPY_COEF = 0.01 # encourages exploration
NUM_EPISODES = 800 if device.type == "cpu" else 1500
# ---------------------------- shared-trunk model ----------------------------
class ActorCritic(nn.Module):
"""Shared MLP trunk -> two heads (logits, scalar value)."""
def __init__(self, n_obs, n_actions):
super().__init__()
self.trunk = nn.Sequential(
nn.Linear(n_obs, 128), nn.ReLU(),
nn.Linear(128, 128), nn.ReLU(),
)
self.actor_head = nn.Linear(128, n_actions)
self.critic_head = nn.Linear(128, 1)
def forward(self, s):
h = self.trunk(s)
return self.actor_head(h), self.critic_head(h).squeeze(-1)
# ---------------------------- setup -----------------------------------------
n_actions = env.action_space.n
state, _ = env.reset()
n_obs = len(state)
net = ActorCritic(n_obs, n_actions).to(device)
optimizer = optim.Adam(net.parameters(), lr=LR)
def select_action(state):
s = torch.tensor(state, dtype=torch.float32, device=device).unsqueeze(0)
logits, value = net(s)
dist = Categorical(logits=logits)
a = dist.sample()
return int(a.item()), dist.log_prob(a), dist.entropy(), value.squeeze(0)
def render_demo(label):
"""Greedy (argmax of actor logits) episode in the rendered env."""
state, _ = render_env.reset()
total = 0
while True:
with torch.no_grad():
s = torch.tensor(state, dtype=torch.float32, device=device).unsqueeze(0)
logits, _ = net(s)
action = int(logits.argmax(dim=1).item())
state, r, terminated, truncated, _ = render_env.step(action)
total += r
if terminated or truncated:
break
print(f" [demo {label}] return = {total:.0f}")
# ---------------------------- training loop ---------------------------------
episode_returns = []
print("Demo before training (untrained policy)...")
render_demo(label="ep 0")
for i_episode in range(NUM_EPISODES):
state, _ = env.reset()
log_probs, values, rewards, entropies, dones = [], [], [], [], []
while True:
action, lp, ent, v = select_action(state)
next_state, r, terminated, truncated, _ = env.step(action)
done = terminated or truncated
log_probs.append(lp)
values .append(v)
rewards .append(r)
entropies.append(ent)
dones .append(float(terminated)) # use terminated (not truncated) for bootstrap masking
state = next_state
if done:
break
# ------- one-step TD targets and advantages over the trajectory ---------
T = len(rewards)
with torch.no_grad():
s_T = torch.tensor(state, dtype=torch.float32, device=device).unsqueeze(0)
_, v_last = net(s_T)
v_last = v_last.squeeze(0)
# if the episode terminated, bootstrap target at the final step is 0
if dones[-1] == 1.0:
v_last = torch.zeros_like(v_last)
rewards_t = torch.tensor(rewards, dtype=torch.float32, device=device)
values_t = torch.stack(values)
log_probs = torch.stack(log_probs)
entropies = torch.stack(entropies)
# bootstrap value V(s_{t+1}); for t = T-1 use v_last
next_values = torch.cat([values_t[1:], v_last.unsqueeze(0)])
# zero out the bootstrap on terminated transitions
not_done = 1.0 - torch.tensor(dones, dtype=torch.float32, device=device)
td_target = rewards_t + GAMMA * next_values * not_done
advantages = (td_target - values_t).detach()
# ------- losses ---------------------------------------------------------
actor_loss = -(log_probs * advantages).mean()
critic_loss = 0.5 * (td_target.detach() - values_t).pow(2).mean()
entropy_bonus = entropies.mean()
loss = actor_loss + critic_loss - ENTROPY_COEF * entropy_bonus
optimizer.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm_(net.parameters(), 0.5)
optimizer.step()
episode_returns.append(sum(rewards))
if (i_episode + 1) % 20 == 0:
recent = episode_returns[-20:]
print(f"Episode {i_episode+1:4d}/{NUM_EPISODES} "
f"return(last 20) avg = {sum(recent)/len(recent):6.1f}")
if (i_episode + 1) % RENDER_EVERY == 0:
render_demo(label=f"ep{i_episode+1:4d}")
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.4, label="episode return")
if len(ret) >= 100:
means = ret.unfold(0, 100, 1).mean(1)
means = torch.cat((torch.zeros(99), means))
plt.plot(means.numpy(), label="100-ep moving average")
plt.xlabel("Episode")
plt.ylabel("Return")
plt.title("A2C on CartPole-v1")
plt.legend()
plt.tight_layout()
plt.savefig("A2C_CartPole_curve.png", dpi=120)
plt.show()
env.close()
render_env.close()