-
Notifications
You must be signed in to change notification settings - Fork 1
/
falco_systemID.py
580 lines (487 loc) · 30.2 KB
/
falco_systemID.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
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import scipy.io as sio
plt.ion()
plt.ion()
class SSM(object):
# linear state space model for the optical system
# the incoherent light is not considered here, however, it should be easy to include them
def __init__(self, Jacobian1, Jacobian2, Q0, Q1, R0, R1, n_observ=4):
# parameters Q0, Q1, R0, R1 define the noise covariance of the state space model
# process noises covariance: Q = Q0 + Q1 * sum(uc^2)
# observation noises covariance: R = R0 + R1 * probe_contrast + R2 * probe_contrast^2
# Jacobian matrices
self.G1_real = tf.Variable(Jacobian1.real, dtype=tf.float64)
self.G1_imag = tf.Variable(Jacobian1.imag, dtype=tf.float64)
self.G2_real = tf.Variable(Jacobian2.real, dtype=tf.float64)
self.G2_imag = tf.Variable(Jacobian2.imag, dtype=tf.float64)
# noise parameters, all of them should be positive
self.q0 = tf.Variable(np.log(Q0), dtype=tf.float64)
self.q1 = tf.Variable(np.log(Q1), dtype=tf.float64)
self.r0 = tf.Variable(np.log(R0), dtype=tf.float64)
self.r1 = tf.Variable(np.log(R1), dtype=tf.float64)
# self.r2 = tf.Variable(np.log(R2), dtype=tf.float64)
self.Q0 = tf.exp(self.q0)
self.Q1 = tf.exp(self.q1)
self.R0 = tf.exp(self.r0)
self.R1 = tf.exp(self.r1)
# self.R2 = tf.exp(self.r2)
self.num_pix = Jacobian1.shape[0]
self.num_act = Jacobian1.shape[1]
self.n_observ = int(n_observ)
def transition(self, Enp, u1, u2):
# state transition model
Enp_next = Enp + tf.cast(tf.tensordot(u1, self.G1_real, axes=[[-1], [1]]) + tf.tensordot(u2, self.G2_real, axes=[[-1], [1]]), tf.complex128) + \
+ 1j * tf.cast(tf.tensordot(u1, self.G1_imag, axes=[[-1], [1]]) + tf.tensordot(u2, self.G2_imag, axes=[[-1], [1]]), tf.complex128)
return Enp_next
def transition_covariance(self, Enp, u1, u2):
# covariance of process/transition noises
# u1_square = tf.reduce_sum(tf.abs(u1)**2, axis=1)
# u2_square = tf.reduce_sum(tf.abs(u2)**2, axis=1)
# Qco = tf.tensordot(tf.expand_dims(u1_square, 1), tf.expand_dims(self.Q1*tf.ones(self.num_pix, dtype=tf.float64), 0), axes=[[1], [0]]) + \
# tf.tensordot(tf.expand_dims(u2_square, 1), tf.expand_dims(self.Q1*tf.ones(self.num_pix, dtype=tf.float64), 0), axes=[[1], [0]]) + self.Q0 + 1e-14
# u1_cubic = tf.reduce_sum(tf.abs(u1)**3, axis=1)
# u2_cubic = tf.reduce_sum(tf.abs(u2)**3, axis=1)
# Qco = tf.tensordot(tf.expand_dims(u1_cubic, 1), tf.expand_dims(self.Q1*tf.ones(self.num_pix, dtype=tf.float64), 0), axes=[[1], [0]]) + \
# tf.tensordot(tf.expand_dims(u2_cubic, 1), tf.expand_dims(self.Q1*tf.ones(self.num_pix, dtype=tf.float64), 0), axes=[[1], [0]]) + self.Q0 + 1e-14
dE = tf.cast(tf.tensordot(u1, self.G1_real, axes=[[-1], [1]]) + tf.tensordot(u2, self.G2_real, axes=[[-1], [1]]), tf.complex128) + \
+ 1j * tf.cast(tf.tensordot(u1, self.G1_imag, axes=[[-1], [1]]) + tf.tensordot(u2, self.G2_imag, axes=[[-1], [1]]), tf.complex128)
dE_square = tf.reduce_mean(tf.abs(dE)**2, axis=1)
Qco = tf.tensordot(tf.expand_dims(dE_square, 1), tf.expand_dims(self.Q1*tf.ones(self.num_pix, dtype=tf.float64), 0), axes=[[1], [0]]) + self.Q0 + 1e-14
return Qco
def observation(self, Enp, u_p1, u_p2):
# observation model
n_probe = self.n_observ
E_p_list = []
I_p_correction_list = []
for k in range(n_probe):
E_p = self.transition(Enp, u_p1[:, k, :], u_p2[:, k, :])
E_p_list.append(tf.expand_dims(E_p, 1))
I_p_correction = 2*self.transition_covariance(Enp, u_p1[:, k, :], u_p2[:, k, :])
I_p_correction_list.append(tf.expand_dims(I_p_correction, 1))
E_p_obs = tf.concat(E_p_list, axis=1)
I_p_cor = tf.concat(I_p_correction_list, axis=1)
I_p_obs = tf.abs(E_p_obs)**2 + I_p_cor
return E_p_obs, I_p_obs
def observation_covariance(self, I_p_obs, u_p1, u_p2):#(self, Enp, u_p1, u_p2):
# covariance of observation noises
# _, I_p_obs = self.observation(Enp, u_p1, u_p2)
contrast_p = tf.reduce_mean(I_p_obs, axis=2)
dEp = tf.cast(tf.tensordot(u_p1, self.G1_real, axes=[[-1], [1]]) + tf.tensordot(u_p2, self.G2_real, axes=[[-1], [1]]), tf.complex128) + \
+ 1j * tf.cast(tf.tensordot(u_p1, self.G1_imag, axes=[[-1], [1]]) + tf.tensordot(u_p2, self.G2_imag, axes=[[-1], [1]]), tf.complex128)
d_contrast_p = tf.reduce_mean(tf.abs(dEp)**2, axis=2)
R = tf.tensordot(tf.expand_dims(self.R0 + self.R1*contrast_p + 4*(self.Q0+self.Q1*d_contrast_p)*contrast_p, axis=-1), tf.ones((1, self.num_pix), dtype=tf.float64), axes=[[-1], [0]]) + 1e-24
return R
def get_params(self):
# get the parameters of the identified system
return [self.G1_real, self.G1_imag, self.G2_real, self.G2_imag,
self.q0, self.q1, self.r0, self.r1]
def LSEnet(model, Ip, u1p, u2p):
# computation graph that defines least squared estimation of the electric field
delta_Ep_pred = tf.cast(tf.tensordot(u1p, model.G1_real, axes=[[-1], [1]]) + tf.tensordot(u2p, model.G2_real, axes=[[-1], [1]]), tf.complex128) + \
+ 1j * tf.cast(tf.tensordot(u1p, model.G1_imag, axes=[[-1], [1]]) + tf.tensordot(u2p, model.G2_imag, axes=[[-1], [1]]), tf.complex128)
delta_Ep_expand = tf.expand_dims(delta_Ep_pred, 2)
delta_Ep_expand_diff = delta_Ep_expand[:, 1::2, :, :] - delta_Ep_expand[:, 2::2, :, :]
y = tf.transpose(Ip[:, 1::2, :]-Ip[:, 2::2, :], [0, 2, 1])
H = tf.concat([2*tf.real(delta_Ep_expand_diff), 2*tf.imag(delta_Ep_expand_diff)], axis=2)
H = tf.transpose(H, [0, 3, 1, 2])
Ht_H = tf.matmul(tf.transpose(H, [0, 1, 3, 2]), H)
Ht_H_inv_Ht = tf.matmul(tf.matrix_inverse(Ht_H+tf.eye(2, dtype=tf.float64)*1e-12), tf.transpose(H, [0, 1, 3, 2]))
x_new = tf.squeeze(tf.matmul(Ht_H_inv_Ht, tf.expand_dims(y, -1)), -1)
n_observ = model.n_observ
contrast_p = tf.reduce_mean(Ip, axis=2)
d_contrast_p = tf.reduce_mean(tf.abs(delta_Ep_pred)**2, axis=2)
Rp = tf.tensordot(tf.expand_dims(model.R0 + model.R1*contrast_p + 4*(model.Q0+model.Q1*d_contrast_p)*contrast_p, axis=-1),
tf.ones((1, model.num_pix), dtype=tf.float64), axes=[[-1], [0]]) + 1e-24
Rp = tf.transpose(Rp, [0, 2, 1])
R_diff = Rp[:, :, 1::2]+Rp[:, :, 2::2]
R = tf.matrix_set_diag(tf.concat([tf.expand_dims(tf.zeros_like(R_diff), -1)]*(n_observ//2), -1), R_diff)
P_new = tf.matmul(tf.matmul(Ht_H_inv_Ht, R), tf.transpose(Ht_H_inv_Ht, [0, 1, 3, 2]))
Enp_pred_new = tf.cast(x_new[:, :, 0], dtype=tf.complex128) + 1j * tf.cast(x_new[:, :, 1], dtype=tf.complex128)
return Enp_pred_new, P_new, H
def KFnet(model, Ip, Enp_old, P_old, u1c, u2c, u1p, u2p):
# computation graph that defines Kalman filtering estimation of the electric field
delta_Ep_pred = tf.cast(tf.tensordot(u1p, model.G1_real, axes=[[-1], [1]]) + tf.tensordot(u2p, model.G2_real, axes=[[-1], [1]]), tf.complex128) + \
+ 1j * tf.cast(tf.tensordot(u1p, model.G1_imag, axes=[[-1], [1]]) + tf.tensordot(u2p, model.G2_imag, axes=[[-1], [1]]), tf.complex128)
delta_Ep_expand = tf.expand_dims(delta_Ep_pred, 2)
delta_Ep_expand_diff = delta_Ep_expand[:, 1::2, :, :] - delta_Ep_expand[:, 2::2, :, :]
y = tf.transpose(Ip[:, 1::2, :]-Ip[:, 2::2, :], [0, 2, 1])
H = tf.concat([2*tf.real(delta_Ep_expand_diff), 2*tf.imag(delta_Ep_expand_diff)], axis=2)
H = tf.transpose(H, [0, 3, 1, 2])
n_observ = model.n_observ
contrast_p = tf.reduce_mean(Ip, axis=2)
d_contrast_p = tf.reduce_mean(tf.abs(delta_Ep_pred)**2, axis=2)
Rp = tf.tensordot(tf.expand_dims(model.R0 + model.R1*contrast_p + 4*(model.Q0+model.Q1*d_contrast_p)*contrast_p, axis=-1),
tf.ones((1, model.num_pix), dtype=tf.float64), axes=[[-1], [0]]) + 1e-24
Rp = tf.transpose(Rp, [0, 2, 1])
R_diff = Rp[:, :, 1::2]+Rp[:, :, 2::2]
R = tf.matrix_set_diag(tf.concat([tf.expand_dims(tf.zeros_like(R_diff), -1)]*(n_observ//2), -1), R_diff)
Qco = model.transition_covariance(Enp_old, u1c, u2c)
Q = tf.concat([tf.expand_dims(Qco, 2), tf.expand_dims(Qco, 2)], axis=2)
Q = tf.matrix_set_diag(tf.concat([tf.expand_dims(tf.zeros_like(Q), -1)]*2, -1), Q)
# state and covariance prediction
Enp_pred = model.transition(Enp_old, u1c, u2c)
Enp_expand = tf.expand_dims(Enp_pred, 2)
x = tf.concat([tf.real(Enp_expand), tf.imag(Enp_expand)], axis=2)
P = P_old + Q
# state and covariance update
y_model = tf.squeeze(tf.matmul(H, tf.expand_dims(x, -1)), -1)
S = R + tf.matmul(tf.matmul(H, P), tf.transpose(H, [0, 1, 3, 2]))
K = tf.matmul(tf.matmul(P, tf.transpose(H, [0, 1, 3, 2])), tf.matrix_inverse(S))
x_new = x + tf.squeeze(tf.matmul(K, tf.expand_dims(y-y_model, -1)), -1)
P_new = P - tf.matmul(tf.matmul(K, H), P)
Enp_pred_new = tf.cast(x_new[:, :, 0], dtype=tf.complex128) + 1j * tf.cast(x_new[:, :, 1], dtype=tf.complex128)
return Enp_pred_new, P_new
class vl_net:
def __init__(self, Q0=1e-14, Q1=0.05, R0=1e-14, R1=1e-9,
path2data='/Users/ajriggs/Repos/falco-matlab/data/jac/'):
# load training data and biased Jacobian matrices
data_train = sio.loadmat( (path2data+'data_train.mat'))
# u1_train = data_train['data_train']['u1'][0, 0]
# u2_train = data_train['data_train']['u2'][0, 0]
# u1p_train = data_train['data_train']['u1p'][0, 0]
# u2p_train = data_train['data_train']['u2p'][0, 0]
# image_train = data_train['data_train']['I'][0, 0]
u1_train = data_train['u1']
u2_train = data_train['u2']
u1p_train = data_train['u1p']
u2p_train = data_train['u2p']
image_train = data_train['I']
# extract the parameters of the system
n_act = u1p_train.shape[0] # number of active actuators on the DM
n_pix = image_train.shape[0] # number of pixels in the dark hole
n_pair = u1p_train.shape[1]//2 # number of probing pairs in each control step
n_image = 2 * n_pair + 1 # number of probe images in each control step
# define the placeholders for the computation graph
u1c = tf.placeholder(tf.float64, shape=(None, n_act))
u2c = tf.placeholder(tf.float64, shape=(None, n_act))
u1p = tf.placeholder(tf.float64, shape=(None, n_image, n_act))
u2p = tf.placeholder(tf.float64, shape=(None, n_image, n_act))
Enp_old = tf.placeholder(tf.complex128, shape=(None, n_pix))
Ip = tf.placeholder(tf.float64, shape=(None, n_image, n_pix))
P_old = tf.placeholder(tf.float64, shape=(None, n_pix, 2, 2))
learning_rate = tf.placeholder(tf.float64, shape=())
learning_rate2 = tf.placeholder(tf.float64, shape=())
# define the optical model as a state space model (SSM) or a neural network
# parameters Q0, Q1, R0, R1, R2 define the noise covariance of the state space model
# process noises covariance: Q = Q0 + Q1 * sum(uc^2)
# observation noises covariance: R = R0 + R1 * probe_contrast + R2 * probe_contrast^2
Jacobian = sio.loadmat((path2data+'jacStruct.mat'))
# G1 = Jacobian['jacStruct']['G1'][0, 0]
# G2 = Jacobian['jacStruct']['G2'][0, 0]
G1 = np.squeeze(Jacobian['G1'])
G2 = np.squeeze(Jacobian['G2'])
model = SSM(G1, G2, Q0, Q1, R0, R1, n_image)
sio.savemat(path2data+'jacStructLearned.mat', {'G1': G1,
'G2': G2,
'noise_coef': np.squeeze(np.array([Q0, Q1, R0, R1]))})
# define the relations of the control/probe inputs, camera images and hidden electric fields
Enp_pred = model.transition(Enp_old, u1c, u2c)
Qco = model.transition_covariance(Enp_old, u1c, u2c)
Enp_est, P_est, H = LSEnet(model, Ip, u1p, u2p)
Enp_est2, P_est2, _ = LSEnet(model, Ip, u1p, u2p)
# Enp_est, P_est = KFnet(model, Ip, Enp_old, P_old, u1c, u2c, u1p, u2p)
_, Ip_pred = model.observation(Enp_est, u1p, u2p)
Ip_pred_err = tf.tile(tf.expand_dims(tf.trace(P_est), 1), [1, n_image, 1])
obs_cov = 4 * tf.tensordot(tf.expand_dims(tf.reduce_mean(Ip, -1), -1), tf.ones((1, n_pix), dtype=tf.float64), [-1, 0]) * tf.tile(tf.expand_dims(tf.trace(P_est), 1), [1, n_image, 1])
Rp = model.observation_covariance(Ip, u1p, u2p)
# Ip_pred_diff = Ip_pred[:, 1::2, :] - Ip_pred[:, 2::2, :]
# Ip_diff = Ip[:, 1::2, :] - Ip[:, 2::2, :]
# Rp_diff = Rp[:, 1::2, :] + Rp[:, 2::2, :]
# HPHt = tf.matmul(tf.matmul(H, P_est), tf.transpose(H, [0, 1, 3, 2]))
# obs_bias = tf.transpose(tf.linalg.diag_part(HPHt), [0, 2, 1])
# evidence lower bound (elbo): cost function for system identification
# we need to maximize the elbo for system ID
elbo = - tf.reduce_sum((tf.abs(Ip-Ip_pred-Ip_pred_err)**2 + obs_cov) / Rp) - tf.reduce_sum(tf.log(2*np.pi*Rp)) - \
(tf.reduce_sum(tf.abs(Enp_pred-Enp_est)**2 / Qco) + tf.reduce_sum(2 * tf.log(Qco)) - \
tf.reduce_sum(tf.linalg.logdet(P_est)) + tf.reduce_sum(tf.trace(P_est) / Qco))
# elbo = - tf.reduce_sum((tf.abs(Ip_diff-Ip_pred_diff)**2 + obs_bias) / Rp_diff) - tf.reduce_sum(tf.log(2*np.pi*Rp)) - \
# (tf.reduce_sum(tf.abs(Enp_pred-Enp_est)**2 / Qco) + tf.reduce_sum(2 * tf.log(Qco)) - \
# tf.reduce_sum(tf.linalg.logdet(P_est)) + tf.reduce_sum(tf.trace(P_est) / Qco))
# mean squared error (MSE): a metric for checking the system ID results
MSE = tf.reduce_sum(tf.abs(Ip - Ip_pred)**2)
# MSE = tf.reduce_sum(tf.abs(Ip_diff-Ip_pred_diff)**2)
params_list = model.get_params() # parameters to be identified
# start identifying/learning the model parameters
train_Jacobian = tf.train.AdamOptimizer(learning_rate=learning_rate,
beta1=0.99, beta2=0.9999, epsilon=1e-08).minimize(-elbo, var_list=params_list[0:4])
train_noise_coef = tf.train.AdamOptimizer(learning_rate=learning_rate2,
beta1=0.99, beta2=0.9999, epsilon=1e-08).minimize(-elbo, var_list=params_list[4::])
train_op = tf.group(train_Jacobian, train_noise_coef)
# train_op = tf.train.AdamOptimizer(learning_rate=learning_rate,
# beta1=0.99, beta2=0.9999, epsilon=1e-08).minimize(-elbo, var_list=params_list)
init = tf.global_variables_initializer()
self.model = model
self.Enp_est2 = Enp_est2
self.P_est2 = P_est2
self.Enp_est = Enp_est
self.P_est = P_est
self.Ip = Ip
self.u1p = u1p
self.u2p = u2p
self.u1c = u1c
self.u2c = u2c
self.Enp_old = Enp_old
self.P_old = P_old
self.learning_rate = learning_rate
self.learning_rate2 = learning_rate2
self.elbo = elbo
self.MSE = MSE
self.params_list = params_list
self.train_Jacobian = train_Jacobian
self.train_noise_coef = train_noise_coef
self.train_op = train_op
self.init = init
self.path2data = path2data
def linear_vl(net, lr=1e-7, lr2=1e-2, epoch=10, print_flag=False):
path2data = net.path2data
# load training data and biased Jacobian matrices
data_train = sio.loadmat( (path2data+'data_train.mat'))
# u1_train = data_train['data_train']['u1'][0, 0]
# u2_train = data_train['data_train']['u2'][0, 0]
# u1p_train = data_train['data_train']['u1p'][0, 0]
# u2p_train = data_train['data_train']['u2p'][0, 0]
# image_train = data_train['data_train']['I'][0, 0]
u1_train = data_train['u1']
u2_train = data_train['u2']
u1p_train = data_train['u1p']
u2p_train = data_train['u2p']
image_train = data_train['I']
Jacobian = sio.loadmat((path2data+'jacStructLearned.mat'))
G1 = Jacobian['G1']
G2 = Jacobian['G2']
noise_coef = np.squeeze(Jacobian['noise_coef'])
# extract the parameters of the system
n_step = u1_train.shape[1] # number of control steps
n_act = u1p_train.shape[0] # number of active actuators on the DM
n_pix = image_train.shape[0] # number of pixels in the dark hole
n_pair = u1p_train.shape[1]//2 # number of probing pairs in each control step
n_image = 2 * n_pair + 1 # number of probe images in each control step
u1p_train = np.concatenate([np.zeros((n_act, 1, n_step)), u1p_train], axis=1)
u2p_train = np.concatenate([np.zeros((n_act, 1, n_step)), u2p_train], axis=1)
mse_list = []
epoch = int(epoch)
with tf.Session() as sess:
sess.run(net.init)
net.model.G1_real.load(np.squeeze(G1).real)
net.model.G1_imag.load(np.squeeze(G1).imag)
net.model.G2_real.load(np.squeeze(G2).real)
net.model.G2_imag.load(np.squeeze(G2).imag)
net.model.q0.load(np.log(noise_coef[0]))
net.model.q1.load(np.log(noise_coef[1]))
net.model.r0.load(np.log(noise_coef[2]))
net.model.r1.load(np.log(noise_coef[3]))
# mse = sess.run(MSE, feed_dict={Ip: np.transpose(image_train, [2, 1, 0]),
# u1p: np.transpose(u1p_train, [2, 1, 0]),
# u2p: np.transpose(u2p_train, [2, 1, 0]),
# noises: np.zeros((n_step, n_pix, 2, mc_sampling))})
EEnp_est_values, P_est_values = sess.run([net.Enp_est, net.P_est], feed_dict={net.Ip: np.transpose(image_train, [2, 1, 0]),
net.u1p: np.transpose(u1p_train, [2, 1, 0]),
net.u2p: np.transpose(u2p_train, [2, 1, 0])})
mse = sess.run(net.MSE, feed_dict={net.Ip: np.transpose(image_train[:, :, 1:n_step], [2, 1, 0]),
net.u1p: np.transpose(u1p_train[:, :, 1:n_step], [2, 1, 0]),
net.u2p: np.transpose(u2p_train[:, :, 1:n_step], [2, 1, 0])})
mse_list.append(mse)
print('initial MSE: {}'.format(mse))
for k in range(epoch):
Enp_est_values, P_est_values = sess.run([net.Enp_est, net.P_est], feed_dict={net.Ip: np.transpose(image_train, [2, 1, 0]),
net.u1p: np.transpose(u1p_train, [2, 1, 0]),
net.u2p: np.transpose(u2p_train, [2, 1, 0])})
sess.run(net.train_op, feed_dict={net.Enp_old: Enp_est_values[0:n_step-1, :], net.P_old: P_est_values[0:n_step-1, :, :, :],
net.Ip: np.transpose(image_train[:, :, 1:n_step], [2, 1, 0]),
net.u1c: np.transpose(u1_train[:,0:n_step-1]), net.u2c: np.transpose(u2_train[:,0:n_step-1]),
net.u1p: np.transpose(u1p_train[:, :, 1:n_step], [2, 1, 0]), net.u2p: np.transpose(u2p_train[:, :, 1:n_step], [2, 1, 0]),
net.learning_rate: lr, net.learning_rate2: lr2})
mse = sess.run(net.MSE, feed_dict={net.Ip: np.transpose(image_train[:, :, 1:n_step], [2, 1, 0]),
net.u1p: np.transpose(u1p_train[:, :, 1:n_step], [2, 1, 0]),
net.u2p: np.transpose(u2p_train[:, :, 1:n_step], [2, 1, 0])})
# Enp_est_values0, P_est_values0 = sess.run([net.Enp_est2, net.P_est2], feed_dict={net.Ip: np.transpose(np.expand_dims(image_train[:, :, 0], -1), [2, 1, 0]),
# net.u1p: np.transpose(np.expand_dims(u1p_train[:, :, 0], -1), [2, 1, 0]),
# net.u2p: np.transpose(np.expand_dims(u2p_train[:, :, 0], -1), [2, 1, 0])})
# Enp_est_values[0, :] = np.squeeze(Enp_est_values0)
# P_est_values[0, :, :, :] = np.squeeze(P_est_values0)
# for i in range(1, n_step):
# Enp_est_values_now, P_est_values_now = sess.run([net.Enp_est, net.P_est], feed_dict={net.Ip: np.transpose(np.expand_dims(image_train[:, :, i], -1), [2, 1, 0]),
# net.u1p: np.transpose(np.expand_dims(u1p_train[:, :, i], -1), [2, 1, 0]),
# net.u2p: np.transpose(np.expand_dims(u2p_train[:, :, i], -1), [2, 1, 0]),
# net.u1c: np.transpose(np.expand_dims(u1_train[:,i-1], -1)),
# net.u2c: np.transpose(np.expand_dims(u2_train[:,i-1], -1)),
# net.Enp_old: np.expand_dims(Enp_est_values[i-1, :], 0),
# net.P_old: np.expand_dims(P_est_values[i-1, :, :, :], 0)})
# Enp_est_values[i, :] = np.squeeze(Enp_est_values_now)
# P_est_values[i, :, :, :] = np.squeeze(P_est_values_now)
# sess.run(net.train_op, feed_dict={net.Enp_old: Enp_est_values[0:n_step-1, :], P_old: P_est_values[0:n_step-1, :, :, :],
# net.Ip: np.transpose(image_train[:, :, 1:n_step], [2, 1, 0]),
# net.u1c: np.transpose(u1_train[:,0:n_step-1]), net.u2c: np.transpose(u2_train[:,0:n_step-1]),
# net.u1p: np.transpose(u1p_train[:, :, 1:n_step], [2, 1, 0]), net.u2p: np.transpose(u2p_train[:, :, 1:n_step], [2, 1, 0]),
# net.learning_rate: lr, net.learning_rate2: lr2})
# Enp_est_values, P_est_values = sess.run([Enp_est2, P_est2], feed_dict={Ip: np.transpose(image_train, [2, 1, 0]),
# u1p: np.transpose(u1p_train, [2, 1, 0]),
# u2p: np.transpose(u2p_train, [2, 1, 0])})
# mse = sess.run(net.MSE, feed_dict={net.Ip: np.transpose(image_train[:, :, 1:n_step], [2, 1, 0]),
# net.u1p: np.transpose(u1p_train[:, :, 1:n_step], [2, 1, 0]),
# net.u2p: np.transpose(u2p_train[:, :, 1:n_step], [2, 1, 0]),
# net.u1c: np.transpose(u1_train[:,0:n_step-1]),
# net.u2c: np.transpose(u2_train[:,0:n_step-1]),
# net.Enp_old: Enp_est_values[0:n_step-1, :],
# net.P_old: P_est_values[0:n_step-1, :, :, :]})
# if mse < mse_list[-1]:
# mse_list.append(mse)
# else:
# break
if print_flag:
print('epoch {} MSE: {}'.format(k, mse))
print('Q0: {}, Q1: {}, R0: {}, R1: {}'.format(sess.run(net.model.Q0), sess.run(net.model.Q1),
sess.run(net.model.R0), sess.run(net.model.R1)))
params_values_list = []
for param in net.params_list:
params_values_list.append(sess.run(param))
sio.savemat(path2data+'jacStructLearned.mat', {'G1': params_values_list[0]+1j*params_values_list[1],
'G2': params_values_list[2]+1j*params_values_list[3],
'noise_coef': np.squeeze(np.exp(np.array(params_values_list[-4::])))})
# if __name__ == "__main__":
# def linear_vl(Q0=1e-10, Q1=1e-7, R0=1e-14, R1=1e-9, R2=1e-2,
# lr=1e-7, lr2=1e-2, epoch=10, print_flag=False, path2data='/Users/ajriggs/Repos/falco-matlab/data/jac/'):
# # load training data and biased Jacobian matrices
# data_train = sio.loadmat( (path2data+'data_train.mat'))
# u1_train = data_train['data_train']['u1'][0, 0]
# u2_train = data_train['data_train']['u2'][0, 0]
# u1p_train = data_train['data_train']['u1p'][0, 0]
# u2p_train = data_train['data_train']['u2p'][0, 0]
# image_train = data_train['data_train']['I'][0, 0]
# Jacobian = sio.loadmat((path2data+'jacStruct.mat'))
# G1 = Jacobian['jacStruct']['G1'][0, 0]
# G2 = Jacobian['jacStruct']['G2'][0, 0]
# # extract the parameters of the system
# n_step = u1_train.shape[1] # number of control steps
# n_act = u1p_train.shape[0] # number of active actuators on the DM
# n_pix = image_train.shape[0] # number of pixels in the dark hole
# n_pair = u1p_train.shape[1]/2 # number of probing pairs in each control step
# n_image = 2 * n_pair + 1 # number of probe images in each control step
# u1p_train = np.concatenate([np.zeros((n_act, 1, n_step)), u1p_train], axis=1)
# u2p_train = np.concatenate([np.zeros((n_act, 1, n_step)), u2p_train], axis=1)
# # define the placeholders for the computation graph
# u1c = tf.placeholder(tf.float64, shape=(None, n_act))
# u2c = tf.placeholder(tf.float64, shape=(None, n_act))
# u1p = tf.placeholder(tf.float64, shape=(None, n_image, n_act))
# u2p = tf.placeholder(tf.float64, shape=(None, n_image, n_act))
# Enp_old = tf.placeholder(tf.complex128, shape=(None, n_pix))
# Ip = tf.placeholder(tf.float64, shape=(None, n_image, n_pix))
# P_old = tf.placeholder(tf.float64, shape=(None, n_pix, 2, 2))
# noises = tf.placeholder(tf.float64, shape=(None, n_pix, 2, mc_sampling))
# learning_rate = tf.placeholder(tf.float64, shape=())
# learning_rate2 = tf.placeholder(tf.float64, shape=())
# # define the optical model as a state space model (SSM) or a neural network
# # parameters Q0, Q1, R0, R1, R2 define the noise covariance of the state space model
# # process noises covariance: Q = Q0 + Q1 * sum(uc^2)
# # observation noises covariance: R = R0 + R1 * probe_contrast + R2 * probe_contrast^2
# model = SSM(G1, G2, Q0, Q1, R0, R1, R2, n_image)
# # define the relations of the control/probe inputs, camera images and hidden electric fields
# Enp_pred = model.transition(Enp_old, u1c, u2c)
# Qco = model.transition_covariance(Enp_old, u1c, u2c)
# Enp_est, P_est = LSEnet(model, Ip, u1p, u2p)
# Enp_est2, P_est2 = LSEnet(model, Ip, u1p, u2p)
# _, Ip_pred = model.observation(Enp_est, u1p, u2p)
# Ip_pred_err = tf.tile(tf.expand_dims(tf.trace(P_est), 1), [1, n_image, 1])
# obs_cov = 4 * Ip * tf.tile(tf.expand_dims(tf.trace(P_est), 1), [1, n_image, 1])
# Rp = model.observation_covariance(Ip, u1p, u2p)
# # evidence lower bound (elbo): cost function for system identification
# # we need to maximize the elbo for system ID
# elbo = - tf.reduce_sum((tf.abs(Ip-Ip_pred-Ip_pred_err)**2 + obs_cov) / Rp) - tf.reduce_sum(tf.log(2*np.pi*Rp)) - \
# (tf.reduce_sum(tf.abs(Enp_pred-Enp_est)**2 / Qco) + tf.reduce_sum(2 * tf.log(Qco)) - \
# tf.reduce_sum(tf.linalg.logdet(P_est)) + tf.reduce_sum(tf.trace(P_est) / Qco))
# # mean squared error (MSE): a metric for checking the system ID results
# MSE = tf.reduce_sum(tf.abs(Ip - Ip_pred)**2)
# params_list = model.get_params() # parameters to be identified
# # start identifying/learning the model parameters
# train_Jacobian = tf.train.AdamOptimizer(learning_rate=learning_rate,
# beta1=0.99, beta2=0.9999, epsilon=1e-08).minimize(-elbo, var_list=params_list[0:4])
# train_noise_coef = tf.train.AdamOptimizer(learning_rate=learning_rate2,
# beta1=0.99, beta2=0.9999, epsilon=1e-08).minimize(-elbo, var_list=params_list[4::])
# train_op = tf.group(train_Jacobian, train_noise_coef)
# # train_op = tf.train.AdamOptimizer(learning_rate=learning_rate,
# # beta1=0.99, beta2=0.9999, epsilon=1e-08).minimize(-elbo, var_list=params_list)
# init = tf.global_variables_initializer()
# mse_list = []
# epoch = int(epoch)
# with tf.Session() as sess:
# sess.run(init)
# # mse = sess.run(MSE, feed_dict={Ip: np.transpose(image_train, [2, 1, 0]),
# # u1p: np.transpose(u1p_train, [2, 1, 0]),
# # u2p: np.transpose(u2p_train, [2, 1, 0]),
# # noises: np.zeros((n_step, n_pix, 2, mc_sampling))})
# Enp_est_values, P_est_values = sess.run([Enp_est2, P_est2], feed_dict={Ip: np.transpose(image_train, [2, 1, 0]),
# u1p: np.transpose(u1p_train, [2, 1, 0]),
# u2p: np.transpose(u2p_train, [2, 1, 0])})
# mse = sess.run(MSE, feed_dict={Ip: np.transpose(image_train[:, :, 1:n_step], [2, 1, 0]),
# u1p: np.transpose(u1p_train[:, :, 1:n_step], [2, 1, 0]),
# u2p: np.transpose(u2p_train[:, :, 1:n_step], [2, 1, 0]),
# u1c: np.transpose(u1_train[:,0:n_step-1]),
# u2c: np.transpose(u2_train[:,0:n_step-1]),
# Enp_old: Enp_est_values[0:n_step-1, :],
# P_old: P_est_values[0:n_step-1, :, :, :],
# noises: np.zeros((n_step-1, n_pix, 2, mc_sampling))})
# mse_list.append(mse)
# print('initial MSE: {}'.format(mse))
# for k in range(epoch):
# # Enp_est_values = sess.run(Enp_est, feed_dict={Ip: np.transpose(image_train, [2, 1, 0]),
# # u1p: np.transpose(u1p_train, [2, 1, 0]),
# # u2p: np.transpose(u2p_train, [2, 1, 0])})
# # sess.run(train_op, feed_dict={noises: np.random.normal(size=(n_step-1, n_pix, 2, mc_sampling)),
# # Enp_old: Enp_est_values[0:n_step-1, :],
# # Ip: np.transpose(image_train[:, :, 1:n_step], [2, 1, 0]),
# # u1c: np.transpose(u1_train[:,0:n_step-1]), u2c: np.transpose(u2_train[:,0:n_step-1]),
# # u1p: np.transpose(u1p_train[:, :, 1:n_step], [2, 1, 0]), u2p: np.transpose(u2p_train[:, :, 1:n_step], [2, 1, 0]),
# # learning_rate: lr, learning_rate2: lr2})
# # mse = sess.run(MSE, feed_dict={Ip: np.transpose(image_train, [2, 1, 0]),
# # u1p: np.transpose(u1p_train, [2, 1, 0]),
# # u2p: np.transpose(u2p_train, [2, 1, 0]),
# # noises: np.zeros((n_step, n_pix, 2, mc_sampling))})
# Enp_est_values0, P_est_values0 = sess.run([Enp_est2, P_est2], feed_dict={Ip: np.transpose(np.expand_dims(image_train[:, :, 0], -1), [2, 1, 0]),
# u1p: np.transpose(np.expand_dims(u1p_train[:, :, 0], -1), [2, 1, 0]),
# u2p: np.transpose(np.expand_dims(u2p_train[:, :, 0], -1), [2, 1, 0])})
# Enp_est_values[0, :] = np.squeeze(Enp_est_values0)
# P_est_values[0, :, :, :] = np.squeeze(P_est_values0)
# for i in range(1, n_step):
# Enp_est_values_now, P_est_values_now = sess.run([Enp_est, P_est], feed_dict={Ip: np.transpose(np.expand_dims(image_train[:, :, i], -1), [2, 1, 0]),
# u1p: np.transpose(np.expand_dims(u1p_train[:, :, i], -1), [2, 1, 0]),
# u2p: np.transpose(np.expand_dims(u2p_train[:, :, i], -1), [2, 1, 0]),
# u1c: np.transpose(np.expand_dims(u1_train[:,i-1], -1)),
# u2c: np.transpose(np.expand_dims(u2_train[:,i-1], -1)),
# Enp_old: np.expand_dims(Enp_est_values[i-1, :], 0),
# P_old: np.expand_dims(P_est_values[i-1, :, :, :], 0),
# noises: np.zeros((1, n_pix, 2, mc_sampling))})
# Enp_est_values[i, :] = np.squeeze(Enp_est_values_now)
# P_est_values[i, :, :, :] = np.squeeze(P_est_values_now)
# sess.run(train_op, feed_dict={noises: np.random.normal(size=(n_step-1, n_pix, 2, mc_sampling)),
# Enp_old: Enp_est_values[0:n_step-1, :], P_old: P_est_values[0:n_step-1, :, :, :],
# Ip: np.transpose(image_train[:, :, 1:n_step], [2, 1, 0]),
# u1c: np.transpose(u1_train[:,0:n_step-1]), u2c: np.transpose(u2_train[:,0:n_step-1]),
# u1p: np.transpose(u1p_train[:, :, 1:n_step], [2, 1, 0]), u2p: np.transpose(u2p_train[:, :, 1:n_step], [2, 1, 0]),
# learning_rate: lr, learning_rate2: lr2})
# Enp_est_values, P_est_values = sess.run([Enp_est2, P_est2], feed_dict={Ip: np.transpose(image_train, [2, 1, 0]),
# u1p: np.transpose(u1p_train, [2, 1, 0]),
# u2p: np.transpose(u2p_train, [2, 1, 0])})
# mse = sess.run(MSE, feed_dict={Ip: np.transpose(image_train[:, :, 1:n_step], [2, 1, 0]),
# u1p: np.transpose(u1p_train[:, :, 1:n_step], [2, 1, 0]),
# u2p: np.transpose(u2p_train[:, :, 1:n_step], [2, 1, 0]),
# u1c: np.transpose(u1_train[:,0:n_step-1]),
# u2c: np.transpose(u2_train[:,0:n_step-1]),
# Enp_old: Enp_est_values[0:n_step-1, :],
# P_old: P_est_values[0:n_step-1, :, :, :],
# noises: np.zeros((n_step-1, n_pix, 2, mc_sampling))})
# if mse < mse_list[-1]:
# mse_list.append(mse)
# else:
# break
# if print_flag:
# print('epoch {} MSE: {}'.format(k, mse))
# # print('Q0: {}, Q1: {}, R0: {}, R1: {}, R2: {}'.format(sess.run(model.Q0), sess.run(model.Q1),
# # sess.run(model.R0), sess.run(model.R1), sess.run(model.R2)))
# params_values_list = []
# for param in params_list:
# params_values_list.append(sess.run(param))
# sio.savemat(path2data+'jacStructLearned.mat', {'G1': params_values_list[0]+1j*params_values_list[1],
# 'G2': params_values_list[2]+1j*params_values_list[3],
# 'noise_coef': np.array(params_values_list[-5::])})