This repository was archived by the owner on Apr 13, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdynamics.py
More file actions
404 lines (353 loc) · 15.9 KB
/
Copy pathdynamics.py
File metadata and controls
404 lines (353 loc) · 15.9 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
"""
The implementation of dynamics model is based on MOPO
"""
from typing import Union, List
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import itertools
from modules import Module, Swish
from logger import Logger
class Normalizer(nn.Module):
def __init__(self, dim: int) -> None:
super().__init__()
self.dim = dim
self.register_buffer('mean', torch.zeros(dim))
self.register_buffer('std', torch.zeros(dim))
def fit(self, X: np.array) -> None:
assert len(X.shape) == 2
assert X.shape[1] == self.dim
device = self.mean.device
self.mean.data.copy_(
torch.from_numpy(np.mean(X, axis=0, keepdims=False)).float().to(device)
)
self.std.data.copy_(
torch.from_numpy(np.std(X, axis=0, keepdims=False)).float().to(device)
)
self.std[self.std < 1e-12] = 1.0
def transform(self, x: Union[np.array, torch.tensor]) -> Union[np.array, torch.tensor]:
if isinstance(x, np.ndarray):
device = self.mean.device
x = torch.from_numpy(x).float().to(device)
return ((x - self.mean) / self.std).cpu().numpy()
elif isinstance(x, torch.Tensor):
return ((x - self.mean) / self.std)
def init_weights(m):
def truncated_normal_init(
t: nn.Module,
mean: float = 0.0,
std: float = 0.01
):
torch.nn.init.normal_(t, mean=mean, std=std)
while True:
cond = torch.logical_or(t < mean - 2 * std, t > mean + 2 * std)
if not torch.sum(cond):
break
t = torch.where(cond, torch.nn.init.normal_(torch.ones(t.shape), mean=mean, std=std), t)
return t
if type(m) == nn.Linear or isinstance(m, EnsembleFC):
input_dim = m.in_features
truncated_normal_init(m.weight, std=1 / (2 * np.sqrt(input_dim)))
m.bias.data.fill_(0.0)
class EnsembleFC(nn.Module):
__constants__ = ['in_features', 'out_features']
in_features: int
out_features: int
ensemble_size: int
weight: torch.Tensor
def __init__(self, in_features: int, out_features: int, ensemble_size: int, weight_decay: float = 0., bias: bool = True) -> None:
super(EnsembleFC, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.ensemble_size = ensemble_size
self.weight = nn.Parameter(torch.Tensor(ensemble_size, in_features, out_features))
self.weight_decay = weight_decay
if bias:
self.bias = nn.Parameter(torch.Tensor(ensemble_size, out_features))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self) -> None:
pass
def forward(self, input: torch.Tensor) -> torch.Tensor:
w_times_x = torch.bmm(input, self.weight)
return torch.add(w_times_x, self.bias[:, None, :]) # w times x + b
def extra_repr(self) -> str:
return 'in_features={}, out_features={}, bias={}'.format(
self.in_features, self.out_features, self.bias is not None
)
class EnsembleModel(nn.Module):
def __init__(
self,
state_size: int,
action_size: int,
reward_size: int,
ensemble_size: int,
hidden_size: int = 200,
learning_rate: float = 1e-3,
use_decay: bool = False,
device: str = 'cuda'
):
super(EnsembleModel, self).__init__()
self.device = device
self.hidden_size = hidden_size
self.output_dim = state_size + reward_size
# trunk layers
self.nn1 = EnsembleFC(state_size + action_size, hidden_size, ensemble_size, weight_decay=0.000025)
self.nn2 = EnsembleFC(hidden_size, hidden_size, ensemble_size, weight_decay=0.00005)
self.nn3 = EnsembleFC(hidden_size, hidden_size, ensemble_size, weight_decay=0.000075)
self.nn4 = EnsembleFC(hidden_size, hidden_size, ensemble_size, weight_decay=0.000075)
self.use_decay = use_decay
# Add variance output
self.nn5 = EnsembleFC(hidden_size, self.output_dim * 2, ensemble_size, weight_decay=0.0001)
# min / max log var bounds
self.max_logvar = nn.Parameter((torch.ones((1, self.output_dim)).float() / 2).to(device), requires_grad=False)
self.min_logvar = nn.Parameter((-torch.ones((1, self.output_dim)).float() * 10).to(device), requires_grad=False)
# optimizer
self.optimizer = torch.optim.Adam(self.parameters(), lr=learning_rate)
# weight init
self.apply(init_weights)
self.swish = Swish()
def forward(
self,
x: torch.tensor,
ret_log_var: bool = False
):
nn1_output = self.swish(self.nn1(x))
nn2_output = self.swish(self.nn2(nn1_output))
nn3_output = self.swish(self.nn3(nn2_output))
nn4_output = self.swish(self.nn4(nn3_output))
nn5_output = self.nn5(nn4_output)
mean = nn5_output[:, :, :self.output_dim]
logvar = nn5_output[:, :, self.output_dim:]
logvar = self.max_logvar - F.softplus(self.max_logvar - logvar)
logvar = self.min_logvar + F.softplus(logvar - self.min_logvar)
if ret_log_var:
return mean, logvar
else:
return mean, torch.exp(logvar)
def get_decay_loss(self):
decay_loss = 0.
for m in self.children():
if isinstance(m, EnsembleFC):
decay_loss += m.weight_decay * torch.sum(torch.square(m.weight)) / 2.
return decay_loss
def loss(
self,
mean: torch.tensor,
logvar: torch.tensor,
labels: torch.tensor,
inc_var_loss: bool = True
):
"""
mean, logvar: [ensemble_size, batch_size, |S| + |A|]
labels: [ensemble_size, batch_size, |S| + 1]
"""
assert len(mean.shape) == len(logvar.shape) == len(labels.shape) == 3
inv_var = torch.exp(-logvar)
# Average over batch and dim, sum over ensembles.
if inc_var_loss:
mse_loss = torch.mean(torch.mean(torch.pow(mean - labels, 2) * inv_var, dim=-1), dim=-1)
var_loss = torch.mean(torch.mean(logvar, dim=-1), dim=-1)
total_loss = torch.sum(mse_loss) + torch.sum(var_loss)
else:
mse_loss = torch.mean(torch.pow(mean - labels, 2), dim=(1, 2))
total_loss = torch.sum(mse_loss)
return total_loss, mse_loss
def train(self, loss):
self.optimizer.zero_grad()
loss += 0.01 * torch.sum(self.max_logvar) - 0.01 * torch.sum(self.min_logvar)
if self.use_decay:
loss += self.get_decay_loss()
loss.backward()
self.optimizer.step()
class EnsembleDynamicsModel(Module):
def __init__(
self,
network_size: int,
elite_size: int,
state_size: int,
action_size: int,
reward_size: int = 1,
hidden_size: int = 200,
use_decay: bool= False,
device: str = 'cuda',
):
super(EnsembleDynamicsModel, self).__init__()
self.network_size = network_size
self.elite_size = elite_size
self.model_list = []
self.state_size = state_size
self.action_size = action_size
self.reward_size = reward_size
self.network_size = network_size
self.elite_model_idxes = []
self.ensemble_model = EnsembleModel(state_size, action_size, reward_size, network_size, hidden_size, use_decay=use_decay, device=device)
self.scaler = Normalizer(dim=state_size + action_size)
self.device = device
def train(
self,
inputs: np.array,
labels: np.array,
batch_size: int = 256,
holdout_ratio: float = 0.,
max_epochs_since_update: int = 5,
logger: Logger = None,
):
self._max_epochs_since_update = max_epochs_since_update
self._epochs_since_update = 0
self._state = {}
self._snapshots = {i: (None, 1e10) for i in range(self.network_size)}
num_holdout = int(inputs.shape[0] * holdout_ratio)
permutation = np.random.permutation(inputs.shape[0])
inputs, labels = inputs[permutation], labels[permutation]
train_inputs, train_labels = inputs[num_holdout:], labels[num_holdout:]
holdout_inputs, holdout_labels = inputs[:num_holdout], labels[:num_holdout]
self.scaler.fit(train_inputs)
train_inputs = self.scaler.transform(train_inputs)
holdout_inputs = self.scaler.transform(holdout_inputs)
holdout_inputs = torch.from_numpy(holdout_inputs).float().to(self.device)
holdout_labels = torch.from_numpy(holdout_labels).float().to(self.device)
holdout_inputs = holdout_inputs[None, :, :].repeat([self.network_size, 1, 1])
holdout_labels = holdout_labels[None, :, :].repeat([self.network_size, 1, 1])
# for log
all_holdout_losses = []
step = 0
for epoch in itertools.count():
train_idx = np.vstack([np.random.permutation(train_inputs.shape[0]) for _ in range(self.network_size)])
for start_pos in range(0, train_inputs.shape[0], batch_size):
idx = train_idx[:, start_pos: min(start_pos + batch_size, train_inputs.shape[0])]
train_input = torch.from_numpy(train_inputs[idx]).float().to(self.device)
train_label = torch.from_numpy(train_labels[idx]).float().to(self.device)
losses = []
mean, logvar = self.ensemble_model(train_input, ret_log_var=True)
loss, _ = self.ensemble_model.loss(mean, logvar, train_label)
self.ensemble_model.train(loss)
losses.append(loss)
# print(loss.item())
if logger is not None:
logger.log_dict({"dynamics_loss": loss.item()}, step=step)
step += 1
with torch.no_grad():
holdout_mean, holdout_logvar = self.ensemble_model(holdout_inputs, ret_log_var = True)
_, holdout_mse_losses = self.ensemble_model.loss(holdout_mean, holdout_logvar, holdout_labels, inc_var_loss=False)
holdout_mse_losses = holdout_mse_losses.detach().cpu().numpy()
all_holdout_losses.append(np.mean(holdout_mse_losses))
# rank and select the elite models
sorted_loss_idx = np.argsort(holdout_mse_losses)
self.elite_model_idxes = sorted_loss_idx[:self.elite_size].tolist()
# check if any model improves
break_train = self._save_best(epoch, holdout_mse_losses)
if break_train:
break
self._track_head_loss(all_holdout_losses)
def _save_best(
self,
epoch: int,
holdout_losses: torch.tensor # [ensemble_size]
):
updated = False
for i in range(len(holdout_losses)):
current = holdout_losses[i]
_, best = self._snapshots[i]
improvement = (best - current) / best
if improvement > 0.01:
self._snapshots[i] = (epoch, current)
updated = True
if updated:
self._epochs_since_update = 0
else:
self._epochs_since_update += 1
if self._epochs_since_update > self._max_epochs_since_update:
return True
else:
return False
def _track_head_loss(
self,
holdout_losses: List # [<= max_train_epoch]
) -> None:
self._current_mean_ensemble_losses = np.mean(holdout_losses)
def predict(
self,
inputs: torch.Tensor,
batch_size: float = 1024,
factor_ensemble: bool = True
):
if inputs.ndim == 2:
B = inputs.shape[0]
inputs = self.scaler.transform(inputs)
ensemble_mean, ensemble_var = [], []
for i in range(0, B, batch_size):
input = inputs[i:min(i + batch_size, B)]
b_mean, b_var = self.ensemble_model(
input[None, :, :].repeat([self.network_size, 1, 1]),
ret_log_var=False
)
ensemble_mean.append(b_mean)
ensemble_var.append(b_var)
ensemble_mean = torch.concat(ensemble_mean, 1) # concat along the batch_size axis
ensemble_var = torch.concat(ensemble_var, 1)
if factor_ensemble:
return ensemble_mean, ensemble_var # [ensemble_size, batch_size, |S|+1]
else:
mean = torch.mean(ensemble_mean, dim=0)
var = torch.mean(ensemble_var, dim=0) + torch.mean(torch.square(ensemble_mean - mean[None, :, :]), dim=0)
return mean, var
elif inputs.ndim == 3:
assert inputs.shape[0] == self.network_size
B = inputs.shape[1]
inputs = self.scaler.transform(inputs)
ensemble_mean, ensemble_var = [], []
for i in range(0, B, batch_size):
input = inputs[:, i:min(i + batch_size, B), :]
b_mean, b_var = self.ensemble_model(
input,
ret_log_var=False
)
ensemble_mean.append(b_mean)
ensemble_var.append(b_var)
ensemble_mean = torch.concat(ensemble_mean, 1) # concat along the batch_size axis
ensemble_var = torch.concat(ensemble_var, 1)
if factor_ensemble:
return ensemble_mean, ensemble_var # [ensemble_size, batch_size, |S|+1]
else:
mean = torch.mean(ensemble_mean, dim=0)
var = torch.mean(ensemble_var, dim=0) + torch.mean(torch.square(ensemble_mean - mean[None, :, :]), dim=0)
return mean, var
else:
raise ValueError
@torch.no_grad()
def compute_disc(self, inputs: torch.Tensor, uncertainty_mode="pairwise-diff"):
mean, var = self.predict(inputs=inputs, factor_ensemble=True)
if uncertainty_mode == "pairwise-diff":
next_obses_mode = mean[:, :, :-1]
next_obs_mean = torch.mean(next_obses_mode, dim=0)
diff = next_obses_mode - next_obs_mean
disc = torch.max(torch.norm(diff, dim=-1), dim=0)[0]
return disc
def compute_max_disc(self, buffer):
s_batch, a_batch, next_s_batch, r_batch, done_batch = buffer.sample_all()
max_disc = -100
min_batch = 1000
slice_num = len(s_batch) // min_batch
alone_num = len(s_batch) % min_batch
for i in range(slice_num):
s = s_batch[i*min_batch:(i+1)*min_batch]
a = a_batch[i*min_batch:(i+1)*min_batch]
s = torch.tensor(s).float().to(self.device)
a = torch.tensor(a).float().to(self.device)
sa = torch.cat([s, a], dim=-1)
mini_batch_max_disc = torch.max(self.compute_disc(sa))
if mini_batch_max_disc > max_disc:
max_disc = mini_batch_max_disc
if alone_num > 0:
s = s_batch[slice_num*min_batch:]
a = a_batch[slice_num*min_batch:]
s = torch.tensor(s).float().to(self.device)
a = torch.tensor(a).float().to(self.device)
sa = torch.cat([s, a], dim=-1)
mini_batch_max_disc = torch.max(self.compute_disc(sa))
if mini_batch_max_disc > max_disc:
max_disc = mini_batch_max_disc
return max_disc