Repository navigation
Expand file tree
/
Copy pathintegrators.py
More file actions
176 lines (140 loc) · 5.67 KB
/
Copy pathintegrators.py
File metadata and controls
176 lines (140 loc) · 5.67 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
import torch
import torch.nn as nn
import time
import numpy as np
import re
from postprocessing import extract_parameter_name
def getIntegrator(config, model, ts):
integrators = {
'RK4' : RK4Simulator,
'discrete' : Sim_discrete,
}[config.integrator]
return integrators(model, ts)
class Simulator(nn.Module):
def __init__(self, ss_model, ts) -> None:
super(Simulator, self).__init__()
self.ss_model = ss_model
self.ts = ts
def clone(self):
copy_ss = self.ss_model.clone() # State-space model module must have a clone function
copy = type(self)(copy_ss, self.ts)
copy.load_state_dict(self.state_dict())
return copy
def simulate(self, u, x0):
start_time = time.time()
with torch.no_grad():
x_sim, y_sim= self(u[None, :, :], x0[None, :]) # Appel à forward
x_sim.squeeze_(0)
y_sim.squeeze_(0)
sim_time = time.time() - start_time
print(f"\nSim time: {sim_time:.2f}")
return x_sim, y_sim
def extract_params(self):
dict_param = {}
# Extract the parametrized linear part if has one
if hasattr(self.ss_model, 'linmod'): # If model has a linear part
if hasattr(self.ss_model.linmod, "eval_") and callable(getattr(self.ss_model.linmod, 'eval_')):
A,Bd,C = self.ss_model.linmod.eval_()
dict_param['A'] = A
dict_param['Bd'] = Bd
dict_param['C'] = C
for name_param, param_tensor in self.named_parameters():
tensor = param_tensor.detach().numpy()
name = extract_parameter_name(name_param)
dict_param[name] = tensor
return dict_param
def extract_weights(self):
weights = []
biases = []
for param_tensor in self.state_dict():
tensor = self.state_dict()[param_tensor].detach().numpy()
if 'weight' in param_tensor:
weights.append(tensor)
if 'bias' in param_tensor:
biases.append(tensor)
weights = np.array(weights, dtype= object)
biases = np.array(biases, dtype = object)
return weights, biases
def write_flat_params(self, x):
r""" Writes vector x to model parameters.."""
index = 0
theta = torch.Tensor(x)
for name, p in self.named_parameters():
p.data = theta[index:index + p.numel()].view_as(p.data)
index = index + p.numel()
def flatten_params(self):
views = []
for i,p in enumerate(self.parameters()):
if p is None:
view = p.new(p.numel()).zero_()
elif p.is_sparse:
view = p.to_dense().view(-1)
else:
view = p.view(-1)
views.append(view)
return torch.cat(views, 0)
class RK4Simulator(Simulator):
def __init__(self, ss_model, ts):
super(RK4Simulator, self).__init__(ss_model=ss_model, ts=ts)
def forward(self, u_batch, x0_batch = torch.Tensor([[0., 0., 0., 0.]])):
""" Multi-step simulation over (mini)batches
Parameters
----------
u_batch: Tensor. Size: (m, q, n_u)
Input sequence for each subsequence in the minibatch
x0_batch: Tensor. Size: (q, n_x)
initial state for each sequence in the minibatch
x_batch: Tensor. Size: (m, q, n_x)
state sequence for each subsequence in the minibatch
Returns
-------
Tensor. Size: (m, q, n_x)
Simulated state for all subsequences in the minibatch
"""
X_sim_list = []
Y_sim_list= []
x_step = x0_batch
for u_step in u_batch.split(1, dim =1):#i in range(seq_len):
u_step = u_step.squeeze(1)
# x_step = x_step.squeeze(0)
X_sim_list += [x_step]
dt2 = self.ts / 2.0
k1,y_step = self.ss_model(u_step, x_step)
Y_sim_list += [y_step]
k2_dx, _ = self.ss_model(u_step, x_step + dt2*k1)
k3_dx, _= self.ss_model(u_step, x_step + dt2*k2_dx)
k4_dx, _ = self.ss_model(u_step, x_step + self.ts*k3_dx)
dx = self.ts / 6.0 * (k1 + 2.0 * k2_dx + 2.0 * k3_dx + k4_dx)
x_step = x_step + dx
X_sim = torch.stack(X_sim_list, 1)
Y_sim = torch.stack(Y_sim_list, 1)
return X_sim, Y_sim
class Sim_discrete(Simulator):
def __init__(self, ss_model, ts = 1):
super(Sim_discrete, self).__init__(ss_model=ss_model, ts=ts)
def forward(self, u_batch, x0_batch = torch.Tensor([[0., 0., 0., 0.]])):
""" Multi-step simulation over (mini)batches
Parameters
----------
u_batch: Tensor. Size: (N_batch, seq_len, n_u)
Input sequence for each subsequence in the minibatch
x0_batch: Tensor. Size: (N_batch, n_x)
initial state for each sequence in the minibatch
x_batch: Tensor. Size: (N_batch, seq_len, n_x)
state sequence for each subsequence in the minibatch
Returns
-------
Tensor. Size: (N_batch, seq_len, n_x)
Simulated state and outputs for all subsequences in the minibatch
"""
X_sim_list = []
Y_sim_list= []
x_step = x0_batch
for u_step in u_batch.split(1, dim = 1):#i in range(seq_len):
u_step = u_step.squeeze(1)
X_sim_list += [x_step]
x_step,y_step = self.ss_model(u_step, x_step)
Y_sim_list += [y_step]
X_sim = torch.stack(X_sim_list, 1)
Y_sim = torch.stack(Y_sim_list, 1)
return X_sim, Y_sim