-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathivampnets.py
1303 lines (1122 loc) · 54.5 KB
/
ivampnets.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from typing import Optional, Union, Callable, Tuple, List
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import Dataset
from itertools import chain
from deeptime.base import Model, Transformer
from deeptime.base_torch import DLEstimatorMixin
from deeptime.util.torch import map_data
from deeptime.markov.tools.analysis import pcca_memberships
CLIP_VALUE = 1.
def _inv(x, return_sqrt=False, epsilon=1e-6):
'''Utility function that returns the inverse of a matrix, with the
option to return the square root of the inverse matrix.
Parameters
----------
x: numpy array with shape [m,m]
matrix to be inverted
ret_sqrt: bool, optional, default = False
if True, the square root of the inverse matrix is returned instead
Returns
-------
x_inv: numpy array with shape [m,m]
inverse of the original matrix
'''
# Calculate eigvalues and eigvectors
eigval_all, eigvec_all = torch.symeig(x, eigenvectors=True)
# eigval_all, eigvec_all = torch.linalg.eigh(x, UPLO='U')
# Filter out eigvalues below threshold and corresponding eigvectors
# eig_th = torch.Tensor(epsilon)
index_eig = eigval_all > epsilon
# print(index_eig)
eigval = eigval_all[index_eig]
eigvec = eigvec_all[:,index_eig]
# Build the diagonal matrix with the filtered eigenvalues or square
# root of the filtered eigenvalues according to the parameter
if return_sqrt:
diag = torch.diag(torch.sqrt(1/eigval))
else:
diag = torch.diag(1/eigval)
# print(diag.shape, eigvec.shape)
# Rebuild the square root of the inverse matrix
x_inv = torch.matmul(eigvec, torch.matmul(diag, eigvec.T))
return x_inv
def symeig_reg(mat, epsilon: float = 1e-6, mode='regularize', eigenvectors=True) \
-> Tuple[torch.Tensor, Optional[torch.Tensor]]:
r""" Solves a eigenvector/eigenvalue decomposition for a hermetian matrix also if it is rank deficient.
Parameters
----------
mat : torch.Tensor
the hermetian matrix
epsilon : float, default=1e-6
Cutoff for eigenvalues.
mode : str, default='regularize'
Whether to truncate eigenvalues if they are too small or to regularize them by taking the absolute value
and adding a small positive constant. :code:`trunc` leads to truncation, :code:`regularize` leads to epsilon
being added to the eigenvalues after taking the absolute value
eigenvectors : bool, default=True
Whether to compute eigenvectors.
Returns
-------
(eigval, eigvec) : Tuple[torch.Tensor, Optional[torch.Tensor]]
Eigenvalues and -vectors.
"""
assert mode in sym_inverse.valid_modes, f"Invalid mode {mode}, supported are {sym_inverse.valid_modes}"
if mode == 'regularize':
identity = torch.eye(mat.shape[0], dtype=mat.dtype, device=mat.device)
mat = mat + epsilon * identity
# Calculate eigvalues and potentially eigvectors
eigval, eigvec = torch.symeig(mat, eigenvectors=True)
# eigval, eigvec = torch.linalg.eigh(mat, UPLO='U')
if eigenvectors:
eigvec = eigvec.transpose(0, 1)
if mode == 'trunc':
# Filter out Eigenvalues below threshold and corresponding Eigenvectors
mask = eigval > epsilon
eigval = eigval[mask]
if eigenvectors:
eigvec = eigvec[mask]
elif mode == 'regularize':
# Calculate eigvalues and eigvectors
eigval = torch.abs(eigval)
elif mode == 'clamp':
eigval = torch.clamp_min(eigval, min=epsilon)
else:
raise RuntimeError("Invalid mode! Should have been caught by the assertion.")
if eigenvectors:
return eigval, eigvec
else:
return eigval, eigvec
def sym_inverse(mat, epsilon: float = 1e-6, return_sqrt=False, mode='regularize', return_both=False):
""" Utility function that returns the inverse of a matrix, with the
option to return the square root of the inverse matrix.
Parameters
----------
mat: numpy array with shape [m,m]
Matrix to be inverted.
epsilon : float
Cutoff for eigenvalues.
return_sqrt: bool, optional, default = False
if True, the square root of the inverse matrix is returned instead
mode: str, default='trunc'
Whether to truncate eigenvalues if they are too small or to regularize them by taking the absolute value
and adding a small positive constant. :code:`trunc` leads to truncation, :code:`regularize` leads to epsilon
being added to the eigenvalues after taking the absolute value
return_both: bool, default=False
Whether to return the sqrt and its inverse or simply the inverse
Returns
-------
x_inv: numpy array with shape [m,m]
inverse of the original matrix
"""
if mode=='old':
return _inv(mat, epsilon=epsilon, return_sqrt=return_sqrt)
eigval, eigvec = symeig_reg(mat, epsilon, mode)
# Build the diagonal matrix with the filtered eigenvalues or square
# root of the filtered eigenvalues according to the parameter
if return_sqrt:
diag_inv = torch.diag(torch.sqrt(1. / eigval))
if return_both:
diag = torch.diag(torch.sqrt(eigval))
else:
diag_inv = torch.diag(1. / eigval)
if return_both:
diag = torch.diag(eigval)
if not return_both:
return eigvec.t() @ diag_inv @ eigvec
else:
return eigvec.t() @ diag_inv @ eigvec, eigvec.t() @ diag @ eigvec
sym_inverse.valid_modes = ('trunc', 'regularize', 'clamp', 'old')
def covariances(x: torch.Tensor, y: torch.Tensor, remove_mean: bool = True):
"""Computes instantaneous and time-lagged covariances matrices.
Parameters
----------
x : (T, n) torch.Tensor
Instantaneous data.
y : (T, n) torch.Tensor
Time-lagged data.
remove_mean: bool, default=True
Whether to remove the mean of x and y.
Returns
-------
cov_00 : (n, n) torch.Tensor
Auto-covariance matrix of x.
cov_0t : (n, n) torch.Tensor
Cross-covariance matrix of x and y.
cov_tt : (n, n) torch.Tensor
Auto-covariance matrix of y.
See Also
--------
deeptime.covariance.Covariance : Estimator yielding these kind of covariance matrices based on raw numpy arrays
using an online estimation procedure.
"""
assert x.shape == y.shape, "x and y must be of same shape"
batch_size = x.shape[0]
if remove_mean:
x = x - x.mean(dim=0, keepdim=True)
y = y - y.mean(dim=0, keepdim=True)
# Calculate the cross-covariance
y_t = y.transpose(0, 1)
x_t = x.transpose(0, 1)
cov_01 = 1 / (batch_size - 1) * torch.matmul(x_t, y)
# Calculate the auto-correlations
cov_00 = 1 / (batch_size - 1) * torch.matmul(x_t, x)
cov_11 = 1 / (batch_size - 1) * torch.matmul(y_t, y)
return cov_00, cov_01, cov_11
valid_score_methods = ('VAMP1', 'VAMP2', 'VAMPE')
def VAMPE_score(chi_t, chi_tau, epsilon=1e-6, mode='regularize'):
'''Calculates the VAMPE score for an individual VAMPnet model. Furthermore, it returns
the singular functions and singular values to construct the global operator.
Parameters
----------
chi_t: (T, n) torch.Tensor
Instantaneous data with shape batchsize x outputsize.
chi_tau: (T, n) torch.Tensor
Time-lagged data with shape batchsize x outputsize.
Returns
-------
score: (1) torch.Tensor
VAMPE score.
S: (n, n) torch.Tensor
Singular values on the diagonal of the matrix.
u: (n, n) torch.Tensor
Left singular functions.
v: (n, n) torch.Tensor
Right singular functions.
trace_cov: (1) torch.Tensor
Sum of eigenvalues of the covariance matrix.
'''
shape = chi_t.shape
batch_size = shape[0]
x, y = chi_t, chi_tau
# Calculate the covariance matrices
cov_00 = 1/(batch_size) * torch.matmul(x.T, x)
cov_11 = 1/(batch_size) * torch.matmul(y.T, y)
cov_01 = 1/(batch_size) * torch.matmul(x.T, y)
# Calculate the inverse of the self-covariance matrices
cov_00_inv = sym_inverse(cov_00, return_sqrt = True, epsilon=epsilon, mode=mode)
cov_11_inv = sym_inverse(cov_11, return_sqrt = True, epsilon=epsilon, mode=mode)
# Estimate Vamp-matrix
K = torch.matmul(cov_00_inv, torch.matmul(cov_01, cov_11_inv))
# Estimate the singular value decomposition
a, sing_values, b = torch.svd(K, compute_uv=True)
# Estimate the singular functions
u = cov_00_inv @ a
v = cov_11_inv @ b
S = torch.diag(sing_values)
# Estimate the VAMPE score
term1 = 2* S @ u.T @ cov_01 @ v
term2 = S @ u.T @ cov_00 @ u @ S @ v.T @ cov_11 @ v
score = torch.trace(term1 - term2)
# expand zero dimension for summation
score = torch.unsqueeze(score, dim=0)
# estimate sum of eigenvalues of the covariance matrix to enforce harder assignment
trace_cov = torch.unsqueeze(torch.trace(cov_00), dim=0)
return score, S, u, v, trace_cov
def VAMPE_score_pair(chi1_t, chi1_tau, chi2_t, chi2_tau, S1, S2, u1, u2, v1, v2, device='cpu'):
'''Calculates the VAMPE score for a pair of individual VAMPnet models. The operator is constructed
by the two individual operators and evaluate on the outer space constructed by the individual
feature functions.
Parameters
----------
chi1_t: (T, n) torch.Tensor
Instantaneous data with shape batchsize x outputsize of VAMPnet 1.
chi1_tau: (T, n) torch.Tensor
Time-lagged data with shape batchsize x outputsize of VAMPnet 1.
chi2_t: (T, m) torch.Tensor
Instantaneous data with shape batchsize x outputsize of VAMPnet 2.
chi2_tau: (T, m) torch.Tensor
Time-lagged data with shape batchsize x outputsize of VAMPnet 2.
S1: (n, n) torch.Tensor
Singular values of VAMPnet 1.
S2: (m, m) torch.Tensor
Singular values of VAMPnet 2.
u1: (n, n) torch.Tensor
Left singular functions of VAMPnet 1.
u2: (m, m) torch.Tensor
Left singular functions of VAMPnet 2.
v1: (n, n) torch.Tensor
Right singular functions of VAMPnet 1.
v2: (m, m) torch.Tensor
Right singular functions of VAMPnet 2.
Returns
-------
score: (1) torch.Tensor
VAMPE score for the performance of the constructed operator on the global features.
pen_C00: (1) torch.Tensor
Error of the left singular functions.
pen_C11: (1) torch.Tensor
Error of the right singular functions.
pen_C01: (1) torch.Tensor
Error of the correlation of the two singular functions.
'''
shape1 = chi1_t.shape
shape2 = chi2_t.shape
new_shape = shape1[1] * shape2[1]
batch_size = shape1[0]
# construct the singular functions for the global model from both individual subsystems
U_train = torch.reshape(u1[:,None,:,None] * u2[None,:,None,:], (new_shape, new_shape))
V_train = torch.reshape(v1[:,None,:,None] * v2[None,:,None,:], (new_shape, new_shape))
K_train = torch.reshape(S1[:,None,:,None] * S2[None,:,None,:], (new_shape, new_shape))
# construct the global feature space as the outer product of the individual spaces
chi_t_outer = torch.reshape(chi1_t[:,:,None] * chi2_t[:,None,:], (batch_size,new_shape))
chi_tau_outer = torch.reshape(chi1_tau[:,:,None] * chi2_tau[:,None,:], (batch_size,new_shape))
x, y = chi_t_outer, chi_tau_outer
# Calculate the covariance matrices
cov_00 = 1/(batch_size) * torch.matmul(x.T, x)
cov_11 = 1/(batch_size) * torch.matmul(y.T, y)
cov_01 = 1/(batch_size) * torch.matmul(x.T, y)
# map the matrices on the singular functions
C00_map = U_train.T @ cov_00 @ U_train
C11_map = V_train.T @ cov_11 @ V_train
C01_map = U_train.T @ cov_01 @ V_train
# helper function to estimate errors from optimal solution
unit_matrix = torch.eye(new_shape, device=device)
# Estimate the deviation from the optimal behaviour if the two system would be truly independent
pen_C00 = torch.unsqueeze(torch.sum(torch.abs(unit_matrix - C00_map)), dim=0) / (new_shape-1)**2
pen_C11 = torch.unsqueeze(torch.sum(torch.abs(unit_matrix - C11_map)), dim=0) / (new_shape-1)**2
pen_C01 = torch.unsqueeze(torch.sum(torch.abs(C01_map - K_train)), dim=0) / (new_shape-1)**2
# Estimate the VAMPE score of how well the constructed operator predicts the dynamic of the global feature space
term1 = 2 * K_train @ C01_map
term2 = K_train @ C00_map @ K_train @ C11_map
score = torch.trace(term1 - term2)
# add zero dimension for summation
score = torch.unsqueeze(score, dim=0)
return score, pen_C00, pen_C11, pen_C01
def score_loss(score1, score2, score12):
''' Estimates the discrepancy of the global score and the two individual VAMPE scores.
Parameters
----------
score1: (1) torch.Tensor
Score of VAMPnets 1.
score2: (1) torch.Tensor
Score of VAMPnets 2.
score12: (1) torch.Tensor
Score of the constructed global operator.
Returns
-------
pen_scores: (1) torch.Tensor
Error of the scores due to non independent behavior.
'''
prod_score = score1 * score2
# Estimate normalizer to rescale them but not use them for gradient updates.
norm1 = torch.abs(prod_score.detach())
norm2 = torch.abs(score12.detach())
diff = torch.abs(score12 - prod_score)
score_diff = torch.unsqueeze(diff / norm1, dim=0)
score_diff2 = torch.unsqueeze(diff / norm2, dim=0)
pen_scores = (score_diff + score_diff2) / 2.
return pen_scores
def score_all_systems(chi_t_list, chi_tau_list, epsilon=1e-6, mode='regularize'):
''' Estimates all scores and singular functions/values for all VAMPnets.
Parameters
----------
chi_t_list: list of length number subsystems
List of the feature functions of all VAMPnets for the instantaneous data.
chi_tau_list: list of length number subsystems
List of the feature functions of all VAMPnets for the time-lagged data.
Returns
-------
scores_single: list of length number subsystems
List of the individual scores of all subsystems.
S_single: list of length number subsystems
List of the individual singular values of all subsystems.
u_single: list of length number subsystems
List of the individual left singular functions of all subsystems.
v_single: list of length number subsystems
List of the individual right singular functions of all subsystems.
trace_single: list of length number subsystems
List of the traces of covariance matrices of all subsystems.
'''
scores_single = []
u_single = []
v_single = []
S_single = []
trace_single = []
N = len(chi_t_list)
for i in range(N):
chi_i_t = chi_t_list[i]
chi_i_tau = chi_tau_list[i]
score_i, S_i , u_i, v_i, trace_i = VAMPE_score(chi_i_t, chi_i_tau, epsilon=epsilon, mode=mode)
scores_single.append(score_i)
trace_single.append(trace_i)
u_single.append(u_i)
v_single.append(v_i)
S_single.append(S_i)
return scores_single, S_single, u_single, v_single, trace_single
def score_all_outer_systems(chi_t_list, chi_tau_list, S_list, u_list, v_list, device='cpu'):
''' Estimates the global scores and all penalties.
Parameters
----------
chi_t_list: list of length number subsystems
List of the feature functions of all VAMPnets for the instantaneous data.
chi_tau_list: list of length number subsystems
List of the feature functions of all VAMPnets for the time-lagged data.
S_list: list of length number subsystems
List of the individual singular values of all subsystems.
u_list: list of length number subsystems
List of the individual left singular functions of all subsystems.
v_list: list of length number subsystems
List of the individual right singular functions of all subsystems.
Returns
-------
scores_outer: list of length N*(N-1)/2, where N is the number of subsystems
List of global scores of all pairs of VAMPnets.
pen_C00_map: list of length number pairs.
List of penalties of singular left functions.
pen_C11_map: list of length number pairs.
List of penalties of singular right functions.
pen_C01_map: list of length number pairs.
List of penalties of the correlation of the singular functions.
'''
scores_outer = []
pen_C00_map = []
pen_C11_map = []
pen_C01_map = []
# N_pair = len(scores_outer)
# N = int(0.5+np.sqrt(0.25+2*N_pair))
N = len(chi_t_list)
for i in range(N):
for j in range(i+1,N):
score_ij, pen_C00_ij, pen_C11_ij, pen_C01_ij = VAMPE_score_pair(
chi_t_list[i], chi_tau_list[i],
chi_t_list[j], chi_tau_list[j],
S_list[i], S_list[j],
u_list[i], u_list[j],
v_list[i], v_list[j],
device=device)
scores_outer.append(score_ij)
pen_C00_map.append(pen_C00_ij)
pen_C11_map.append(pen_C11_ij)
pen_C01_map.append(pen_C01_ij)
return scores_outer, pen_C00_map, pen_C11_map, pen_C01_map
def pen_all_scores(scores, score_pairs):
''' Estimate all penalties of the individual and global scores.
Paramters
---------
scores: list of length number subsystems
List of the VAMPE scores of all VAMPnets.
score_pairs: list of length number of pairs
List of the pairwise global scores of all combinations of VAMPnets.
Returns
-------
pen_scores: list of length number of pairs
List of the pairwise penalties of all combindations of VAMPnets.
'''
pen_scores = []
counter = 0
N = len(scores)
for i in range(N):
for j in range(i+1, N):
pen_score_ij = score_loss(scores[i], scores[j], score_pairs[counter])
pen_scores.append(pen_score_ij)
counter+=1
return pen_scores
def estimate_transition_matrix(chi_t, chi_tau, mode='regularize', epsilon=1e-6):
''' Estimate the transition matrix given the feature vectors at time t and tau
'''
shape = chi_t.shape
batch_size = shape[0]
x, y = chi_t, chi_tau
# Calculate the covariance matrices
cov_00 = 1/(batch_size) * torch.matmul(x.T, x)
cov_11 = 1/(batch_size) * torch.matmul(y.T, y)
cov_01 = 1/(batch_size) * torch.matmul(x.T, y)
# Calculate the inverse of the self-covariance matrices
cov_00_inv = sym_inverse(cov_00, return_sqrt = False, epsilon=epsilon, mode=mode)
T = cov_00_inv @ cov_01
return T.detach().to('cpu').numpy()
class iVAMPnetModel(Transformer, Model):
r"""
A iVAMPNet model which can be fit to data optimizing for one of the implemented VAMP scores.
Parameters
----------
lobes : list of torch.nn.Module
List of the lobes of each VAMPNet. See also :class:`deeptime.util.torch.MLP`.
mask : layer
Layer, which masks the inputs to the different iVAMPnets. This makes it possible to interpret which
part of the input is important for each lobe.
dtype : data type, default=np.float32
The data type for which operations should be performed. Leads to an appropriate cast within fit and
transform methods.
device : device, default=None
The device for the lobes. Can be None which defaults to CPU.
See Also
--------
iVAMPNet : The corresponding estimator.
"""
def __init__(self, lobes: list, mask,
dtype=np.float32, device=None, epsilon=1e-6, mode='regularize'):
super().__init__()
self._lobes = lobes
self._mask = mask
self._N = len(lobes)
self._dtype = dtype
if self._dtype == np.float32:
for n in range(self._N):
self._lobes[n] = self._lobes[n].float()
elif self._dtype == np.float64:
for n in range(self._N):
self._lobes[n] = self._lobes[n].double()
self._device = device
self._epsilon = epsilon
self._mode = mode
def transform(self, data, numpy=True, batchsize=0, **kwargs):
'''Transforms the supplied data with the model. It outputs the fuzzy state assignment for each
subsystem.
Parameters
----------
data: nd.array or torch.Tensor
Data which should be transformed to a fuzzy state assignment.
numpy: bool, default=True
If the output should be converted to a numpy array.
batchsize: int, default=0
The batchsize which should be used to predict one chunk of the data, which is useful, if
data does not fit into the memory. If batchsize<=0 the whole dataset will be simultaneously
transformed.
Returns
-------
out: nd.array or torch.Tensor
The transformed data. If numpy=True the output will be a nd.array otherwise a torch.Tensor.
'''
for lobe in self._lobes:
lobe.eval()
if batchsize>0:
batches = data.shape[0]//batchsize + (data.shape[0]%batchsize>0)
if isinstance(data, torch.Tensor):
torch.split(data, batches)
else:
data = np.array_split(data, batches)
out = [[] for _ in range(self._N)]
with torch.no_grad():
for data_tensor in map_data(data, device=self._device, dtype=self._dtype):
mask_data = self._mask(data_tensor)
for n in range(self._N):
mask_n = torch.squeeze(mask_data[n], dim=2)
if numpy:
out[n].append(self._lobes[n](mask_n).cpu().numpy())
else:
out[n].append(self._lobes[n](mask_n))
return out if len(out) > 1 else out[0]
def get_transition_matrix(self, data_0, data_t, batchsize=0):
''' Estimates the transition matrix based on the two provided datasets, where each frame
should be lagtime apart.
Parameters
----------
data_0: nd.array or torch.Tensor
The instantaneous data.
data_t: nd.array or torch.Tensor
The time-lagged data.
batchsize: int, default=0
The batchsize which should be used to predict one chunk of the data, which is useful, if
data does not fit into the memory. If batchsize<=0 the whole dataset will be simultaneously
transformed.
Returns
-------
T_list: list
The list of the transition matrices of all subsystems.
'''
chi_t_list = self.transform(data_0, numpy=False, batchsize=batchsize)
chi_tau_list = self.transform(data_t, numpy=False, batchsize=batchsize)
T_list = []
for n in range(self._N):
chi_t, chi_tau = torch.cat(chi_t_list[n], dim=0), torch.cat(chi_tau_list[n], dim=0)
K = estimate_transition_matrix(chi_t, chi_tau, mode=self._mode, epsilon=self._epsilon).astype('float64')
# Converting to double precision destroys the normalization
T = K / K.sum(axis=1)[:, None]
T_list.append(T)
return T_list
def timescales(self, data_0, data_t, tau, batchsize=0):
''' Estimates the timescales of the model given the provided data.
Parameters
----------
data_0: nd.array or torch.Tensor
The instantaneous data.
data_t: nd.array or torch.Tensor
The time-lagged data.
tau: int
The time-lagged used for the data.
batchsize: int, default=0
The batchsize which should be used to predict one chunk of the data, which is useful, if
data does not fit into the memory. If batchsize<=0 the whole dataset will be simultaneously
transformed.
Returns
-------
its: list
The list of the implied timescales of all subsystems.
'''
T_list = self.get_transition_matrix(data_0, data_t, batchsize=batchsize)
its = []
for T in T_list:
eigvals = np.linalg.eigvals(T)
eigvals_sort = np.sort(eigvals)[:-1] # remove eigenvalue 1
its.append( - tau/np.log(np.abs(eigvals_sort[::-1])))
return its
class iVAMPnet(DLEstimatorMixin, Transformer):
r""" Implementation of iVAMPNets :cite:`vnet-mardt2018vampnets` which try to find an optimal featurization of
data based on a VAMPE score :cite:`vnet-wu2020variational` by using neural networks as featurizing transforms
which are sought to be independent. This estimator is also a transformer
and can be used to transform data into the optimized space. From there it can either be used to estimate
Markov state models via making assignment probabilities crisp (in case of softmax output distributions) or
to estimate the Koopman operator using the :class:`VAMP <deeptime.decomposition.VAMP>` estimator.
Parameters
----------
lobes : list of torch.nn.Module
List of the lobes of each VAMPNet. See also :class:`deeptime.util.torch.MLP`.
mask : torch.nn.module
Module which masks the input features to assign them to a specific subsystem.
device : torch device, default=None
The device on which the torch modules are executed.
optimizer : str or Callable, default='Adam'
An optimizer which can either be provided in terms of a class reference (like `torch.optim.Adam`) or
a string (like `'Adam'`). Defaults to Adam.
learning_rate : float, default=5e-4
The learning rate of the optimizer.
score_mode : str, default='regularize'
The mode under which inverses of positive semi-definite matrices are estimated. Per default, the matrices
are perturbed by a small constant added to the diagonal. This makes sure that eigenvalues are not too
small. For a complete list of modes, see :meth:`sym_inverse`.
epsilon : float, default=1e-6
The strength of the regularization under which matrices are inverted. Meaning depends on the score_mode,
see :meth:`sym_inverse`.
dtype : dtype, default=np.float32
The data type of the modules and incoming data.
shuffle : bool, default=True
Whether to shuffle data during training after each epoch.
See Also
--------
deeptime.decomposition.VAMP
References
----------
.. bibliography:: /references.bib
:style: unsrt
:filter: docname in docnames
:keyprefix: vnet-
"""
_MUTABLE_INPUT_DATA = True
def __init__(self, lobes: list, mask: nn.Module,
device=None, optimizer: Union[str, Callable] = 'Adam', learning_rate: float = 5e-4, learning_rate_mask: float = 5e-4,
score_mode: str = 'regularize', epsilon: float = 1e-6,
dtype=np.float32, shuffle: bool = True):
super().__init__()
self.N = len(lobes)
self.lobes = lobes
self.mask = mask
self.score_mode = score_mode
self._step = 0
self.shuffle = shuffle
self._epsilon = epsilon
self.device = device
self.learning_rate = learning_rate
self.learning_rate_mask = learning_rate_mask
self.dtype = dtype
self.optimizer_lobes = [torch.optim.Adam(lobe.parameters(), lr=self.learning_rate) for lobe in self.lobes]
self.optimizer_mask = torch.optim.Adam(self.mask.parameters(), lr=self.learning_rate_mask)
self._train_scores = []
self._validation_scores = []
self._train_vampe = []
self._train_pen_C00 = []
self._train_pen_C11 = []
self._train_pen_C01 = []
self._train_pen_scores = []
self._train_trace = []
self._validation_vampe = []
self._validation_pen_C00 = []
self._validation_pen_C11 = []
self._validation_pen_C01 = []
self._validation_pen_scores = []
self._validation_trace = []
@property
def train_scores(self) -> np.ndarray:
r""" The collected train scores. First dimension contains the step, second dimension the score. Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._train_scores)
@property
def train_vampe(self) -> np.ndarray:
r""" The collected train scores. First dimension contains the step, second dimension the score. Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._train_vampe)
@property
def train_pen_C00(self) -> np.ndarray:
r""" The collected train scores. First dimension contains the step, second dimension the score. Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._train_pen_C00)
@property
def train_pen_C11(self) -> np.ndarray:
r""" The collected train scores. First dimension contains the step, second dimension the score. Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._train_pen_C11)
@property
def train_pen_C01(self) -> np.ndarray:
r""" The collected train scores. First dimension contains the step, second dimension the score. Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._train_pen_C01)
@property
def train_pen_scores(self) -> np.ndarray:
r""" The collected train scores. First dimension contains the step, second dimension the score. Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._train_pen_scores)
@property
def train_trace(self) -> np.ndarray:
r""" The collected train scores. First dimension contains the step, second dimension the score. Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._train_trace)
@property
def validation_scores(self) -> np.ndarray:
r""" The collected validation scores. First dimension contains the step, second dimension the score.
Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._validation_scores)
@property
def validation_vampe(self) -> np.ndarray:
r""" The collected train scores. First dimension contains the step, second dimension the score. Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._validation_vampe)
@property
def validation_pen_C00(self) -> np.ndarray:
r""" The collected train scores. First dimension contains the step, second dimension the score. Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._validation_pen_C00)
@property
def validation_pen_C11(self) -> np.ndarray:
r""" The collected train scores. First dimension contains the step, second dimension the score. Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._validation_pen_C11)
@property
def validation_pen_C01(self) -> np.ndarray:
r""" The collected train scores. First dimension contains the step, second dimension the score. Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._validation_pen_C01)
@property
def validation_pen_scores(self) -> np.ndarray:
r""" The collected train scores. First dimension contains the step, second dimension the score. Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._validation_pen_scores)
@property
def validation_trace(self) -> np.ndarray:
r""" The collected train scores. First dimension contains the step, second dimension the score. Initially empty.
:type: (T, 2) ndarray
"""
return np.array(self._validation_trace)
@property
def epsilon(self) -> float:
r""" Regularization parameter for matrix inverses.
:getter: Gets the currently set parameter.
:setter: Sets a new parameter. Must be non-negative.
:type: float
"""
return self._epsilon
@epsilon.setter
def epsilon(self, value: float):
assert value >= 0
self._epsilon = value
@property
def score_method(self) -> str:
r""" Property which steers the scoring behavior of this estimator.
:getter: Gets the current score.
:setter: Sets the score to use.
:type: str
"""
return self._score_method
@score_method.setter
def score_method(self, value: str):
if value not in valid_score_methods:
raise ValueError(f"Tried setting an unsupported scoring method '{value}', "
f"available are {valid_score_methods}.")
self._score_method = value
# @property
# def lobes(self) -> nn.Module:
# r""" The instantaneous lobe of the VAMPNet.
# :getter: Gets the instantaneous lobe.
# :setter: Sets a new lobe.
# :type: torch.nn.Module
# """
# return self.lobes
# @lobes.setter
# def lobes(self, value: list):
# assert len(value)==self.N, 'You must provide as many lobes as independent subsystems!'
# for n in range(self.N):
# self.lobes[n] = value[n]
# if self.dtype == np.float32:
# self.lobes[n] = self.lobes[n].float()
# else:
# self.lobes[n] = self.lobes[n].double()
# self.lobes[n] = self.lobes[n].to(device=self.device)
def forward(self, data):
if data.get_device():
data = data.to(device=self.device)
masked_data = self.mask(data)
chi_data_list = []
for n in range(self.N):
lobe = self.lobes[n]
data_n = torch.squeeze(masked_data[n], dim=2)
chi_data_list.append(lobe(data_n))
return chi_data_list
def reset_scores(self):
self._train_scores = []
self._validation_scores = []
self._train_vampe = []
self._train_pen_C00 = []
self._train_pen_C11 = []
self._train_pen_C01 = []
self._train_pen_scores = []
self._train_trace = []
self._validation_vampe = []
self._validation_pen_C00 = []
self._validation_pen_C11 = []
self._validation_pen_C01 = []
self._validation_pen_scores = []
self._validation_trace = []
self._step = 0
def partial_fit(self, data, lam_decomp: float = 1., mask: bool = False, lam_trace: float = 0.,
train_score_callback: Callable[[int, torch.Tensor], None] = None,
tb_writer=None, clip=False, lam_pen_perc=None, lam_pen_C00=0., lam_pen_C11=0., lam_pen_C01=0.):
r""" Performs a partial fit on data. This does not perform any batching.
Parameters
----------
data : tuple or list of length 2, containing instantaneous and timelagged data
The data to train the lobe(s) on.
lam_decomp : float
The weighting factor how much the dependency score should be weighted in the loss.
mask : bool default False
Whether the mask should be trained or not.
lam_trace : float
The weighting factor how much the trace should be weighted in the loss.
train_score_callback : callable, optional, default=None
An optional callback function which is evaluated after partial fit, containing the current step
of the training (only meaningful during a :meth:`fit`) and the current score as torch Tensor.
tb_writer : tensorboard writer
If given, scores will be recorded in the tensorboard log file.
clip : bool default=False
If True the gradients of the weights will be clipped by norm before applying them for the update.
Returns
-------
self : iVAMPNet
Reference to self.
"""
if self.dtype == np.float32:
for n in range(self.N):
self.lobes[n] = self.lobes[n].float()
elif self.dtype == np.float64:
for n in range(self.N):
self.lobes[n] = self.lobes[n].double()
for n in range(self.N):
self.lobes[n].train()
self.mask.train()
assert isinstance(data, (list, tuple)) and len(data) == 2, \
"Data must be a list or tuple of batches belonging to instantaneous " \
"and respective time-lagged data."
batch_0, batch_t = data[0], data[1]
if isinstance(data[0], np.ndarray):
batch_0 = torch.from_numpy(data[0].astype(self.dtype)).to(device=self.device)
if isinstance(data[1], np.ndarray):
batch_t = torch.from_numpy(data[1].astype(self.dtype)).to(device=self.device)
for opt in self.optimizer_lobes:
opt.zero_grad()
if mask:
self.optimizer_mask.zero_grad()
chi_t_list = self.forward(batch_0) # returns list of feature vectors
chi_tau_list = self.forward(batch_t)
# Estimate all individual scores and singular functions
scores_single, S_single, u_single, v_single, trace_single = score_all_systems(chi_t_list, chi_tau_list,
epsilon=self._epsilon, mode=self.score_mode)
# Estimate all pairwise scores and independent penalties
score_pairs, pen_C00_map, pen_C11_map, pen_C01_map = score_all_outer_systems(chi_t_list, chi_tau_list, S_single,
u_single, v_single, device=self.device)
# Estimate the penalty of the scores
pen_scores = pen_all_scores(scores_single, score_pairs)
# Take the mean over all pairs
pen_scores_all = torch.mean(torch.cat(pen_scores, dim=0))
pen_C00_map_all = torch.mean(torch.cat(pen_C00_map, dim=0))
pen_C11_map_all = torch.mean(torch.cat(pen_C11_map, dim=0))
pen_C01_map_all = torch.mean(torch.cat(pen_C01_map, dim=0))
trace_all = torch.mean(torch.cat(trace_single, dim=0))
# Estimate the sum of scores, !!! Check if mean is correct
vamp_sum_score = torch.mean(torch.cat(scores_single, dim=0))
vamp_score_pairs = torch.mean(torch.cat(score_pairs, dim=0))
if lam_pen_perc is not None:
vamp_score_item, pen_score_item, pen_c00_item, pen_c11_item, pen_c01_item = vamp_score_pairs.item(), pen_scores_all.item(), pen_C00_map_all.item(), pen_C11_map_all.item(), pen_C01_map_all.item()
fac_pen_score, fac_pen_c00, fac_pen_c11, fac_pen_c01 = lam_pen_perc * vamp_score_item/pen_score_item, lam_pen_C00 * vamp_score_item/pen_c00_item, lam_pen_C11 * vamp_score_item/pen_c11_item, lam_pen_C01 * vamp_score_item/pen_c01_item
loss_value = - vamp_score_pairs - lam_trace * trace_all + fac_pen_score * pen_scores_all + fac_pen_c00 * pen_C00_map_all + fac_pen_c11 * pen_C11_map_all + fac_pen_c01 * pen_C01_map_all
else:
loss_value = - vamp_score_pairs + lam_decomp * pen_scores_all - lam_trace * trace_all