-
Notifications
You must be signed in to change notification settings - Fork 12
/
TD7.py
327 lines (240 loc) · 9.58 KB
/
TD7.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
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
318
319
320
321
322
323
324
325
326
327
import copy
from dataclasses import dataclass
from typing import Callable
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import buffer
@dataclass
class Hyperparameters:
# Generic
batch_size: int = 256
buffer_size: int = 1e6
discount: float = 0.99
target_update_rate: int = 250
exploration_noise: float = 0.1
# TD3
target_policy_noise: float = 0.2
noise_clip: float = 0.5
policy_freq: int = 2
# LAP
alpha: float = 0.4
min_priority: float = 1
# TD3+BC
lmbda: float = 0.1
# Checkpointing
max_eps_when_checkpointing: int = 20
steps_before_checkpointing: int = 75e4
reset_weight: float = 0.9
# Encoder Model
zs_dim: int = 256
enc_hdim: int = 256
enc_activ: Callable = F.elu
encoder_lr: float = 3e-4
# Critic Model
critic_hdim: int = 256
critic_activ: Callable = F.elu
critic_lr: float = 3e-4
# Actor Model
actor_hdim: int = 256
actor_activ: Callable = F.relu
actor_lr: float = 3e-4
def AvgL1Norm(x, eps=1e-8):
return x/x.abs().mean(-1,keepdim=True).clamp(min=eps)
def LAP_huber(x, min_priority=1):
return torch.where(x < min_priority, 0.5 * x.pow(2), min_priority * x).sum(1).mean()
class Actor(nn.Module):
def __init__(self, state_dim, action_dim, zs_dim=256, hdim=256, activ=F.relu):
super(Actor, self).__init__()
self.activ = activ
self.l0 = nn.Linear(state_dim, hdim)
self.l1 = nn.Linear(zs_dim + hdim, hdim)
self.l2 = nn.Linear(hdim, hdim)
self.l3 = nn.Linear(hdim, action_dim)
def forward(self, state, zs):
a = AvgL1Norm(self.l0(state))
a = torch.cat([a, zs], 1)
a = self.activ(self.l1(a))
a = self.activ(self.l2(a))
return torch.tanh(self.l3(a))
class Encoder(nn.Module):
def __init__(self, state_dim, action_dim, zs_dim=256, hdim=256, activ=F.elu):
super(Encoder, self).__init__()
self.activ = activ
# state encoder
self.zs1 = nn.Linear(state_dim, hdim)
self.zs2 = nn.Linear(hdim, hdim)
self.zs3 = nn.Linear(hdim, zs_dim)
# state-action encoder
self.zsa1 = nn.Linear(zs_dim + action_dim, hdim)
self.zsa2 = nn.Linear(hdim, hdim)
self.zsa3 = nn.Linear(hdim, zs_dim)
def zs(self, state):
zs = self.activ(self.zs1(state))
zs = self.activ(self.zs2(zs))
zs = AvgL1Norm(self.zs3(zs))
return zs
def zsa(self, zs, action):
zsa = self.activ(self.zsa1(torch.cat([zs, action], 1)))
zsa = self.activ(self.zsa2(zsa))
zsa = self.zsa3(zsa)
return zsa
class Critic(nn.Module):
def __init__(self, state_dim, action_dim, zs_dim=256, hdim=256, activ=F.elu):
super(Critic, self).__init__()
self.activ = activ
self.q01 = nn.Linear(state_dim + action_dim, hdim)
self.q1 = nn.Linear(2*zs_dim + hdim, hdim)
self.q2 = nn.Linear(hdim, hdim)
self.q3 = nn.Linear(hdim, 1)
self.q02 = nn.Linear(state_dim + action_dim, hdim)
self.q4 = nn.Linear(2*zs_dim + hdim, hdim)
self.q5 = nn.Linear(hdim, hdim)
self.q6 = nn.Linear(hdim, 1)
def forward(self, state, action, zsa, zs):
sa = torch.cat([state, action], 1)
embeddings = torch.cat([zsa, zs], 1)
q1 = AvgL1Norm(self.q01(sa))
q1 = torch.cat([q1, embeddings], 1)
q1 = self.activ(self.q1(q1))
q1 = self.activ(self.q2(q1))
q1 = self.q3(q1)
q2 = AvgL1Norm(self.q02(sa))
q2 = torch.cat([q2, embeddings], 1)
q2 = self.activ(self.q4(q2))
q2 = self.activ(self.q5(q2))
q2 = self.q6(q2)
return torch.cat([q1, q2], 1)
class Agent(object):
def __init__(self, state_dim, action_dim, max_action, offline=False, hp=Hyperparameters()):
# Changing hyperparameters example: hp=Hyperparameters(batch_size=128)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.hp = hp
self.actor = Actor(state_dim, action_dim, hp.zs_dim, hp.actor_hdim, hp.actor_activ).to(self.device)
self.actor_optimizer = torch.optim.Adam(self.actor.parameters(), lr=hp.actor_lr)
self.actor_target = copy.deepcopy(self.actor)
self.critic = Critic(state_dim, action_dim, hp.zs_dim, hp.critic_hdim, hp.critic_activ).to(self.device)
self.critic_optimizer = torch.optim.Adam(self.critic.parameters(), lr=hp.critic_lr)
self.critic_target = copy.deepcopy(self.critic)
self.encoder = Encoder(state_dim, action_dim, hp.zs_dim, hp.enc_hdim, hp.enc_activ).to(self.device)
self.encoder_optimizer = torch.optim.Adam(self.encoder.parameters(), lr=hp.encoder_lr)
self.fixed_encoder = copy.deepcopy(self.encoder)
self.fixed_encoder_target = copy.deepcopy(self.encoder)
self.checkpoint_actor = copy.deepcopy(self.actor)
self.checkpoint_encoder = copy.deepcopy(self.encoder)
self.replay_buffer = buffer.LAP(state_dim, action_dim, self.device, hp.buffer_size, hp.batch_size,
max_action, normalize_actions=True, prioritized=True)
self.max_action = max_action
self.offline = offline
self.training_steps = 0
# Checkpointing tracked values
self.eps_since_update = 0
self.timesteps_since_update = 0
self.max_eps_before_update = 1
self.min_return = 1e8
self.best_min_return = -1e8
# Value clipping tracked values
self.max = -1e8
self.min = 1e8
self.max_target = 0
self.min_target = 0
def select_action(self, state, use_checkpoint=False, use_exploration=True):
with torch.no_grad():
state = torch.tensor(state.reshape(1,-1), dtype=torch.float, device=self.device)
if use_checkpoint:
zs = self.checkpoint_encoder.zs(state)
action = self.checkpoint_actor(state, zs)
else:
zs = self.fixed_encoder.zs(state)
action = self.actor(state, zs)
if use_exploration:
action = action + torch.randn_like(action) * self.hp.exploration_noise
return action.clamp(-1,1).cpu().data.numpy().flatten() * self.max_action
def train(self):
self.training_steps += 1
state, action, next_state, reward, not_done = self.replay_buffer.sample()
#########################
# Update Encoder
#########################
with torch.no_grad():
next_zs = self.encoder.zs(next_state)
zs = self.encoder.zs(state)
pred_zs = self.encoder.zsa(zs, action)
encoder_loss = F.mse_loss(pred_zs, next_zs)
self.encoder_optimizer.zero_grad()
encoder_loss.backward()
self.encoder_optimizer.step()
#########################
# Update Critic
#########################
with torch.no_grad():
fixed_target_zs = self.fixed_encoder_target.zs(next_state)
noise = (torch.randn_like(action) * self.hp.target_policy_noise).clamp(-self.hp.noise_clip, self.hp.noise_clip)
next_action = (self.actor_target(next_state, fixed_target_zs) + noise).clamp(-1,1)
fixed_target_zsa = self.fixed_encoder_target.zsa(fixed_target_zs, next_action)
Q_target = self.critic_target(next_state, next_action, fixed_target_zsa, fixed_target_zs).min(1,keepdim=True)[0]
Q_target = reward + not_done * self.hp.discount * Q_target.clamp(self.min_target, self.max_target)
self.max = max(self.max, float(Q_target.max()))
self.min = min(self.min, float(Q_target.min()))
fixed_zs = self.fixed_encoder.zs(state)
fixed_zsa = self.fixed_encoder.zsa(fixed_zs, action)
Q = self.critic(state, action, fixed_zsa, fixed_zs)
td_loss = (Q - Q_target).abs()
critic_loss = LAP_huber(td_loss)
self.critic_optimizer.zero_grad()
critic_loss.backward()
self.critic_optimizer.step()
#########################
# Update LAP
#########################
priority = td_loss.max(1)[0].clamp(min=self.hp.min_priority).pow(self.hp.alpha)
self.replay_buffer.update_priority(priority)
#########################
# Update Actor
#########################
if self.training_steps % self.hp.policy_freq == 0:
actor = self.actor(state, fixed_zs)
fixed_zsa = self.fixed_encoder.zsa(fixed_zs, actor)
Q = self.critic(state, actor, fixed_zsa, fixed_zs)
actor_loss = -Q.mean()
if self.offline:
actor_loss = actor_loss + self.hp.lmbda * Q.abs().mean().detach() * F.mse_loss(actor, action)
self.actor_optimizer.zero_grad()
actor_loss.backward()
self.actor_optimizer.step()
#########################
# Update Iteration
#########################
if self.training_steps % self.hp.target_update_rate == 0:
self.actor_target.load_state_dict(self.actor.state_dict())
self.critic_target.load_state_dict(self.critic.state_dict())
self.fixed_encoder_target.load_state_dict(self.fixed_encoder.state_dict())
self.fixed_encoder.load_state_dict(self.encoder.state_dict())
self.replay_buffer.reset_max_priority()
self.max_target = self.max
self.min_target = self.min
# If using checkpoints: run when each episode terminates
def maybe_train_and_checkpoint(self, ep_timesteps, ep_return):
self.eps_since_update += 1
self.timesteps_since_update += ep_timesteps
self.min_return = min(self.min_return, ep_return)
# End evaluation of current policy early
if self.min_return < self.best_min_return:
self.train_and_reset()
# Update checkpoint
elif self.eps_since_update == self.max_eps_before_update:
self.best_min_return = self.min_return
self.checkpoint_actor.load_state_dict(self.actor.state_dict())
self.checkpoint_encoder.load_state_dict(self.fixed_encoder.state_dict())
self.train_and_reset()
# Batch training
def train_and_reset(self):
for _ in range(self.timesteps_since_update):
if self.training_steps == self.hp.steps_before_checkpointing:
self.best_min_return *= self.hp.reset_weight
self.max_eps_before_update = self.hp.max_eps_when_checkpointing
self.train()
self.eps_since_update = 0
self.timesteps_since_update = 0
self.min_return = 1e8