forked from TJU-DRL-LAB/AI-Optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuwac_impl.py
380 lines (321 loc) · 13.2 KB
/
uwac_impl.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import math
from typing import Optional, Sequence
import numpy as np
import torch
from torch.optim import Optimizer
from d3rlpy.gpu import Device
from d3rlpy.models.builders import create_conditional_vae, create_parameter
from d3rlpy.models.encoders import EncoderFactory
from d3rlpy.models.optimizers import OptimizerFactory
from d3rlpy.models.q_functions import QFunctionFactory
from d3rlpy.models.torch import (
ConditionalVAE,
Parameter,
compute_max_with_n_actions_and_indices,
compute_max_with_n_actions_and_indices_and_vars,
)
from d3rlpy.preprocessing import ActionScaler, RewardScaler, Scaler
from d3rlpy.torch_utility import TorchMiniBatch, torch_api, train_api
from d3rlpy.algos.torch.sac_impl import SACImpl
def _gaussian_kernel(
x: torch.Tensor, y: torch.Tensor, sigma: float
) -> torch.Tensor:
# x: (batch, n, 1, action), y: (batch, 1, n, action) -> (batch, n, n)
return (-((x - y) ** 2).sum(dim=3) / (2 * sigma)).exp()
def _laplacian_kernel(
x: torch.Tensor, y: torch.Tensor, sigma: float
) -> torch.Tensor:
# x: (batch, n, 1, action), y: (batch, 1, n, action) -> (batch, n, n)
return (-(x - y).abs().sum(dim=3) / (2 * sigma)).exp()
class UWACImpl(SACImpl):
_imitator_learning_rate: float
_alpha_learning_rate: float
_imitator_optim_factory: OptimizerFactory
_alpha_optim_factory: OptimizerFactory
_imitator_encoder_factory: EncoderFactory
_initial_alpha: float
_alpha_threshold: float
_lam: float
_n_action_samples: int
_n_target_samples: int
_n_mmd_action_samples: int
_mmd_kernel: str
_mmd_sigma: float
_vae_kl_weight: float
_imitator: Optional[ConditionalVAE]
_imitator_optim: Optional[Optimizer]
_log_alpha: Optional[Parameter]
_alpha_optim: Optional[Optimizer]
def __init__(
self,
observation_shape: Sequence[int],
action_size: int,
actor_learning_rate: float,
critic_learning_rate: float,
imitator_learning_rate: float,
temp_learning_rate: float,
alpha_learning_rate: float,
actor_optim_factory: OptimizerFactory,
critic_optim_factory: OptimizerFactory,
imitator_optim_factory: OptimizerFactory,
temp_optim_factory: OptimizerFactory,
alpha_optim_factory: OptimizerFactory,
actor_encoder_factory: EncoderFactory,
critic_encoder_factory: EncoderFactory,
imitator_encoder_factory: EncoderFactory,
q_func_factory: QFunctionFactory,
gamma: float,
tau: float,
n_critics: int,
initial_temperature: float,
initial_alpha: float,
alpha_threshold: float,
lam: float,
n_action_samples: int,
n_target_samples: int,
n_mmd_action_samples: int,
mmd_kernel: str,
mmd_sigma: float,
vae_kl_weight: float,
use_gpu: Optional[Device],
scaler: Optional[Scaler],
action_scaler: Optional[ActionScaler],
reward_scaler: Optional[RewardScaler],
beta,
clip_bottom,
clip_top,
use_exp_weight,
var_Pi,
q_penalty,
use_exp_penalty,
dropout,
):
super().__init__(
observation_shape=observation_shape,
action_size=action_size,
actor_learning_rate=actor_learning_rate,
critic_learning_rate=critic_learning_rate,
temp_learning_rate=temp_learning_rate,
actor_optim_factory=actor_optim_factory,
critic_optim_factory=critic_optim_factory,
temp_optim_factory=temp_optim_factory,
actor_encoder_factory=actor_encoder_factory,
critic_encoder_factory=critic_encoder_factory,
q_func_factory=q_func_factory,
gamma=gamma,
tau=tau,
n_critics=n_critics,
initial_temperature=initial_temperature,
use_gpu=use_gpu,
scaler=scaler,
action_scaler=action_scaler,
reward_scaler=reward_scaler,
)
self._imitator_learning_rate = imitator_learning_rate
self._alpha_learning_rate = alpha_learning_rate
self._imitator_optim_factory = imitator_optim_factory
self._alpha_optim_factory = alpha_optim_factory
self._imitator_encoder_factory = imitator_encoder_factory
self._initial_alpha = initial_alpha
self._alpha_threshold = alpha_threshold
self._lam = lam
self._n_action_samples = n_action_samples
self._n_target_samples = n_target_samples
self._n_mmd_action_samples = n_mmd_action_samples
self._mmd_kernel = mmd_kernel
self._mmd_sigma = mmd_sigma
self._vae_kl_weight = vae_kl_weight
# initialized in build
self._imitator = None
self._imitator_optim = None
self._log_alpha = None
self._alpha_optim = None
# UWAC related
self.beta = beta
self.var_Pi = var_Pi
self.use_exp_weight = use_exp_weight
self.clip_top = clip_top
self.clip_bottom = clip_bottom
self.q_penalty = q_penalty
self.use_exp_penalty = use_exp_penalty
self.dropout = dropout
def build(self) -> None:
self._build_imitator()
self._build_alpha()
super().build()
self._build_imitator_optim()
self._build_alpha_optim()
def _build_imitator(self) -> None:
self._imitator = create_conditional_vae(
observation_shape=self._observation_shape,
action_size=self._action_size,
latent_size=2 * self._action_size,
beta=self._vae_kl_weight,
min_logstd=-4.0,
max_logstd=15.0,
encoder_factory=self._imitator_encoder_factory,
)
def _build_imitator_optim(self) -> None:
assert self._imitator is not None
self._imitator_optim = self._imitator_optim_factory.create(
self._imitator.parameters(), lr=self._imitator_learning_rate
)
def _build_alpha(self) -> None:
initial_val = math.log(self._initial_alpha)
self._log_alpha = create_parameter((1, 1), initial_val)
def _build_alpha_optim(self) -> None:
assert self._log_alpha is not None
self._alpha_optim = self._alpha_optim_factory.create(
self._log_alpha.parameters(), lr=self._alpha_learning_rate
)
def _get_weight(self, var, LAMBDA, factor=1):
if self.use_exp_weight:
weight = torch.clamp(torch.exp(-LAMBDA * var/factor), self.clip_bottom, self.clip_top)
else:
weight = torch.clamp(LAMBDA*factor/var, self.clip_bottom, self.clip_top)
return weight
def compute_weighted_sac_actor_loss(self, batch: TorchMiniBatch) -> torch.Tensor:
assert self._policy is not None
assert self._log_temp is not None
assert self._q_func is not None
action, log_prob = self._policy.sample_with_log_prob(batch.observations)
entropy = self._log_temp().exp() * log_prob
if self.dropout:
pass
else:
q_t_min = self._q_func(batch.observations, action, "min")
q_t_max = self._q_func(batch.observations, action, "max")
q_t_var = torch.var(q_t_min, q_t_max)
print("@UWACIMPL #208", q_t_min.shape, q_t_var.shape)
if self.var_Pi:
weight = self._get_weight(q_t_var, self.beta)
else:
weight = 1
return (entropy - weight.detach()*q_t_min).mean()
def compute_actor_loss(self, batch: TorchMiniBatch) -> torch.Tensor:
loss = self.compute_weighted_sac_actor_loss(batch)
mmd_loss = self._compute_mmd_loss(batch.observations)
return loss + mmd_loss
@train_api
@torch_api()
def warmup_actor(self, batch: TorchMiniBatch) -> np.ndarray:
assert self._actor_optim is not None
self._actor_optim.zero_grad()
loss = self._compute_mmd_loss(batch.observations)
loss.backward()
self._actor_optim.step()
return loss.cpu().detach().numpy()
def _compute_mmd_loss(self, obs_t: torch.Tensor) -> torch.Tensor:
assert self._log_alpha
mmd = self._compute_mmd(obs_t)
alpha = self._log_alpha().exp()
return (alpha * (mmd - self._alpha_threshold)).mean()
@train_api
@torch_api()
def update_imitator(self, batch: TorchMiniBatch) -> np.ndarray:
assert self._imitator_optim is not None
self._imitator_optim.zero_grad()
loss = self.compute_imitator_loss(batch)
loss.backward()
self._imitator_optim.step()
return loss.cpu().detach().numpy()
def compute_imitator_loss(self, batch: TorchMiniBatch) -> torch.Tensor:
assert self._imitator is not None
return self._imitator.compute_error(batch.observations, batch.actions)
@train_api
@torch_api()
def update_alpha(self, batch: TorchMiniBatch) -> np.ndarray:
assert self._alpha_optim is not None
assert self._log_alpha is not None
loss = -self._compute_mmd_loss(batch.observations)
self._alpha_optim.zero_grad()
loss.backward()
self._alpha_optim.step()
# clip for stability
self._log_alpha.data.clamp_(-5.0, 10.0)
cur_alpha = self._log_alpha().exp().cpu().detach().numpy()[0][0]
return loss.cpu().detach().numpy(), cur_alpha
def _compute_mmd(self, x: torch.Tensor) -> torch.Tensor:
assert self._imitator is not None
assert self._policy is not None
with torch.no_grad():
behavior_actions = self._imitator.sample_n_without_squash(
x, self._n_mmd_action_samples
)
policy_actions = self._policy.sample_n_without_squash(
x, self._n_mmd_action_samples
)
if self._mmd_kernel == "gaussian":
kernel = _gaussian_kernel
elif self._mmd_kernel == "laplacian":
kernel = _laplacian_kernel
else:
raise ValueError(f"Invalid kernel type: {self._mmd_kernel}")
# (batch, n, action) -> (batch, n, 1, action)
behavior_actions = behavior_actions.reshape(
x.shape[0], -1, 1, self.action_size
)
policy_actions = policy_actions.reshape(
x.shape[0], -1, 1, self.action_size
)
# (batch, n, action) -> (batch, 1, n, action)
behavior_actions_T = behavior_actions.reshape(
x.shape[0], 1, -1, self.action_size
)
policy_actions_T = policy_actions.reshape(
x.shape[0], 1, -1, self.action_size
)
# 1 / N^2 \sum k(a_\pi, a_\pi)
inter_policy = kernel(policy_actions, policy_actions_T, self._mmd_sigma)
mmd = inter_policy.mean(dim=[1, 2])
# 1 / N^2 \sum k(a_\beta, a_\beta)
inter_data = kernel(
behavior_actions, behavior_actions_T, self._mmd_sigma
)
mmd += inter_data.mean(dim=[1, 2])
# 2 / N^2 \sum k(a_\pi, a_\beta)
distance = kernel(policy_actions, behavior_actions_T, self._mmd_sigma)
mmd -= 2 * distance.mean(dim=[1, 2])
return (mmd + 1e-6).sqrt().view(-1, 1)
def compute_target_and_vars(self, batch: TorchMiniBatch) -> torch.Tensor:
assert self._policy is not None
assert self._targ_q_func is not None
assert self._log_temp is not None
with torch.no_grad():
# BCQ-like target computation
actions, log_probs = self._policy.sample_n_with_log_prob(
batch.next_observations,
self._n_target_samples,
)
values, indices, values_vars = compute_max_with_n_actions_and_indices_and_vars(
batch.next_observations, actions, self._targ_q_func, self._lam
)
# (batch, n, 1) -> (batch, 1)
batch_size = batch.observations.shape[0]
max_log_prob = log_probs[torch.arange(batch_size), indices]
return values - self._log_temp().exp() * max_log_prob, values_vars
def compute_target(self, batch: TorchMiniBatch) -> torch.Tensor:
return self.compute_target_and_vars(batch)[0]
def _predict_best_action(self, x: torch.Tensor) -> torch.Tensor:
assert self._policy is not None
assert self._q_func is not None
with torch.no_grad():
# (batch, n, action)
actions = self._policy.onnx_safe_sample_n(x, self._n_action_samples)
# (batch, n, action) -> (batch * n, action)
flat_actions = actions.reshape(-1, self._action_size)
# (batch, observation) -> (batch, 1, observation)
expanded_x = x.view(x.shape[0], 1, *x.shape[1:])
# (batch, 1, observation) -> (batch, n, observation)
repeated_x = expanded_x.expand(
x.shape[0], self._n_action_samples, *x.shape[1:]
)
# (batch, n, observation) -> (batch * n, observation)
flat_x = repeated_x.reshape(-1, *x.shape[1:])
# (batch * n, 1)
flat_values = self._q_func(flat_x, flat_actions, "none")[0]
# (batch, n)
values = flat_values.view(x.shape[0], self._n_action_samples)
# (batch, n) -> (batch,)
max_indices = torch.argmax(values, dim=1)
return actions[torch.arange(x.shape[0]), max_indices]