-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathagent.py
247 lines (219 loc) · 9.02 KB
/
agent.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
import math
import gymnasium as gym
import torch
import torch.nn.functional as F
from torch import Tensor
from torch.distributions import Categorical
from torchmetrics import MeanMetric
from lightning.pytorch import LightningModule
from rl.loss import entropy_loss, policy_loss, value_loss
from rl.utils import layer_init
class PPOAgent(torch.nn.Module):
def __init__(self, envs: gym.vector.SyncVectorEnv, act_fun: str = "relu", ortho_init: bool = False) -> None:
super().__init__()
if act_fun.lower() == "relu":
act_fun = torch.nn.ReLU()
elif act_fun.lower() == "tanh":
act_fun = torch.nn.Tanh()
else:
raise ValueError("Unrecognized activation function: `act_fun` must be either `relu` or `tanh`")
self.critic = torch.nn.Sequential(
layer_init(
torch.nn.Linear(math.prod(envs.single_observation_space.shape), 64),
ortho_init=ortho_init,
),
act_fun,
layer_init(torch.nn.Linear(64, 64), ortho_init=ortho_init),
act_fun,
layer_init(torch.nn.Linear(64, 1), std=1.0, ortho_init=ortho_init),
)
self.actor = torch.nn.Sequential(
layer_init(
torch.nn.Linear(math.prod(envs.single_observation_space.shape), 64),
ortho_init=ortho_init,
),
act_fun,
layer_init(torch.nn.Linear(64, 64), ortho_init=ortho_init),
act_fun,
layer_init(torch.nn.Linear(64, envs.single_action_space.n), std=0.01, ortho_init=ortho_init),
)
def get_action(self, x: Tensor, action: Tensor = None) -> tuple[Tensor, Tensor, Tensor]:
logits = self.actor(x)
distribution = Categorical(logits=logits)
if action is None:
action = distribution.sample()
return action, distribution.log_prob(action), distribution.entropy()
def get_greedy_action(self, x: Tensor) -> Tensor:
logits = self.actor(x)
probs = F.softmax(logits, dim=-1)
return torch.argmax(probs, dim=-1)
def get_value(self, x: Tensor) -> Tensor:
return self.critic(x)
def get_action_and_value(self, x: Tensor, action: Tensor = None) -> tuple[Tensor, Tensor, Tensor, Tensor]:
action, log_prob, entropy = self.get_action(x, action)
value = self.get_value(x)
return action, log_prob, entropy, value
def forward(self, x: Tensor, action: Tensor = None) -> tuple[Tensor, Tensor, Tensor, Tensor]:
return self.get_action_and_value(x, action)
@torch.no_grad()
def estimate_returns_and_advantages(
self,
rewards: Tensor,
values: Tensor,
dones: Tensor,
next_obs: Tensor,
next_done: Tensor,
num_steps: int,
gamma: float,
gae_lambda: float,
) -> tuple[Tensor, Tensor]:
next_value = self.get_value(next_obs).reshape(1, -1)
advantages = torch.zeros_like(rewards)
lastgaelam = 0
for t in reversed(range(num_steps)):
if t == num_steps - 1:
nextnonterminal = torch.logical_not(next_done)
nextvalues = next_value
else:
nextnonterminal = torch.logical_not(dones[t + 1])
nextvalues = values[t + 1]
delta = rewards[t] + gamma * nextvalues * nextnonterminal - values[t]
advantages[t] = lastgaelam = delta + gamma * gae_lambda * nextnonterminal * lastgaelam
returns = advantages + values
return returns, advantages
class PPOLightningAgent(LightningModule):
def __init__(
self,
envs: gym.vector.SyncVectorEnv,
act_fun: str = "relu",
ortho_init: bool = False,
vf_coef: float = 1.0,
ent_coef: float = 0.0,
clip_coef: float = 0.2,
clip_vloss: bool = False,
normalize_advantages: bool = False,
**torchmetrics_kwargs,
):
super().__init__()
if act_fun.lower() == "relu":
act_fun = torch.nn.ReLU()
elif act_fun.lower() == "tanh":
act_fun = torch.nn.Tanh()
else:
raise ValueError("Unrecognized activation function: `act_fun` must be either `relu` or `tanh`")
self.vf_coef = vf_coef
self.ent_coef = ent_coef
self.clip_coef = clip_coef
self.clip_vloss = clip_vloss
self.normalize_advantages = normalize_advantages
self.critic = torch.nn.Sequential(
layer_init(
torch.nn.Linear(math.prod(envs.single_observation_space.shape), 64),
ortho_init=ortho_init,
),
act_fun,
layer_init(torch.nn.Linear(64, 64), ortho_init=ortho_init),
act_fun,
layer_init(torch.nn.Linear(64, 1), std=1.0, ortho_init=ortho_init),
)
self.actor = torch.nn.Sequential(
layer_init(
torch.nn.Linear(math.prod(envs.single_observation_space.shape), 64),
ortho_init=ortho_init,
),
act_fun,
layer_init(torch.nn.Linear(64, 64), ortho_init=ortho_init),
act_fun,
layer_init(torch.nn.Linear(64, envs.single_action_space.n), std=0.01, ortho_init=ortho_init),
)
self.avg_pg_loss = MeanMetric(**torchmetrics_kwargs)
self.avg_value_loss = MeanMetric(**torchmetrics_kwargs)
self.avg_ent_loss = MeanMetric(**torchmetrics_kwargs)
def get_action(self, x: Tensor, action: Tensor = None) -> tuple[Tensor, Tensor, Tensor]:
logits = self.actor(x)
distribution = Categorical(logits=logits)
if action is None:
action = distribution.sample()
return action, distribution.log_prob(action), distribution.entropy()
def get_greedy_action(self, x: Tensor) -> Tensor:
logits = self.actor(x)
probs = F.softmax(logits, dim=-1)
return torch.argmax(probs, dim=-1)
def get_value(self, x: Tensor) -> Tensor:
return self.critic(x)
def get_action_and_value(self, x: Tensor, action: Tensor = None) -> tuple[Tensor, Tensor, Tensor, Tensor]:
action, log_prob, entropy = self.get_action(x, action)
value = self.get_value(x)
return action, log_prob, entropy, value
def forward(self, x: Tensor, action: Tensor = None) -> tuple[Tensor, Tensor, Tensor, Tensor]:
return self.get_action_and_value(x, action)
@torch.no_grad()
def estimate_returns_and_advantages(
self,
rewards: Tensor,
values: Tensor,
dones: Tensor,
next_obs: Tensor,
next_done: Tensor,
num_steps: int,
gamma: float,
gae_lambda: float,
) -> tuple[Tensor, Tensor]:
next_value = self.get_value(next_obs).reshape(1, -1)
advantages = torch.zeros_like(rewards)
lastgaelam = 0
for t in reversed(range(num_steps)):
if t == num_steps - 1:
nextnonterminal = torch.logical_not(next_done)
nextvalues = next_value
else:
nextnonterminal = torch.logical_not(dones[t + 1])
nextvalues = values[t + 1]
delta = rewards[t] + gamma * nextvalues * nextnonterminal - values[t]
advantages[t] = lastgaelam = delta + gamma * gae_lambda * nextnonterminal * lastgaelam
returns = advantages + values
return returns, advantages
def training_step(self, batch: dict[str, Tensor]):
# Get actions and values given the current observations
_, newlogprob, entropy, newvalue = self(batch["obs"], batch["actions"].long())
logratio = newlogprob - batch["logprobs"]
ratio = logratio.exp()
# Policy loss
advantages = batch["advantages"]
if self.normalize_advantages:
advantages = (advantages - advantages.mean()) / (advantages.std() + 1e-8)
pg_loss = policy_loss(batch["advantages"], ratio, self.clip_coef)
# Value loss
v_loss = value_loss(
newvalue,
batch["values"],
batch["returns"],
self.clip_coef,
self.clip_vloss,
self.vf_coef,
)
# Entropy loss
ent_loss = entropy_loss(entropy, self.ent_coef)
# Update metrics
self.avg_pg_loss(pg_loss)
self.avg_value_loss(v_loss)
self.avg_ent_loss(ent_loss)
# Overall loss
return pg_loss + ent_loss + v_loss
def on_train_epoch_end(self, global_step: int) -> None:
# Log metrics and reset their internal state
self.logger.log_metrics(
{
"Loss/policy_loss": self.avg_pg_loss.compute(),
"Loss/value_loss": self.avg_value_loss.compute(),
"Loss/entropy_loss": self.avg_ent_loss.compute(),
},
global_step,
)
self.reset_metrics()
def reset_metrics(self):
self.avg_pg_loss.reset()
self.avg_value_loss.reset()
self.avg_ent_loss.reset()
def configure_optimizers(self, lr: float):
return torch.optim.Adam(self.parameters(), lr=lr, eps=1e-4)