-
Notifications
You must be signed in to change notification settings - Fork 0
/
olfactory_model.py
310 lines (276 loc) · 13.4 KB
/
olfactory_model.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
from olfactory_import import *
from olfactory_util import *
def negative_ll_A(x_true_trial,Kernel,sig_o,D,N):
T = x_true_trial.shape[0]
A = x_true_trial.T
a_cumsum = np.cumsum(A,1)
c1 = 1/np.sqrt(np.multiply(np.linspace(1,T-1,T-1), np.linspace(2,T,T-1)))
c1 = np.hstack((c1, 1/np.sqrt(T)))
c2 = np.sqrt(np.divide(np.linspace(1,T-1,T-1), np.linspace(2,T,T-1)))
c2 = np.hstack((0, c2))
a1 = np.multiply(np.tile(c1,(D*N,1)),a_cumsum)
a2 = np.roll(np.multiply(np.tile(c2,(D*N,1)),A),-1)
x_m = a1-a2
x_m_T_1 = x_m[:,:-1]
x_m_T = x_m[:,-1].reshape(N,D)
x1 = MultivariateNormalTriL(loc=tf.zeros((D*(T-1),N)), scale_tril=tf.cholesky(sig_o))
x2 = MultivariateNormalTriL(loc=tf.zeros((D,N)), scale_tril=tf.cholesky(Kernel*T+sig_o))
x1t = x_m_T_1.T.reshape(T-1,N,D).swapaxes(1,2).reshape(-1,N)
x2t = x_m_T.T
return x1, x2, x1t, x2t
def negative_ll_sign_B2(x_true_trial,Kernel,sig_o,sig_n,D,N):
T = x_true_trial.shape[0]
A = x_true_trial.T
a_cumsum = np.cumsum(A,1)
c1 = 1/np.sqrt(np.multiply(np.linspace(1,T-1,T-1), np.linspace(2,T,T-1)))
c1 = np.hstack((c1, 1/np.sqrt(T)))
c2 = np.sqrt(np.divide(np.linspace(1,T-1,T-1), np.linspace(2,T,T-1)))
c2 = np.hstack((0, c2))
a1 = np.multiply(np.tile(c1,(D*N,1)),a_cumsum)
a2 = np.roll(np.multiply(np.tile(c2,(D*N,1)),A),-1)
x_m = a1-a2
x_m_T_1 = x_m[:,:-1]
x_m_T = x_m[:,-1].reshape(-1,1)
x1 = MatrixNormal_Kron_fast_sample(loc=const(np.zeros([T-1, D*N])), cov_C=tf.eye(N)*0, cov_D=tf.eye(D)*0,
cov_A=sig_o, cov_B=sig_n)
x2 = MatrixNormal_Kron_fast_sample(loc=const(np.zeros([1, D*N])), cov_C=Kernel, cov_D=tf.eye(D)*T,
cov_A=sig_o, cov_B=sig_n)
x1t = x_m_T_1.T
x2t = x_m_T.T
return x1, x2, x1t, x2t
def pack_params(z_init_tr,sig_f1_z_est,l_z1_est,l_z_est,Bnest,Boest,l_B_nest,l_B_oest,sig_o1_z_est,sig_n1_z_est,model_flag):
if model_flag==1:
sig_o1_z = sig_o1_z_est
return np.hstack((z_init_tr.flatten(), sig_f1_z_est, l_z1_est, l_z_est, sig_o1_z))
if model_flag==2:
sig_n1_z = sig_n1_z_est
return np.hstack((z_init_tr.flatten(), sig_f1_z_est, l_z1_est, l_z_est, sig_n1_z))
if model_flag==3:
sig_o1_z = sig_o1_z_est
sig_n1_z = sig_n1_z_est
return np.hstack((z_init_tr.flatten(), sig_f1_z_est, l_z1_est, l_z_est, sig_o1_z, sig_n1_z))
if model_flag==4:
sig_o1_z = sig_o1_z_est
sig_n1_z = sig_n1_z_est
Bn = Bnest
l_B_n = l_B_nest
return np.hstack((z_init_tr.flatten(), sig_f1_z_est, l_z1_est, l_z_est, sig_o1_z, sig_n1_z, Bn.flatten(), l_B_n))
if model_flag==5:
sig_o1_z = sig_o1_z_est
sig_n1_z = sig_n1_z_est
Bo = Boest
l_B_o = l_B_oest
return np.hstack((z_init_tr.flatten(), sig_f1_z_est, l_z1_est, l_z_est, sig_o1_z, sig_n1_z, Bo.flatten(), l_B_o))
if model_flag==6:
sig_n1_z = sig_n1_z_est
sig_o1_z = sig_o1_z_est
Bo = Boest
l_B_o = l_B_oest
Bn = Bnest
l_B_n = l_B_nest
return np.hstack((z_init_tr.flatten(), sig_f1_z_est, l_z1_est, l_z_est, sig_o1_z, sig_n1_z, \
Bo.flatten(), l_B_o, Bn.flatten(), l_B_n))
if model_flag==7:
sig_o1_z = sig_o1_z_est
Bo = Boest
l_B_o = l_B_oest
return np.hstack((z_init_tr.flatten(), sig_f1_z_est, l_z1_est, l_z_est, sig_o1_z, Bo.flatten(), l_B_o))
if model_flag==8:
sig_n1_z = sig_n1_z_est
Bn = Bnest
l_B_n = l_B_nest
return np.hstack((z_init_tr.flatten(), sig_f1_z_est, l_z1_est, l_z_est, sig_n1_z, Bn.flatten(), l_B_n))
def unpack_params(params,N1_tr,D1,K,R,model_flag):
z_init_tr = params[:N1_tr*K].reshape(N1_tr,K)
sig_f = params[N1_tr*K:N1_tr*K+1].flatten()
lz1 = params[N1_tr*K+1:N1_tr*K+1+1].flatten()
lz = params[N1_tr*K+1+1:N1_tr*K+1+1+K].flatten()
ss = N1_tr*K+1+1+K
if model_flag==1:
sig_o1_z = params[ss:ss+N1_tr].flatten()
sig_n1_z = np.ones(D1)
Bo = np.zeros((N1_tr,R))
l_B_o = np.zeros(R)
Bn = np.zeros((D1,R))
l_B_n = np.zeros(R)
if model_flag==2:
sig_n1_z = params[ss:ss+D1].flatten()
sig_o1_z = np.ones(N1_tr)
Bo = np.zeros((N1_tr,R))
l_B_o = np.zeros(R)
Bn = np.zeros((D1,R))
l_B_n = np.zeros(R)
if model_flag==3:
sig_o1_z = params[ss:ss+N1_tr].flatten()
sig_n1_z = params[ss+N1_tr:ss+N1_tr+D1].flatten()
Bo = np.zeros((N1_tr,R))
l_B_o = np.zeros(R)
Bn = np.zeros((D1,R))
l_B_n = np.zeros(R)
if model_flag==4:
sig_o1_z = params[ss:ss+N1_tr].flatten()
sig_n1_z = params[ss+N1_tr:ss+N1_tr+D1].flatten()
Bn = params[ss+N1_tr+D1:ss+N1_tr+D1+R*D1].reshape(D1,R)
l_B_n = params[ss+N1_tr+D1+R*D1:ss+N1_tr+D1+R*D1+R].flatten()
Bo = np.zeros((N1_tr,R))
l_B_o = np.zeros(R)
if model_flag==5:
sig_o1_z = params[ss:ss+N1_tr].flatten()
sig_n1_z = params[ss+N1_tr:ss+N1_tr+D1].flatten()
Bo = params[ss+N1_tr+D1:ss+N1_tr+D1+R*N1_tr].reshape(N1_tr,R)
l_B_o = params[ss+N1_tr+D1+R*N1_tr:ss+N1_tr+D1+R*N1_tr+R].flatten()
Bn = np.zeros((D1,R))
l_B_n = np.zeros(R)
if model_flag==6:
sig_n1_z = params[ss+N1_tr:ss+N1_tr+D1].flatten()
sig_o1_z = params[ss:ss+N1_tr].flatten()
Bo = params[ss+N1_tr+D1:ss+N1_tr+D1+R*N1_tr].reshape(N1_tr,R)
l_B_o = params[ss+N1_tr+D1+R*N1_tr:ss+N1_tr+D1+R*N1_tr+R].flatten()
Bn = params[ss+N1_tr+D1+R*N1_tr+R:ss+N1_tr+D1+R*N1_tr+R+R*D1].reshape(D1,R)
l_B_n = params[ss+N1_tr+D1+R*N1_tr+R+R*D1:ss+N1_tr+D1+R*N1_tr+R+R*D1+R].flatten()
if model_flag==7:
sig_o1_z = params[ss:ss+N1_tr].flatten()
sig_n1_z = np.ones(D1)
Bo = params[ss+N1_tr:ss+N1_tr+R*N1_tr].reshape(N1_tr,R)
l_B_o = params[ss+N1_tr+R*N1_tr:ss+N1_tr+R*N1_tr+R].flatten()
Bn = np.zeros((D1,R))
l_B_n = np.zeros(R)
if model_flag==8:
sig_n1_z = params[ss:ss+D1].flatten()
sig_o1_z = np.ones(N1_tr)
Bn = params[ss+D1:ss+D1+R*D1].reshape(D1,R)
l_B_n = params[ss+D1+R*D1:ss+D1+R*D1+R].flatten()
Bo = np.zeros((N1_tr,R))
l_B_o = np.zeros(R)
return z_init_tr, sig_f, lz1, lz, sig_o1_z, sig_n1_z, Bo, l_B_o, Bn, l_B_n
def tmp(A,n,o):
T = A.shape[1]
a_cumsum = np.cumsum(A,1)
c1 = 1/np.sqrt(np.multiply(np.linspace(1,T-1,T-1), np.linspace(2,T,T-1)))
c1 = np.hstack((c1, 1/np.sqrt(T)))
c2 = np.sqrt(np.divide(np.linspace(1,T-1,T-1), np.linspace(2,T,T-1)))
c2 = np.hstack((0, c2))
a1 = np.multiply(np.tile(c1,(n*o,1)),a_cumsum)
a2 = np.roll(np.multiply(np.tile(c2,(n*o,1)),A),-1)
x_m = a1-a2
x_m_T_1 = x_m[:,:-1]
x_m_T = x_m[:,-1].reshape(-1,1)
return x_m_T_1, x_m_T
def negative_ll_tr(params,a1_est,b1_est,N1_tr,D1,T,K,R,model_flag,kernel_flag,trial_flag,x_true_avg1_train,x_true1_trial_train):
z_init_tr, sig_f, lz1, lz, sig_o1_z, sig_n1_z, Bo, l_B_o, Bn, l_B_n = unpack_params(params,N1_tr,D1,K,R,model_flag)
z1 = np.dot(z_init_tr,np.diag(np.sqrt(np.exp(lz))))
if kernel_flag=='rbf':
K_z1 = rbf_covariance(np.hstack((sig_f,lz1)),z1,z1)
elif kernel_flag=='linear':
K_z1 = np.dot(z1, z1.T)
elif kernel_flag=='mixture':
k_rbf1 = rbf_covariance(np.hstack((sig_f,lz1)),z1,z1)
k_linear1 = np.dot(z1, z1.T)
K_z1 = k_rbf1*b1_est+k_linear1*a1_est
# data likelihood
B_n = np.dot(np.dot(Bn,np.diag(np.exp(l_B_n))),Bn.T)+np.diag(np.exp(sig_n1_z))
B_o = np.dot(np.dot(Bo,np.diag(np.exp(l_B_o))),Bo.T)+np.diag(np.exp(sig_o1_z))
if trial_flag==0:
if model_flag==1 or model_flag==7:
Sigma = K_z1+B_o
Sigma_L = np.linalg.cholesky(Sigma)
Sigma_Linv = np.linalg.inv(Sigma_L)
Sigmainv = np.dot(Sigma_Linv.T, Sigma_Linv)
Lambda = np.dot(x_true_avg1_train, x_true_avg1_train.T)
ll = -0.5*np.trace(np.dot(Sigmainv, Lambda)) - 0.5*D1*logdet(Sigma_L)-0.5*np.trace(np.dot(z_init_tr.T, z_init_tr))
else:
loc = np.zeros((1,D1*N1_tr))
ll = np.sum(mvn_kron_autograd.matnorm_logp(x_true_avg1_train.reshape(1,-1),loc,\
cov_A=B_o,cov_B=B_n,cov_C=K_z1,cov_D=np.eye(D1)))
else:
x_m_T_1, x_m_T = tmp(x_true1_trial_train.T,D1,N1_tr)
if model_flag==1 or model_flag==7:
Sigma = B_o
Sigma_L = np.linalg.cholesky(Sigma)
Sigma_Linv = np.linalg.inv(Sigma_L)
Sigmainv = np.dot(Sigma_Linv.T, Sigma_Linv)
x1t = x_m_T_1.T.reshape(T-1,N1_tr,D1).swapaxes(1,2).reshape(-1,N1_tr)
Lambda = np.dot(x1t.T, x1t)
ll1 = -0.5*np.trace(np.dot(Sigmainv, Lambda)) - 0.5*D1*(T-1)*logdet(Sigma_L)
Sigma = K_z1*T+B_o
Sigma_L = np.linalg.cholesky(Sigma)
Sigma_Linv = np.linalg.inv(Sigma_L)
Sigmainv = np.dot(Sigma_Linv.T, Sigma_Linv)
Lambda = np.dot(x_m_T.reshape(N1_tr,D1), x_m_T.reshape(N1_tr,D1).T)
ll2 = -0.5*np.trace(np.dot(Sigmainv, Lambda)) - 0.5*D1*logdet(Sigma_L)
ll = ll1+ll2-0.5*np.trace(np.dot(z_init_tr.T, z_init_tr))
else:
loc = np.zeros((T-1,D1*N1_tr))
ll1 = np.sum(mvn_kron_autograd.matnorm_logp(x_m_T_1.T,loc,\
cov_A=B_o,cov_B=B_n,cov_C=np.eye(N1_tr)*0,cov_D=np.eye(D1)*0))
loc = np.zeros((1,D1*N1_tr))
ll2 = np.sum(mvn_kron_autograd.matnorm_logp(x_m_T.T,loc,\
cov_A=B_o,cov_B=B_n,cov_C=K_z1,cov_D=np.eye(D1)*T))
ll = ll1+ll2-0.5*np.trace(np.dot(z_init_tr.T, z_init_tr))
return -ll
def negative_ll_te(params,qz_tr_mean1,a1_est,b1_est,sig_f,lz1,lz,sig_o1_z,sig_n1_z,Bo,l_B_o,Bn,l_B_n,N1_tr,N1_te,D1,D1_va,T,K,R,model_flag,kernel_flag,trial_flag,x_true_avg1_test_va_all,x_true1_trial_test_va_all,n1ii):
z_init_te = params[:N1_te*K].reshape(N1_te,K)
z_all = np.vstack((qz_tr_mean1,z_init_te))
z1 = np.dot(z_all,np.diag(np.sqrt(np.exp(lz))))
if kernel_flag=='rbf':
K_z1 = rbf_covariance(np.hstack((sig_f,lz1)),z1,z1)
elif kernel_flag=='linear':
K_z1 = np.dot(z1, z1.T)
elif kernel_flag=='mixture':
k_rbf1 = rbf_covariance(np.hstack((sig_f,lz1)),z1,z1)
k_linear1 = np.dot(z1, z1.T)
K_z1 = k_rbf1*b1_est+k_linear1*a1_est
# data likelihood
B_n = np.dot(np.dot(Bn,np.diag(np.exp(l_B_n))),Bn.T)+np.diag(np.exp(sig_n1_z))
B_n1 = B_n[n1ii,:]
B_n1 = B_n1[:,n1ii]
B_o = np.dot(np.dot(Bo,np.diag(np.exp(l_B_o))),Bo.T)+np.diag(np.exp(sig_o1_z))
if model_flag==2 or model_flag==8:
sig_o1_z_va = np.zeros(N1_te)
else:
sig_o1_z_va = np.exp(params[-1])
sig_o1_z_all = np.hstack((sig_o1_z,sig_o1_z_va))
Bo_va = params[N1_te*K:N1_te*K+N1_te*R].reshape(N1_te,R)
Bo_all = np.vstack((Bo,Bo_va))
if model_flag==5 or model_flag==6 or model_flag==7:
B_o_all = np.dot(Bo_all,np.dot(np.diag(np.exp(l_B_o)),Bo_all.T))+np.diag(np.exp(sig_o1_z_all))
else:
B_o_all = np.diag(np.exp(sig_o1_z_all))
if trial_flag==0:
if model_flag==1 or model_flag==7:
Sigma = K_z1+B_o_all
Sigma_L = np.linalg.cholesky(Sigma)
Sigma_Linv = np.linalg.inv(Sigma_L)
Sigmainv = np.dot(Sigma_Linv.T, Sigma_Linv)
Lambda = np.dot(x_true_avg1_test_va_all, x_true_avg1_test_va_all.T)
ll = -0.5*np.trace(np.dot(Sigmainv, Lambda)) - 0.5*D1_va*logdet(Sigma_L)-0.5*np.trace(np.dot(z_all.T, z_all))
else:
loc = np.zeros((1,D1_va*(N1_tr+N1_te)))
ll = np.sum(mvn_kron_autograd.matnorm_logp(x_true_avg1_test_va_all.reshape(1,-1),loc,\
cov_A=B_o_all,cov_B=B_n1,cov_C=K_z1,cov_D=np.eye(D1_va)))
else:
x_m_T_1, x_m_T = tmp(x_true1_trial_test_va_all.T,D1_va,N1_tr+N1_te)
if model_flag==1 or model_flag==7:
Sigma = B_o_all
Sigma_L = np.linalg.cholesky(Sigma)
Sigma_Linv = np.linalg.inv(Sigma_L)
Sigmainv = np.dot(Sigma_Linv.T, Sigma_Linv)
x1t = x_m_T_1.T.reshape(T-1,N1_tr+N1_te,D1_va).swapaxes(1,2).reshape(-1,N1_tr+N1_te)
Lambda = np.dot(x1t.T, x1t)
ll1 = -0.5*np.trace(np.dot(Sigmainv, Lambda)) - 0.5*D1_va*(T-1)*logdet(Sigma_L)
Sigma = K_z1*T+B_o_all
Sigma_L = np.linalg.cholesky(Sigma)
Sigma_Linv = np.linalg.inv(Sigma_L)
Sigmainv = np.dot(Sigma_Linv.T, Sigma_Linv)
Lambda = np.dot(x_m_T.reshape(N1_tr+N1_te,D1_va), x_m_T.reshape(N1_tr+N1_te,D1_va).T)
ll2 = -0.5*np.trace(np.dot(Sigmainv, Lambda)) - 0.5*D1_va*logdet(Sigma_L)
ll = ll1+ll2-0.5*np.trace(np.dot(z_all.T, z_all))
else:
loc = np.zeros((T-1,D1_va*(N1_tr+N1_te)))
ll1 = np.sum(mvn_kron_autograd.matnorm_logp(x_m_T_1.T,loc,cov_A=B_o_all,cov_B=B_n1,\
cov_C=np.eye(N1_tr+N1_te)*0,cov_D=np.eye(D1_va)*0))
loc = np.zeros((1,D1_va*(N1_tr+N1_te)))
ll2 = np.sum(mvn_kron_autograd.matnorm_logp(x_m_T.T,loc,\
cov_A=B_o_all,cov_B=B_n1,cov_C=K_z1,cov_D=np.eye(D1_va)*T))
ll = ll1+ll2-0.5*np.trace(np.dot(z_all.T, z_all))
return -ll