-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.py
697 lines (557 loc) · 24.3 KB
/
timer.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
from tqdm import tqdm
import torch
from torch.autograd import Function, Variable
import torch.nn.functional as F
from torch import nn
from torch.nn.parameter import Parameter
from torch import optim
from torch.nn.utils import parameters_to_vector
from torch.utils.data import TensorDataset, DataLoader
from mpc import mpc
from mpc.mpc import GradMethods, QuadCost, LinDx
from mpc.dynamics import NNDynamics
import mpc.util as eutil
from mpc.env_dx import pendulum, cartpole
import numpy as np
import numpy.random as npr
import argparse
import os
import sys
import shutil
import time
import re
import pickle as pkl
from il_env import IL_Env
import time
class ReflexNet(nn.Module):
def __init__(self, n_state, n_ctrl, h_dim, T=20):
super(ReflexNet, self).__init__()
self.n_state = n_state
self.n_ctrl = n_ctrl
self.T = T
self.net = nn.Sequential(
nn.Linear(n_state, h_dim),
nn.ReLU(),
nn.Linear(h_dim, h_dim),
nn.ReLU(),
nn.Linear(h_dim, h_dim*2),
nn.ReLU(),
nn.Linear(h_dim*2, h_dim*4),
nn.ReLU(),
nn.Linear(h_dim*4, T*n_ctrl),
nn.Tanh()
)
def forward(self, xinits, scalar=2.):
return (self.net(xinits) * scalar)
class RNN(nn.Module):
def __init__(self, n_state, n_ctrl, h_dim, T=20):
super(RNN, self).__init__()
self.n_state = n_state
self.n_ctrl = n_ctrl
self.T = T
self.state_emb = nn.Sequential(
nn.Linear(n_state, h_dim),
nn.ReLU(),
nn.Linear(h_dim, h_dim),
nn.ReLU(),
nn.Linear(h_dim, h_dim),
)
self.ctrl_emb = nn.Sequential(
nn.Linear(n_ctrl, h_dim),
nn.ReLU(),
nn.Linear(h_dim, h_dim),
nn.ReLU(),
nn.Linear(h_dim, h_dim),
)
self.decode = nn.Sequential(
nn.Linear(h_dim, h_dim),
nn.ReLU(),
nn.Linear(h_dim, h_dim),
nn.ReLU(),
nn.Linear(h_dim, n_ctrl),
nn.Tanh()
)
self.cell = nn.LSTMCell(h_dim, h_dim)
def forward(self, xinits, scalar=2.):
yt = self.state_emb(xinits)
cell_state = None
uts = []
for t in range(self.T):
cell_state = self.cell(yt, cell_state)
ht, ct = cell_state
# ut = self.decode(ct)
ut = self.decode(ct) * scalar
uts.append(ut)
yt = self.ctrl_emb(ut)
uts = torch.stack(uts, dim=1)
return uts
class KoopmanNet(nn.Module):
def __init__(self, dt, n_state, n_ctrl, h_dim, z_dim, aux_dim):
super(KoopmanNet, self).__init__()
self.dt = dt
self.z_dim = z_dim
self.n_state = n_state
self.n_ctrl = n_ctrl
self.encoder = nn.Sequential(nn.Linear(n_state, h_dim),
# nn.GELU(),
# nn.Linear(h_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, z_dim))
self.decoder = nn.Sequential(nn.Linear(z_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, n_state))
# aux
self.aux_nn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, z_dim))
self.bux_nn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, z_dim))
self.diag_nn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, z_dim+n_ctrl))
self.lower_nn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, ((z_dim+n_ctrl)**2)//2 - z_dim+n_ctrl))
self.pnn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, z_dim+n_ctrl))
def block(self, a, b):
return torch.exp(a*dt) * torch.stack((torch.stack((torch.cos(b*dt), -torch.sin(b*dt))),
torch.stack((torch.sin(b*dt), torch.cos(b*dt)))))
def aux(self, x):
a, b = self.aux_nn(x)
return self.block(a, b)
def bux(self, x):
a, b = self.bux_nn(x)
return torch.stack((a.unsqueeze(0), b.unsqueeze(0)))
def psd(self, diag, lower):
L = torch.diag(diag)
idx1, idx2 = np.tril_indices(len(L), k=-1)
L = L.index_put((torch.tensor(idx1), torch.tensor(idx2)), lower)
return L
def cux(self, x):
diag = self.diag_nn(x)
lower = self.lower_nn(x)
L = self.psd(diag, lower)
C = L @ L.T
return C
def pux(self, x):
return self.pnn(x)
def encode(self, x):
z = self.encoder(x)
return z
def decode(self, z):
x = self.decoder(z)
return x
def identity(self, x):
return self.decode(self.encode(x))
def predict(self, z, u, x):
return self.aux(x) @ z + self.bux(x) @ u
class BlockKoopmanNet(nn.Module):
def __init__(self, dt, n_state, n_ctrl, h_dim, z_dim, aux_dim):
super(BlockKoopmanNet, self).__init__()
self.dt = dt
self.z_dim = z_dim
self.n_state = n_state
self.n_ctrl = n_ctrl
self.n_block = z_dim // 2 # number of complex-conjugate pair Jordan blocks
self.z_dim = z_dim
self.encoder = nn.Sequential(nn.Linear(n_state, h_dim),
# nn.GELU(),
# nn.Linear(h_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, z_dim))
self.decoder = nn.Sequential(nn.Linear(z_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, n_state))
# aux
self.aux_nn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, z_dim))
self.bux_nn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, z_dim*n_ctrl))
self.diag_nn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, z_dim+n_ctrl))
self.lower_nn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, ((z_dim+n_ctrl)**2 - (z_dim+n_ctrl))//2)) # subtract the diagonal
self.pnn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, z_dim+n_ctrl))
def block(self, a, b):
return torch.exp(a*dt) * torch.stack((torch.stack((torch.cos(b*dt), -torch.sin(b*dt))),
torch.stack((torch.sin(b*dt), torch.cos(b*dt)))))
def aux(self, x):
A = []
for a, b in torch.tensor_split(self.aux_nn(x), self.n_block):
A.append(self.block(a, b))
return torch.block_diag(*A)
def bux(self, x):
return torch.reshape(self.bux_nn(x), (self.z_dim, self.n_ctrl))
def psd(self, diag, lower):
L = torch.diag(diag)
idx1, idx2 = np.tril_indices(len(L), k=-1)
L = L.index_put((torch.tensor(idx1), torch.tensor(idx2)), lower)
return L
def cux(self, x):
diag = self.diag_nn(x)
lower = self.lower_nn(x)
L = self.psd(diag, lower)
C = L @ L.T
return C
def pux(self, x):
return self.pnn(x)
def encode(self, x):
z = self.encoder(x)
return z
def decode(self, z):
x = self.decoder(z)
return x
def identity(self, x):
return self.decode(self.encode(x))
def predict(self, z, u, x):
return self.aux(x) @ z + self.bux(x) @ u
class ContinuousYinNetwork(nn.Module):
def __init__(self, dt, n_state, n_ctrl, h_dim, z_dim, aux_dim, T=10, scalar=2.):
super(ContinuousYinNetwork, self).__init__()
self.dt = dt
self.z_dim = z_dim
self.n_state = n_state
self.n_ctrl = n_ctrl
self.n_block = z_dim // 2 # number of complex-conjugate pair Jordan blocks
self.z_dim = z_dim
self.T = T
self.xr = nn.Parameter(torch.zeros(z_dim,))
self.scalar = scalar
self.encoder = nn.Sequential(nn.Linear(n_state, h_dim),
# nn.GELU(),
# nn.Linear(h_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, z_dim))
self.decoder = nn.Sequential(nn.Linear(z_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, h_dim),
nn.GELU(),
nn.Linear(h_dim, n_state))
# aux
self.aux_nn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, z_dim))
self.bux_nn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, z_dim*n_ctrl))
self.diag_nn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, z_dim))
self.lower_nn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, ((z_dim)**2)//2 - z_dim//2)) # subtract the diagonal
self.rux_nn = nn.Sequential(nn.Linear(n_state, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, aux_dim),
nn.GELU(),
nn.Linear(aux_dim, n_ctrl*n_ctrl))
def block(self, a, b):
return torch.exp(a*dt) * torch.stack((torch.stack((torch.cos(b*dt), -torch.sin(b*dt))),
torch.stack((torch.sin(b*dt), torch.cos(b*dt)))))
def aux(self, x):
A = []
for a, b in torch.tensor_split(self.aux_nn(x), self.n_block):
A.append(self.block(a, b))
return torch.block_diag(*A)
def bux(self, x):
return torch.reshape(self.bux_nn(x), (self.z_dim, self.n_ctrl))
def psd(self, diag, lower):
L = torch.diag(diag)
idx1, idx2 = np.tril_indices(len(L), k=-1)
L = L.index_put((torch.tensor(idx1), torch.tensor(idx2)), lower)
return L
def qux(self, x):
# obtain positive semidefinite Q
diag = self.diag_nn(x)
lower = self.lower_nn(x)
L = self.psd(diag, lower)
C = L @ L.T
return C
def rux(self, x):
# obtain positive definite R
M, _ = torch.linalg.qr(torch.reshape(self.rux_nn(x), (self.n_ctrl, self.n_ctrl))) # use QR decomposition to obtain a full rank matrix
return M.T @ M
def encode(self, x):
z = self.encoder(x)
return z
def decode(self, z):
x = self.decoder(z)
return x
def identity(self, x):
return self.decode(self.encode(x))
def predict(self, z, u, x):
return self.aux(x) @ z + self.bux(x) @ u
def dare(self, A, B, Q, R, N=150): # play with N, ranges from 25 to 150
"""
Solve the discrete algebraic Riccati equation (DARE)
"""
P = torch.eye(A.size(0))
for _ in range(N):
next_P = Q + A.T @ P @ A - A.T @ P @ B @ torch.inverse(R + B.T @ P @ B) @ B.T @ P @ A
P = next_P
return P
def lqr(self, x0, A, B, Q, R):
"""
Solve the discrete time LQR controller.
"""
P = self.dare(A, B, Q, R)
# Compute the LQR gain
K = -torch.inverse(R + B.T @ P @ B) @ (B.T @ P @ A)
x = x0
x_traj = [x0]
u_traj = []
for _ in range(self.T):
u = K @ (x - self.xr)
x = A @ x + B @ u
x_traj.append(x)
u_traj.append(u)
return torch.stack(x_traj), torch.tanh(torch.stack(u_traj)) * self.scalar
def predict(model, zinit, u, xinit):
z_next = zinit
x_nexts = []
z_nexts = []
for i in range(T):
z_next = model.predict(z_next, u[i], xinit)
x_nexts.append(model.decode(z_next))
z_nexts.append(z_next)
return torch.vstack(x_nexts), torch.vstack(z_nexts)
def pred_loss_fn(model, zinits, xinits, x_true, u_true):
z_true = torch.vmap(model.encode)(x_true)
x_pred, z_pred = torch.vmap(predict, in_dims=(None, 0, 0, 0))(model, zinits, u_true, xinits)
pred_loss = torch.mean((x_pred - x_true)**2) + torch.mean((z_pred - z_true)**2)
return pred_loss
def loss_fn(model, xinits, x_true, u_true, u_upper, u_lower, T=20, is_pred_loss=False, is_constrained=True):
batch_size = xinits.shape[0]
zinits = torch.vmap(model.encode)(xinits)
dx = LinDx(torch.cat((torch.vmap(model.aux)(xinits), torch.vmap(model.bux)(xinits)), dim=-1).unsqueeze(0).repeat(T, 1, 1, 1))
C = torch.vmap(model.cux)(xinits).unsqueeze(0).repeat(T, 1, 1, 1)
c = torch.vmap(model.pux)(xinits).unsqueeze(0).repeat(T, 1, 1)
cost = QuadCost(C, c)
u_init = None
z_pred, u_pred, objs_pred = mpc.MPC(
model.z_dim, n_ctrl, T,
u_lower=u_lower, u_upper=u_upper, u_init=u_init,
lqr_iter=T*2 if is_constrained else 1,
verbose=-1,
exit_unconverged=False,
detach_unconverged=False,
n_batch=batch_size,
)(zinits, cost, dx)
# note that I'm not warm-starting, I could use the indices for that
x_pred = torch.vmap(model.decode)(z_pred)
u_pred = u_pred.transpose(0, 1) # (T, B, 1) -> (B, T, 1) to match u_true's shape
x_pred = x_pred.transpose(0, 1) # (T, B, 3) -> (B, T, 3) to match x_true's shape
im_loss = torch.mean((u_true - u_pred)**2)
sysid_loss = torch.mean((x_true - x_pred)**2)
id_loss = torch.mean((model.identity(x_true) - x_true)**2)
if is_pred_loss:
pred_loss = pred_loss_fn(model, zinits, xinits, x_true, u_true)
else:
pred_loss = 0
traj_loss = im_loss + id_loss
return traj_loss, im_loss, id_loss
def loss_fn_rnn(model, xinits, x_true, u_true, T=20, scalar=None):
batch_size = xinits.shape[0]
if scalar:
u_pred = model(xinits, scalar)
else:
u_pred = model(xinits)
im_loss = torch.mean((u_true - u_pred)**2)
sysid_loss = 0
id_loss = 0
pred_loss = 0
traj_loss = im_loss
return traj_loss, im_loss, id_loss#, sysid_loss, pred_loss
def loss_fn_reflex(model, xinits, x_true, u_true, T=20, scalar=None):
batch_size = xinits.shape[0]
if scalar:
u_pred = model(xinits, scalar)
else:
u_pred = model(xinits)
u_pred = u_pred.unsqueeze(-1)
im_loss = torch.mean((u_true - u_pred)**2)
id_loss = 0
pred_loss = 0
traj_loss = im_loss
return traj_loss, im_loss, id_loss
def loss_fn_contyin(model, xinits, x_true, u_true, u_upper, u_lower, T=20, is_pred_loss=False, is_constrained=True):
batch_size = xinits.shape[0]
zinits = torch.vmap(model.encode)(xinits)
A = torch.vmap(model.aux)(xinits)
B = torch.vmap(model.bux)(xinits)
Q = torch.vmap(model.qux)(xinits)
R = torch.vmap(model.rux)(xinits)
model.scalar = u_upper # attach scalar to the model
z_pred, u_pred = torch.vmap(model.lqr)(zinits, A, B, Q, R)
# note that I'm not warm-starting, I could use the indices for that
im_loss = torch.mean((u_true - u_pred)**2)
id_loss = torch.mean((model.identity(x_true) - x_true)**2)
if is_pred_loss:
pred_loss = pred_loss_fn(model, zinits, xinits, x_true, u_true)
else:
pred_loss = 0
traj_loss = im_loss + id_loss
return traj_loss, im_loss, id_loss
parser = argparse.ArgumentParser(prog="Koopman DMPC",
description="Learn constrained policies.")
parser.add_argument("-en", "--env_name", default="pendulum")
parser.add_argument("-s", "--seed", default=0, type=int)
parser.add_argument("-p", "--project", default="koopman-dmpc")
parser.add_argument("-mt", "--model_type", default="koopman")
parser.add_argument("-e", "--num_epochs", default=250, type=int)
parser.add_argument("-b", "--batch_size", default=100, type=int)
parser.add_argument("-t", "--horizon", default=10, type=int)
parser.add_argument("-ub", "--upper_bound", default=2.0, type=float)
parser.add_argument("-lb", "--lower_bound", default=-2.0, type=float)
parser.add_argument("-ubt", "--upper_bound_test", default=1.0, type=float)
parser.add_argument("-lbt", "--lower_bound_test", default=-1.0, type=float)
args = parser.parse_args()
seed = args.seed
np.random.seed(seed)
batch_size = args.batch_size
def make_data(n_state, n_ctrl, data, batch_size=32, warmstart=None, shuffle=False):
xs, us = data[:,:,:n_state], data[:,:,-n_ctrl:]
xinits = xs[:,0]
n_data = xinits.shape[0]
ds = TensorDataset(xinits, xs, us, torch.arange(0,n_data))
loader = DataLoader(ds, batch_size=batch_size, shuffle=shuffle)
return ds, loader
## assume data has already been created by make_data.py before reading into memory!
torch.manual_seed(seed)
env_name = args.env_name
u_upper = args.upper_bound
u_lower = args.lower_bound
u_upper_test = args.upper_bound_test
u_lower_test = args.lower_bound_test
with open("./data/{}_upper{:.1f}_lower{:.1f}.pkl".format(env_name, u_upper, u_lower), "rb") as f:
env2 = pkl.load(f)
with open("./data/{}_upper{:.1f}_lower{:.1f}.pkl".format(env_name, u_upper_test, u_lower_test), "rb") as f:
env1 = pkl.load(f)
n_state, n_ctrl = env2.true_dx.n_state, env2.true_dx.n_ctrl
# train bounds
train_data, train = make_data(n_state, n_ctrl,
env2.train_data, batch_size=batch_size, shuffle=True)
val_data2, val2 = make_data(n_state, n_ctrl,
env2.val_data, batch_size=batch_size)
test_data2, test2 = make_data(n_state, n_ctrl,
env2.test_data, batch_size=batch_size)
# test bounds
val_data1, val1 = make_data(n_state, n_ctrl,
env1.val_data, batch_size=batch_size)
test_data1, test1 = make_data(n_state, n_ctrl,
env1.test_data, batch_size=batch_size)
model_type = args.model_type
T = args.horizon
dt = 0.05
is_koopman = model_type == "koopman"
is_rnn = model_type == "rnn"
is_reflex = model_type == "reflex"
is_cyin = model_type == "cyin"
if is_koopman:
model = BlockKoopmanNet(dt, n_state, n_ctrl, 80, 2 if env_name == "pendulum" or "mountaincar" else 6, 170)
elif is_rnn:
model = RNN(n_state, n_ctrl, 256, T=T)
elif is_reflex:
model = ReflexNet(n_state, n_ctrl, 256, T=T)
elif is_cyin:
model = ContinuousYinNetwork(dt, n_state, n_ctrl, 80, 2 if env_name == "pendulum" or "mountaincar" else 6, 170, T=T)
criterion = nn.MSELoss()
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3)
num_epochs = args.num_epochs
times = []
for epoch in tqdm(range(10)):
start = time.time()
for j, (xinits, x_true, u_true, idxs) in enumerate(train):
if is_koopman:
loss, im_loss, id_loss = loss_fn(model, xinits, x_true, u_true, u_upper, u_lower, T=T)
elif is_rnn:
loss, im_loss, id_loss = loss_fn_rnn(model, xinits, x_true, u_true, T=T, scalar=u_upper)
elif is_reflex:
loss, im_loss, id_loss = loss_fn_reflex(model, xinits, x_true, u_true, T=T, scalar=u_upper)
elif is_cyin:
loss, im_loss, id_loss = loss_fn_contyin(model, xinits, x_true, u_true, u_upper, u_lower, T=T)
optimizer.zero_grad()
loss.backward()
optimizer.step()
times.append(time.time() - start)
print(times)
print(np.median(times), np.std(times))
with torch.no_grad():
def no_grad(loader, u_upper, u_lower):
avg_loss = []
avg_im_loss = []
avg_id_loss = []
for j, (xinits, x_true, u_true, idxs) in enumerate(loader):
if is_koopman:
loss, im_loss, id_loss = loss_fn(model, xinits, x_true, u_true, u_upper, u_lower, T=T)
elif is_rnn:
loss, im_loss, id_loss = loss_fn_rnn(model, xinits, x_true, u_true, T=T, scalar=u_upper)
elif is_reflex:
loss, im_loss, id_loss = loss_fn_reflex(model, xinits, x_true, u_true, T=T, scalar=u_upper)
elif is_cyin:
loss, im_loss, id_loss = loss_fn_contyin(model, xinits, x_true, u_true, u_upper, u_lower, T=T)
avg_loss.append(loss)
avg_im_loss.append(im_loss)
avg_id_loss.append(id_loss)
return np.mean(avg_loss), np.mean(avg_im_loss), np.mean(avg_id_loss)
## (-2, 2)
times = []
for _ in range(10):
start = time.time()
avg_loss, avg_im_loss, avg_id_loss = no_grad(test2, u_upper_test, u_lower_test)
times.append(time.time() - start)
print(times)
print(np.median(times), np.std(times))