Repository navigation
Expand file tree
/
Copy pathpytorch_stats_model_multi_dim.py
More file actions
executable file
·564 lines (402 loc) · 20 KB
/
Copy pathpytorch_stats_model_multi_dim.py
File metadata and controls
executable file
·564 lines (402 loc) · 20 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
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
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 8 16:45:52 2022
statistical models for fitting single-trial neuronal population response fom one brain area to multiple stimuli
these models are designed to capture trial-to-trial variability in the neuronal population
@author: xiaji
"""
import torch
import numpy as np
from sklearn.decomposition import FactorAnalysis
dev = "cpu"
device = torch.device(dev)
####################################
class multiplicative_model():
def __init__(self,x, n, n_stim, n_trial, n_compo, alpha_p_init, psi_p_init):
self.n = n
self.n_stim = n_stim
self.n_trial = n_trial
self.x = x
self.n_compo = n_compo
self.SMALL = 1e-5
#initialize params
d_p = np.zeros((n, n_stim))
alpha_p = alpha_p_init
psi_p = np.maximum(psi_p_init, self.SMALL)
for stim_i in range(n_stim):
x_tmp = x[:, stim_i*n_trial:(stim_i+1)*n_trial]
d_p[:, stim_i] = np.mean(x_tmp, axis=1)
d_p = torch.from_numpy(d_p).to(device)
alpha_p = torch.from_numpy(alpha_p).to(device)
alpha_p.requires_grad=True
psi_p = torch.from_numpy(psi_p).to(device)
psi_p.requires_grad=True
self.d_p = d_p
self.alpha_p = alpha_p
self.psi_p = psi_p
def loss_nll(self, x, n_trial):
#calculating negative log likelihood of the model given data x
x_var = torch.from_numpy(x)
x_var = x_var.to(device)
NLL = 0
for stim_i in range(self.n_stim):
d_s = self.d_p[:, stim_i]
psi_s = torch.maximum(self.psi_p[:, stim_i], torch.tensor(self.SMALL))
A = self.alpha_p*d_s[:,None]
cov = A@A.T + torch.diag(psi_s)
to_learn = torch.distributions.multivariate_normal.MultivariateNormal(loc=d_s, covariance_matrix= cov)
NLL += -torch.mean(to_learn.log_prob(x_var[:, stim_i*n_trial:(stim_i+1)*n_trial].T))
return NLL/self.n_stim
def recon_data(self, x, n_trial):
#x_recon: reconstructed data x from the multiplicative model
#E_z: the posterior (P(z|x)) expectation of latent variable z
x_var = torch.from_numpy(x)
x_var = x_var.to(device)
E_z = torch.zeros([self.n_compo, self.n_stim*n_trial]).double()
x_recon = torch.zeros([self.n, self.n_stim*n_trial])
for stim_i in range(self.n_stim):
d_s = self.d_p[:, stim_i]
psi_s = torch.maximum(self.psi_p[:, stim_i], torch.tensor(self.SMALL))
x_s = x_var[:, stim_i*n_trial:(stim_i+1)*n_trial]
A = self.alpha_p*d_s[:, None]
G = torch.linalg.inv(torch.eye(self.n_compo) + A.T@torch.diag(1/psi_s)@A)
E_z[:, stim_i*n_trial: (stim_i+1)*n_trial] = G@A.T@torch.diag(1/psi_s)@(x_s - d_s[:, None])
x_recon[:, stim_i*n_trial: (stim_i+1)*n_trial] = d_s[:, None] + A@E_z[:, stim_i*n_trial: (stim_i+1)*n_trial]
return x_recon, E_z
def train(self, lr0, x_test, n_trial_test):
#train parameters with gradient descent to minimize the negative log likelihood
optimizer = torch.optim.Adam([self.alpha_p, self.psi_p], lr0)
decayRate = 0.96
lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer=optimizer, gamma=decayRate)
NLL_old = self.loss_nll(x_test, n_trial_test)
for t in range(20001):
optimizer.zero_grad()
NLL = self.loss_nll(self.x, self.n_trial)
NLL_test = self.loss_nll(x_test, n_trial_test)
NLL.backward()
optimizer.step()
if (t-1) % 500 == 0:
print(f"Iteration: {t}, Loss: {NLL.item():0.2f}, test Loss: {NLL_test.item():0.2f}")
if NLL_test > (NLL_old-1e-5):
print(f"Stop: Iteration: {t}, old test Loss: {NLL_old.item():0.5f}, new test Loss: {NLL_test.item():0.5f}")
break
else:
NLL_old = NLL_test
lr_scheduler.step()
print('learning rate: ', lr_scheduler.get_last_lr())
return self.d_p, self.alpha_p, self.psi_p
#################################
class affine_model():
def __init__(self,x, n, n_stim, n_trial, n_compo, alpha_p_init, beta_p_init, psi_p_init):
self.n = n
self.n_stim = n_stim
self.n_trial = n_trial
self.x = x
self.n_compo = n_compo
self.SMALL = 1e-5
#initialize params
d_p = np.zeros((n, n_stim))
alpha_p = alpha_p_init
beta_p = beta_p_init
psi_p = np.maximum(psi_p_init, self.SMALL)
for stim_i in range(n_stim):
x_tmp = x[:, stim_i*n_trial:(stim_i+1)*n_trial]
d_p[:, stim_i] = np.mean(x_tmp, axis=1)
d_p = torch.from_numpy(d_p).to(device)
alpha_p = torch.from_numpy(alpha_p).to(device)
alpha_p.requires_grad=True
beta_p = torch.from_numpy(beta_p).to(device)
beta_p.requires_grad=True
psi_p = torch.from_numpy(psi_p).to(device)
psi_p.requires_grad=True
self.d_p = d_p
self.alpha_p = alpha_p
self.beta_p = beta_p
self.psi_p = psi_p
def loss_nll(self, x, n_trial):
x_var = torch.from_numpy(x)
x_var = x_var.to(device)
NLL = 0
for stim_i in range(self.n_stim):
d_s = self.d_p[:, stim_i]
psi_s = torch.maximum(self.psi_p[:, stim_i], torch.tensor(self.SMALL))
A = self.alpha_p*d_s[:,None] + self.beta_p
cov = A@A.T + torch.diag(psi_s)
to_learn = torch.distributions.multivariate_normal.MultivariateNormal(loc=d_s, covariance_matrix= cov)
NLL += -torch.mean(to_learn.log_prob(x_var[:, stim_i*n_trial:(stim_i+1)*n_trial].T))
return NLL/self.n_stim
def recon_data(self, x, n_trial):
x_var = torch.from_numpy(x)
x_var = x_var.to(device)
E_z = torch.zeros([self.n_compo, self.n_stim*n_trial]).double()
x_recon = torch.zeros([self.n, self.n_stim*n_trial])
for stim_i in range(self.n_stim):
d_s = self.d_p[:, stim_i]
psi_s = torch.maximum(self.psi_p[:, stim_i], torch.tensor(self.SMALL))
x_s = x_var[:, stim_i*n_trial:(stim_i+1)*n_trial]
A = self.alpha_p*d_s[:, None] + self.beta_p
G = torch.linalg.inv(torch.eye(self.n_compo) + A.T@torch.diag(1/psi_s)@A)
E_z[:, stim_i*n_trial: (stim_i+1)*n_trial] = G@A.T@torch.diag(1/psi_s)@(x_s - d_s[:, None])
x_recon[:, stim_i*n_trial: (stim_i+1)*n_trial] = d_s[:, None] + A@E_z[:, stim_i*n_trial: (stim_i+1)*n_trial]
return x_recon, E_z
def train(self, lr0, x_test, n_trial_test):
optimizer = torch.optim.Adam([self.alpha_p, self.beta_p, self.psi_p], lr0)
decayRate = 0.96
lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer=optimizer, gamma=decayRate)
NLL_old = self.loss_nll(x_test, n_trial_test)
for t in range(20001):
optimizer.zero_grad()
NLL = self.loss_nll(self.x, self.n_trial)
NLL_test = self.loss_nll(x_test, n_trial_test)
NLL.backward()
optimizer.step()
if (t-1) % 500 == 0:
print(f"Iteration: {t}, Loss: {NLL.item():0.2f}, test Loss: {NLL_test.item():0.2f}")
if NLL_test > (NLL_old-1e-5):
print(f"Stop: Iteration: {t}, old test Loss: {NLL_old.item():0.5f}, new test Loss: {NLL_test.item():0.5f}")
break
else:
NLL_old = NLL_test
lr_scheduler.step()
print('learning rate: ', lr_scheduler.get_last_lr())
return self.d_p, self.alpha_p, self.beta_p, self.psi_p
#####################################################
class additive_varp_model():
'''
This is the additive model used in Xia et al 2023
'''
def __init__(self,x, n, n_stim, n_trial, n_compo, h_p_init, psi_p_init):
self.n = n
self.n_stim = n_stim
self.n_trial = n_trial
self.x = x
self.n_compo = n_compo
self.SMALL = 1e-5
#initialize params
d_p = np.zeros((n, n_stim))
for stim_i in range(n_stim):
x_tmp = x[:, stim_i*n_trial:(stim_i+1)*n_trial]
d_p[:, stim_i] = np.mean(x_tmp, axis=1)
h_p = h_p_init
psi_p = np.maximum(psi_p_init, self.SMALL)
d_p = torch.from_numpy(d_p).to(device)
h_p = torch.from_numpy(h_p).to(device)
h_p.requires_grad=True
psi_p = torch.from_numpy(psi_p).to(device)
psi_p.requires_grad=True
self.d_p = d_p
self.h_p = h_p
self.psi_p = psi_p
def loss_nll(self, x, n_trial):
x_var = torch.from_numpy(x)
x_var = x_var.to(device)
NLL = 0
for stim_i in range(self.n_stim):
d_s = self.d_p[:, stim_i]
psi_s = torch.maximum(self.psi_p[:, stim_i], torch.tensor(self.SMALL))
A = self.h_p
cov = A@A.T + torch.diag(psi_s)
to_learn = torch.distributions.multivariate_normal.MultivariateNormal(loc=d_s, covariance_matrix= cov)
NLL += -torch.mean(to_learn.log_prob(x_var[:, stim_i*n_trial:(stim_i+1)*n_trial].T))
return NLL/self.n_stim
def recon_data(self, x, n_trial):
x_var = torch.from_numpy(x)
x_var = x_var.to(device)
E_z = torch.zeros([self.n_compo, self.n_stim*n_trial]).double()
x_recon = torch.zeros([self.n, self.n_stim*n_trial])
for stim_i in range(self.n_stim):
d_s = self.d_p[:, stim_i]
psi_s = torch.maximum(self.psi_p[:, stim_i], torch.tensor(self.SMALL))
x_s = x_var[:, stim_i*n_trial:(stim_i+1)*n_trial]
A = self.h_p
G = torch.linalg.inv(torch.eye(self.n_compo) + A.T@torch.diag(1/psi_s)@A)
E_z[:, stim_i*n_trial: (stim_i+1)*n_trial] = G@A.T@torch.diag(1/psi_s)@(x_s - d_s[:, None])
x_recon[:, stim_i*n_trial: (stim_i+1)*n_trial] = d_s[:, None] + A@E_z[:, stim_i*n_trial: (stim_i+1)*n_trial]
return x_recon, E_z
def train(self, lr0, x_test, n_trial_test):
optimizer = torch.optim.Adam([self.h_p, self.psi_p], lr0)
decayRate = 0.96
lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer=optimizer, gamma=decayRate)
NLL_old = self.loss_nll(x_test, n_trial_test)
for t in range(20001):
optimizer.zero_grad()
NLL = self.loss_nll(self.x, self.n_trial)
NLL_test = self.loss_nll(x_test, n_trial_test)
NLL.backward()
optimizer.step()
if (t-1) % 500 == 0:
print(f"Iteration: {t}, Loss: {NLL.item():0.2f}, test Loss: {NLL_test.item():0.2f}")
if NLL_test > (NLL_old-1e-5):
print(f"Stop: Iteration: {t}, old test Loss: {NLL_old.item():0.5f}, new test Loss: {NLL_test.item():0.5f}")
break
else:
NLL_old = NLL_test
lr_scheduler.step()
print('learning rate: ', lr_scheduler.get_last_lr())
return self.d_p, self.h_p, self.psi_p
##########################################################
class generalized_model():
def __init__(self,x, n, n_stim, n_trial, x_test, n_trial_test, n_compo):
#initialize params
d_p = np.zeros((n, n_stim))
psi_p = np.ones((n, n_stim))
F_p = np.zeros((n, n_compo, n_stim))
z = np.zeros((n_stim*n_trial, n_compo))
z_test = np.zeros((n_stim*n_trial_test, n_compo))
x_recon = np.zeros_like(x, dtype=float)
x_test_recon = np.zeros_like(x_test, dtype=float)
ll = 0
ll_test = 0
for stim_i in range(n_stim):
x_tmp = x[:, stim_i*n_trial:(stim_i+1)*n_trial]
d_p[:, stim_i] = np.mean(x_tmp, axis=1)
res_x = x_tmp - d_p[:, stim_i:stim_i+1]
fa = FactorAnalysis()
fa.n_components = n_compo
fa.fit(res_x.T)
F_p[:,:, stim_i] = fa.components_.T
psi_p[:,stim_i] = fa.noise_variance_
ll += fa.score(res_x.T)
x_test_tmp = x_test[:, stim_i*n_trial_test:(stim_i+1)*n_trial_test]
res_x_test = x_test_tmp - d_p[:, stim_i:stim_i+1]
ll_test += fa.score(res_x_test.T)
z[stim_i*n_trial:(stim_i+1)*n_trial, :] = fa.transform(res_x.T)
z_test[stim_i*n_trial_test:(stim_i+1)*n_trial_test, :] = fa.transform(res_x_test.T)
x_recon[:, stim_i*n_trial:(stim_i+1)*n_trial] = d_p[:, stim_i:stim_i+1] + F_p[:,:, stim_i] @ (z[stim_i*n_trial:(stim_i+1)*n_trial, :].T)
x_test_recon[:, stim_i*n_trial_test:(stim_i+1)*n_trial_test] = d_p[:, stim_i:stim_i+1] + F_p[:,:,stim_i] @ (z_test[stim_i*n_trial_test:(stim_i+1)*n_trial_test, :].T)
self.d_p = d_p
self.psi_p = psi_p
self.F_p = F_p
self.z = z
self.z_test = z_test
self.NLL = -ll/n_stim
self.NLL_test = -ll_test/n_stim
print('test NLL: ', self.NLL_test, 'train NLL: ', self.NLL)
self.x_recon = x_recon
self.x_test_recon = x_test_recon
###################################################
class additive_model():
'''
This is NOT the additive model used in Xia et al.
This additive model assumes stimulus-independent private variability for each neuron
'''
def __init__(self,x, n, n_stim, n_trial, x_test, n_trial_test, n_compo):
#initialize params
d_p = np.zeros((n, n_stim))
z = np.zeros((n_stim*n_trial, n_compo))
z_test = np.zeros((n_stim*n_trial_test, n_compo))
x_recon = np.zeros_like(x, dtype=float)
x_test_recon = np.zeros_like(x_test, dtype=float)
ll = 0
ll_test = 0
res_x = np.zeros_like(x)
res_x_test = np.zeros_like(x_test)
for stim_i in range(n_stim):
x_tmp = x[:, stim_i*n_trial:(stim_i+1)*n_trial]
d_p[:, stim_i] = np.mean(x_tmp, axis=1)
res_x[:, stim_i*n_trial:(stim_i+1)*n_trial] = x_tmp - d_p[:, stim_i:stim_i+1]
x_test_tmp = x_test[:, stim_i*n_trial_test:(stim_i+1)*n_trial_test]
res_x_test[:, stim_i*n_trial_test:(stim_i+1)*n_trial_test] = x_test_tmp - d_p[:, stim_i:stim_i+1]
fa = FactorAnalysis()
fa.n_components = n_compo
fa.fit(res_x.T)
h_p = fa.components_.T #h_p is n x n_compo
psi_p = fa.noise_variance_
ll = fa.score(res_x.T)
ll_test = fa.score(res_x_test.T)
z = fa.transform(res_x.T) # z is n_samples x n_compo
z_test = fa.transform(res_x_test.T)
for stim_i in range(n_stim):
x_recon[:, stim_i*n_trial:(stim_i+1)*n_trial] = d_p[:, stim_i:stim_i+1] + h_p @ (z[stim_i*n_trial:(stim_i+1)*n_trial, :].T)
x_test_recon[:, stim_i*n_trial_test:(stim_i+1)*n_trial_test] = d_p[:, stim_i:stim_i+1] + h_p @ (z_test[stim_i*n_trial_test:(stim_i+1)*n_trial_test, :].T)
self.d_p = d_p
self.psi_p = psi_p
self.h_p = h_p
self.z = z
self.z_test = z_test
self.NLL = -ll
self.NLL_test = -ll_test
print('test NLL: ', self.NLL_test, 'train NLL: ', self.NLL)
self.x_recon = x_recon
self.x_test_recon = x_test_recon
class exponent_model():
'''
this model is not used in Xia et al 2023
'''
def __init__(self,x, n, n_stim, n_trial, n_compo, expo_p_init, beta_p_init, psi_p_init):
self.n = n
self.n_stim = n_stim
self.n_trial = n_trial
self.x = x
self.n_compo = n_compo
self.SMALL = 1e-5
#initialize params
d_p = np.zeros((n, n_stim))
alpha_p = np.zeros((n,n_compo))
beta_p = beta_p_init
psi_p = np.maximum(psi_p_init, self.SMALL)
expo_p = expo_p_init
for stim_i in range(n_stim):
x_tmp = x[:, stim_i*n_trial:(stim_i+1)*n_trial]
d_p[:, stim_i] = np.mean(x_tmp, axis=1)
d_p = torch.from_numpy(d_p).to(device)
alpha_p = torch.from_numpy(alpha_p).to(device)
alpha_p.requires_grad=True
beta_p = torch.from_numpy(beta_p).to(device)
beta_p.requires_grad=True
psi_p = torch.from_numpy(psi_p).to(device)
psi_p.requires_grad=True
expo_p = torch.tensor(expo_p).to(device)
expo_p.requires_grad=True
self.d_p = d_p
self.alpha_p = alpha_p
self.beta_p = beta_p
self.psi_p = psi_p
self.expo_p = expo_p
def loss_nll(self, x, n_trial):
x_var = torch.from_numpy(x)
x_var = x_var.to(device)
NLL = 0
for stim_i in range(self.n_stim):
d_s = self.d_p[:, stim_i]
psi_s = torch.maximum(self.psi_p[:, stim_i], torch.tensor(self.SMALL))
A = self.alpha_p*(d_s[:,None]**self.expo_p) + self.beta_p
cov = A@A.T + torch.diag(psi_s)
to_learn = torch.distributions.multivariate_normal.MultivariateNormal(loc=d_s, covariance_matrix= cov)
NLL += -torch.mean(to_learn.log_prob(x_var[:, stim_i*n_trial:(stim_i+1)*n_trial].T))
return NLL/self.n_stim
def recon_data(self, x, n_trial):
x_var = torch.from_numpy(x)
x_var = x_var.to(device)
E_z = torch.zeros([self.n_compo, self.n_stim*n_trial]).double()
x_recon = torch.zeros([self.n, self.n_stim*n_trial])
for stim_i in range(self.n_stim):
d_s = self.d_p[:, stim_i]
psi_s = torch.maximum(self.psi_p[:, stim_i], torch.tensor(self.SMALL))
x_s = x_var[:, stim_i*n_trial:(stim_i+1)*n_trial]
A = self.alpha_p*(d_s[:, None]**self.expo_p) + self.beta_p
G = torch.linalg.inv(torch.eye(self.n_compo) + A.T@torch.diag(1/psi_s)@A)
E_z[:, stim_i*n_trial: (stim_i+1)*n_trial] = G@A.T@torch.diag(1/psi_s)@(x_s - d_s[:, None])
x_recon[:, stim_i*n_trial: (stim_i+1)*n_trial] = d_s[:, None] + A@E_z[:, stim_i*n_trial: (stim_i+1)*n_trial]
return x_recon, E_z
def train(self, lr0, x_test, n_trial_test):
optimizer = torch.optim.Adam([self.alpha_p, self.beta_p, self.psi_p, self.expo_p], lr0)
decayRate = 0.96
lr_scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer=optimizer, gamma=decayRate)
NLL_old = self.loss_nll(x_test, n_trial_test)
for t in range(20001):
optimizer.zero_grad()
NLL = self.loss_nll(self.x, self.n_trial)
NLL_test = self.loss_nll(x_test, n_trial_test)
NLL.backward()
optimizer.step()
if (t-1) % 500 == 0:
print(f"Iteration: {t}, Loss: {NLL.item():0.2f}, test Loss: {NLL_test.item():0.2f}")
if NLL_test > (NLL_old-1e-5):
print(f"Stop: Iteration: {t}, old test Loss: {NLL_old.item():0.5f}, new test Loss: {NLL_test.item():0.5f}")
break
else:
NLL_old = NLL_test
lr_scheduler.step()
print('learning rate: ', lr_scheduler.get_last_lr())
return self.d_p, self.alpha_p, self.beta_p, self.psi_p, self.expo_p