-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKalmanNet_nn.py
353 lines (275 loc) · 11.1 KB
/
KalmanNet_nn.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
"""# **Class: KalmanNet**"""
import torch
import torch.nn as nn
import torch.nn.functional as func
import matplotlib.pyplot as plt
from filing_paths import path_model
import sys
sys.path.insert(1, path_model)
from model import getJacobian
if torch.cuda.is_available():
dev = torch.device("cuda:0")
torch.set_default_tensor_type("torch.cuda.FloatTensor")
else:
dev = torch.device("cpu")
in_mult = 5
out_mult = 40
class KalmanNetNN(torch.nn.Module):
###################
### Constructor ###
###################
def __init__(self):
super().__init__()
######################################
### Initialize Kalman Gain Network ###
######################################
def Build(self, SysModel, steady_state=False, is_control_enable=True):
# Set flags
self.steady_state = steady_state
self.is_control_enable = is_control_enable
self.InitSystemDynamics(SysModel, infoString = "partialInfo")
self.InitSequence(SysModel.m1x_0, SysModel.T)
# # Number of neurons in the 1st hidden layer
# H1_KNet = (ssModel.m + ssModel.n) * (10) * 8
# # Number of neurons in the 2nd hidden layer
# H2_KNet = (ssModel.m * ssModel.n) * 1 * (4)
self.InitKGainNet(SysModel.prior_Q, SysModel.prior_Sigma, SysModel.prior_S)
######################################
### Initialize Kalman Gain Network ###
######################################
def InitKGainNet(self, prior_Q, prior_Sigma, prior_S):
self.seq_len_input = 1
self.batch_size = 1
self.prior_Q = prior_Q
self.prior_Sigma = prior_Sigma
self.prior_S = prior_S
# GRU to track Q
self.d_input_Q = self.m * in_mult
self.d_hidden_Q = self.m ** 2
self.GRU_Q = nn.GRU(self.d_input_Q, self.d_hidden_Q)
self.h_Q = torch.randn(self.seq_len_input, self.batch_size, self.d_hidden_Q).to(dev, non_blocking=True)
# GRU to track Sigma
self.d_input_Sigma = self.d_hidden_Q + self.m * in_mult
self.d_hidden_Sigma = self.m ** 2
self.GRU_Sigma = nn.GRU(self.d_input_Sigma, self.d_hidden_Sigma)
self.h_Sigma = torch.randn(self.seq_len_input, self.batch_size, self.d_hidden_Sigma).to(dev, non_blocking=True)
# GRU to track S
self.d_input_S = self.n ** 2 + 2 * self.n * in_mult
self.d_hidden_S = self.n ** 2
self.GRU_S = nn.GRU(self.d_input_S, self.d_hidden_S)
self.h_S = torch.randn(self.seq_len_input, self.batch_size, self.d_hidden_S).to(dev, non_blocking=True)
# Fully connected 1
self.d_input_FC1 = self.d_hidden_Sigma
self.d_output_FC1 = self.n ** 2
self.FC1 = nn.Sequential(
nn.Linear(self.d_input_FC1, self.d_output_FC1),
nn.ReLU())
# Fully connected 2
self.d_input_FC2 = self.d_hidden_S + self.d_hidden_Sigma
self.d_output_FC2 = self.n * self.m
self.d_hidden_FC2 = self.d_input_FC2 * out_mult
self.FC2 = nn.Sequential(
nn.Linear(self.d_input_FC2, self.d_hidden_FC2),
nn.ReLU(),
nn.Linear(self.d_hidden_FC2, self.d_output_FC2))
# Fully connected 3
self.d_input_FC3 = self.d_hidden_S + self.d_output_FC2
self.d_output_FC3 = self.m ** 2
self.FC3 = nn.Sequential(
nn.Linear(self.d_input_FC3, self.d_output_FC3),
nn.ReLU())
# Fully connected 4
self.d_input_FC4 = self.d_hidden_Sigma + self.d_output_FC3
self.d_output_FC4 = self.d_hidden_Sigma
self.FC4 = nn.Sequential(
nn.Linear(self.d_input_FC4, self.d_output_FC4),
nn.ReLU())
# Fully connected 5
self.d_input_FC5 = self.m
self.d_output_FC5 = self.m * in_mult
self.FC5 = nn.Sequential(
nn.Linear(self.d_input_FC5, self.d_output_FC5),
nn.ReLU())
# Fully connected 6
self.d_input_FC6 = self.m
self.d_output_FC6 = self.m * in_mult
self.FC6 = nn.Sequential(
nn.Linear(self.d_input_FC6, self.d_output_FC6),
nn.ReLU())
# Fully connected 7
self.d_input_FC7 = 2 * self.n
self.d_output_FC7 = 2 * self.n * in_mult
self.FC7 = nn.Sequential(
nn.Linear(self.d_input_FC7, self.d_output_FC7),
nn.ReLU())
"""
# Fully connected 8
self.d_input_FC8 = self.d_hidden_Q
self.d_output_FC8 = self.d_hidden_Q
self.d_hidden_FC8 = self.d_hidden_Q * Q_Sigma_mult
self.FC8 = nn.Sequential(
nn.Linear(self.d_input_FC8, self.d_hidden_FC8),
nn.ReLU(),
nn.Linear(self.d_hidden_FC8, self.d_output_FC8))
"""
##################################
### Initialize System Dynamics ###
##################################
def InitSystemDynamics(self, SysModel, infoString = 'fullInfo'):
if(infoString == 'partialInfo'):
self.fString ='ModInacc'
self.hString ='ObsInacc'
else:
self.fString ='ModAcc'
self.hString ='ObsAcc'
# Set State Evolution Function
self.f = SysModel.f
self.m = SysModel.m
self.G = SysModel.G
self.p = SysModel.p
# Set Observation Function
self.h = SysModel.h
self.n = SysModel.n
###########################
### Initialize Sequence ###
###########################
def InitSequence(self, M1_0, T):
self.T = T
# self.x_out = torch.empty(self.m, T).to(dev, non_blocking=True)
self.m1x_posterior = M1_0.to(dev, non_blocking=True)
self.m1x_posterior_previous = self.m1x_posterior.to(dev, non_blocking=True)
self.m1x_prior_previous = self.m1x_posterior.to(dev, non_blocking=True)
self.y_previous = self.h(self.m1x_posterior).to(dev, non_blocking=True)
# KGain saving
self.i = 0
self.KGain_array = self.KG_array = torch.zeros((self.T,self.m,self.n)).to(dev, non_blocking=True)
######################
### Compute Priors ###
######################
def step_prior(self, u):
# Predict the 1-st moment of x
self.m1x_prior = self.f(self.m1x_posterior) + torch.matmul(self.G, u)
# Predict the 1-st moment of y
self.m1y = self.h(self.m1x_prior)
##############################
### Kalman Gain Estimation ###
##############################
def step_KGain_est(self, y):
obs_diff = y - torch.squeeze(self.y_previous)
obs_innov_diff = y - torch.squeeze(self.m1y)
fw_evol_diff = torch.squeeze(self.m1x_posterior) - torch.squeeze(self.m1x_posterior_previous)
fw_update_diff = torch.squeeze(self.m1x_posterior) - torch.squeeze(self.m1x_prior_previous)
obs_diff = func.normalize(obs_diff, p=2, dim=0, eps=1e-12, out=None)
obs_innov_diff = func.normalize(obs_innov_diff, p=2, dim=0, eps=1e-12, out=None)
fw_evol_diff = func.normalize(fw_evol_diff, p=2, dim=0, eps=1e-12, out=None)
fw_update_diff = func.normalize(fw_update_diff, p=2, dim=0, eps=1e-12, out=None)
# Kalman Gain Network Step
KG = self.KGain_step(obs_diff, obs_innov_diff, fw_evol_diff, fw_update_diff)
# Reshape Kalman Gain to a Matrix
self.KGain = torch.reshape(KG, (self.m, self.n))
#######################
### Kalman Net Step ###
#######################
def KNet_step(self, y, u):
# Compute Priors
self.step_prior(u)
# Compute Kalman Gain
self.step_KGain_est(y)
# Save KGain in array
self.KGain_array[self.i] = self.KGain
self.i += 1
# Innovation
# y_obs = torch.unsqueeze(y, 1)
# dy = y_obs - self.m1y
dy = y - self.m1y
# Compute the 1-st posterior moment
INOV = torch.matmul(self.KGain, dy)
self.m1x_posterior_previous = self.m1x_posterior
self.m1x_posterior = self.m1x_prior + INOV
#self.state_process_posterior_0 = self.state_process_prior_0
self.m1x_prior_previous = self.m1x_prior
# update y_prev
self.y_previous = y
# return
return torch.squeeze(self.m1x_posterior)
########################
### Kalman Gain Step ###
########################
def KGain_step(self, obs_diff, obs_innov_diff, fw_evol_diff, fw_update_diff):
def expand_dim(x):
expanded = torch.empty(self.seq_len_input, self.batch_size, x.shape[-1])
expanded[0, 0, :] = x
return expanded
obs_diff = expand_dim(obs_diff)
obs_innov_diff = expand_dim(obs_innov_diff)
fw_evol_diff = expand_dim(fw_evol_diff)
fw_update_diff = expand_dim(fw_update_diff)
####################
### Forward Flow ###
####################
# FC 5
in_FC5 = fw_evol_diff
out_FC5 = self.FC5(in_FC5)
# Q-GRU
in_Q = out_FC5
out_Q, self.h_Q = self.GRU_Q(in_Q, self.h_Q)
"""
# FC 8
in_FC8 = out_Q
out_FC8 = self.FC8(in_FC8)
"""
# FC 6
in_FC6 = fw_update_diff
out_FC6 = self.FC6(in_FC6)
# Sigma_GRU
in_Sigma = torch.cat((out_Q, out_FC6), 2)
out_Sigma, self.h_Sigma = self.GRU_Sigma(in_Sigma, self.h_Sigma)
# FC 1
in_FC1 = out_Sigma
out_FC1 = self.FC1(in_FC1)
# FC 7
in_FC7 = torch.cat((obs_diff, obs_innov_diff), 2)
out_FC7 = self.FC7(in_FC7)
# S-GRU
in_S = torch.cat((out_FC1, out_FC7), 2)
out_S, self.h_S = self.GRU_S(in_S, self.h_S)
# FC 2
in_FC2 = torch.cat((out_Sigma, out_S), 2)
out_FC2 = self.FC2(in_FC2)
#####################
### Backward Flow ###
#####################
# FC 3
in_FC3 = torch.cat((out_S, out_FC2), 2)
out_FC3 = self.FC3(in_FC3)
# FC 4
in_FC4 = torch.cat((out_Sigma, out_FC3), 2)
out_FC4 = self.FC4(in_FC4)
# updating hidden state of the Sigma-GRU
self.h_Sigma = out_FC4
return out_FC2
###############
### Forward ###
###############
def forward(self, y, u):
y = y.to(dev, non_blocking=True)
'''
for t in range(0, self.T):
self.x_out[:, t] = self.KNet_step(y[:, t])
'''
self.x_out = self.KNet_step(y, u)
return self.x_out
#########################
### Init Hidden State ###
#########################
def init_hidden(self):
weight = next(self.parameters()).data
hidden = weight.new(1, self.batch_size, self.d_hidden_S).zero_()
self.h_S = hidden.data
self.h_S[0, 0, :] = self.prior_S.flatten()
hidden = weight.new(1, self.batch_size, self.d_hidden_Sigma).zero_()
self.h_Sigma = hidden.data
self.h_Sigma[0, 0, :] = self.prior_Sigma.flatten()
hidden = weight.new(1, self.batch_size, self.d_hidden_Q).zero_()
self.h_Q = hidden.data
self.h_Q[0, 0, :] = self.prior_Q.flatten()