-
Notifications
You must be signed in to change notification settings - Fork 27
/
models.py
979 lines (756 loc) · 40.6 KB
/
models.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
import objax
import jax.numpy as np
from jax import vmap
from .utils import diag, cho_factor, cho_solve, softplus, softplus_inv, transpose
from .basemodels import (
GaussianProcess,
SparseGaussianProcess,
MarkovGaussianProcess,
SparseMarkovGaussianProcess,
MarkovMeanFieldGaussianProcess,
SparseMarkovMeanFieldGaussianProcess,
InfiniteHorizonGaussianProcess,
SparseInfiniteHorizonGaussianProcess
)
from .inference import (
Newton,
VariationalInference,
ExpectationPropagation,
PosteriorLinearisation,
PosteriorLinearisation2ndOrder,
PosteriorLinearisation2ndOrderGaussNewton,
PosteriorLinearisation2ndOrderQuasiNewton,
Taylor,
GaussNewton,
VariationalGaussNewton,
QuasiNewton,
VariationalQuasiNewton,
ExpectationPropagationQuasiNewton,
# PosteriorLinearisationQuasiNewton,
NewtonRiemann,
VariationalInferenceRiemann,
ExpectationPropagationRiemann,
PosteriorLinearisation2ndOrderRiemann
)
from .kernels import Independent
# ############ Syntactic sugar adding the inference method functionality to the models ################
# note: re-declaring the inputs here is not strictly necessary, but creates nice documentation
# ##### Variational Inference #####
class VariationalGP(VariationalInference, GaussianProcess):
"""
Variational Gaussian process [1], adapted to use conjugate computation VI [2]
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
[1] Opper, Archambeau: The Variational Gaussian Approximation Revisited, Neural Computation, 2009
[2] Khan, Lin: Conugate-Computation Variational Inference - Converting Inference in Non-Conjugate Models in to
Inference in Conjugate Models, AISTATS 2017
"""
def __init__(self, kernel, likelihood, X, Y):
super().__init__(kernel, likelihood, X, Y)
class VariationalRiemannGP(VariationalInferenceRiemann, GaussianProcess):
"""
Variational Gaussian process [1], adapted to use conjugate computation VI [2] with PSD guarantees [3].
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
[1] Opper, Archambeau: The Variational Gaussian Approximation Revisited, Neural Computation, 2009
[2] Khan, Lin: Conugate-Computation Variational Inference - Converting Inference in Non-Conjugate Models in to
Inference in Conjugate Models, AISTATS 2017
[3] Lin, Schmidt & Khan: Handling the Positive-Definite Constraint in the Bayesian Learning Rule, ICML 2020
"""
def __init__(self, kernel, likelihood, X, Y):
super().__init__(kernel, likelihood, X, Y)
class SparseVariationalGP(VariationalInference, SparseGaussianProcess):
"""
Sparse variational Gaussian process (SVGP) [1], adapted to use conjugate computation VI [2]
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
:param Z: inducing inputs
:param opt_z: boolean determining whether to optimise the inducing input locations
[1] Hensman, Matthews, Ghahramani: Scalable Variational Gaussian Process Classification, AISTATS 2015
[2] Khan, Lin: Conugate-Computation Variational Inference - Converting Inference in Non-Conjugate Models in to
Inference in Conjugate Models, AISTATS 2017
"""
def __init__(self, kernel, likelihood, X, Y, Z, opt_z=False):
super().__init__(kernel, likelihood, X, Y, Z, opt_z)
class SparseVariationalRiemannGP(VariationalInferenceRiemann, SparseGaussianProcess):
"""
Sparse variational Gaussian process (SVGP) [1], adapted to use conjugate computation VI [2] with PSD guarantees [3].
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
:param Z: inducing inputs
:param opt_z: boolean determining whether to optimise the inducing input locations
[1] Hensman, Matthews, Ghahramani: Scalable Variational Gaussian Process Classification, AISTATS 2015
[2] Khan, Lin: Conugate-Computation Variational Inference - Converting Inference in Non-Conjugate Models in to
Inference in Conjugate Models, AISTATS 2017
[3] Lin, Schmidt & Khan: Handling the Positive-Definite Constraint in the Bayesian Learning Rule, ICML 2020
"""
def __init__(self, kernel, likelihood, X, Y, Z, opt_z=False):
super().__init__(kernel, likelihood, X, Y, Z, opt_z)
SVGP = SparseVariationalGP
class MarkovVariationalGP(VariationalInference, MarkovGaussianProcess):
"""
Markov variational Gaussian process: a VGP where the posterior is computed via
(spatio-temporal) filtering and smoothing [1]
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
:param R: spatial inputs
:param parallel: boolean determining whether to run parallel filtering
[1] Chang, Wilkinson, Khan, Solin: Fast Variational Learning in State Space Gaussian Process Models, MLSP 2020
"""
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None):
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
class SparseMarkovVariationalGP(VariationalInference, SparseMarkovGaussianProcess):
"""
Sparse Markov variational Gaussian process: a sparse VGP with inducing states, where the posterior is computed via
(spatio-temporal) filtering and smoothing [1, 2].
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
:param R: spatial inputs
:param parallel: boolean determining whether to run parallel filtering
:param Z: inducing inputs
[1] Adam, Eleftheriadis, Durrande, Artemev, Hensman: Doubly Sparse Variational Gaussian Processes, AISTATS 2020
[2] Wilkinson, Solin, Adam: Sparse Algorithms for Markovian Gaussian Processes, AISTATS 2021
"""
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None, Z=None):
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel, Z=Z)
class MarkovVariationalMeanFieldGP(VariationalInference, MarkovMeanFieldGaussianProcess):
pass
class SparseMarkovVariationalMeanFieldGP(VariationalInference, SparseMarkovMeanFieldGaussianProcess):
pass
class InfiniteHorizonVariationalGP(VariationalInference, InfiniteHorizonGaussianProcess):
"""
Infinite-horizon GP [1] with variational inference.
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
:param R: spatial inputs
:param dare_iters: number of iterations to run the DARE solver for
[1] Solin, Hensman, Turner: Infinite-Horizon Gaussian Processes, NeurIPS 2018
"""
def __init__(self, kernel, likelihood, X, Y, R=None, dare_iters=20, parallel=None):
super().__init__(kernel, likelihood, X, Y, R=R, dare_iters=dare_iters, parallel=parallel)
class SparseInfiniteHorizonVariationalGP(VariationalInference, SparseInfiniteHorizonGaussianProcess):
pass
# ##### Expectation Propagation #####
class ExpectationPropagationGP(ExpectationPropagation, GaussianProcess):
"""
Expectation propagation Gaussian process (EPGP).
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
[1] Minka: A Family of Algorithms for Approximate Bayesian Inference, Ph. D thesis 2000
"""
def __init__(self, kernel, likelihood, X, Y, power=1.):
self.power = power
super().__init__(kernel, likelihood, X, Y)
class SparseExpectationPropagationGP(ExpectationPropagation, SparseGaussianProcess):
"""
Sparse expectation propagation Gaussian process (SEPGP) [1].
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
:param Z: inducing inputs
:param opt_z: boolean determining whether to optimise the inducing input locations
[1] Csato, Opper: Sparse on-line Gaussian processes, Neural Computation 2002
[2] Bui, Yan, Turner: A Unifying Framework for Gaussian Process Pseudo Point Approximations Using
Power Expectation Propagation, JMLR 2017
"""
def __init__(self, kernel, likelihood, X, Y, Z, power=1., opt_z=False):
self.power = power
super().__init__(kernel, likelihood, X, Y, Z, opt_z=opt_z)
class MarkovExpectationPropagationGP(ExpectationPropagation, MarkovGaussianProcess):
"""
Markov EP Gaussian process: an EPGP where the posterior is computed via
(spatio-temporal) filtering and smoothing [1].
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
:param R: spatial inputs
:param parallel: boolean determining whether to run parallel filtering
[1] Wilkinson, Chang, Riis Andersen, Solin: State Space Expectation Propagation, ICML 2020
"""
def __init__(self, kernel, likelihood, X, Y, R=None, power=1., parallel=None):
self.power = power
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
class SparseMarkovExpectationPropagationGP(ExpectationPropagation, SparseMarkovGaussianProcess):
"""
Sparse Markov EP Gaussian process: a sparse EPGP with inducing states, where the posterior is computed via
(spatio-temporal) filtering and smoothing [1].
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
:param R: spatial inputs
:param parallel: boolean determining whether to run parallel filtering
:param Z: inducing inputs
[1] Wilkinson, Solin, Adam: Sparse Algorithms for Markovian Gaussian Processes, AISTATS 2021
"""
def __init__(self, kernel, likelihood, X, Y, R=None, power=1., parallel=None, Z=None):
self.power = power
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel, Z=Z)
class MarkovExpectationPropagationMeanFieldGP(ExpectationPropagation, MarkovMeanFieldGaussianProcess):
pass
class SparseMarkovExpectationPropagationMeanFieldGP(ExpectationPropagation, SparseMarkovMeanFieldGaussianProcess):
pass
class InfiniteHorizonExpectationPropagationGP(ExpectationPropagation, InfiniteHorizonGaussianProcess):
"""
Infinite-horizon GP [1] with expectation propagation.
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
:param R: spatial inputs
:param dare_iters: number of iterations to run the DARE solver for
[1] Solin, Hensman, Turner: Infinite-Horizon Gaussian Processes, NeurIPS 2018
"""
def __init__(self, kernel, likelihood, X, Y, R=None, power=1., dare_iters=20, parallel=None):
self.power = power
super().__init__(kernel, likelihood, X, Y, R=R, dare_iters=dare_iters, parallel=parallel)
class SparseInfiniteHorizonExpectationPropagationGP(ExpectationPropagation, SparseInfiniteHorizonGaussianProcess):
pass
# ##### Newton / Laplace #####
class NewtonGP(Newton, GaussianProcess):
"""
[1] Rasmussen, Williams: Gaussian Processes for Machine Learning, 2006
"""
def __init__(self, kernel, likelihood, X, Y):
super().__init__(kernel, likelihood, X, Y)
class SparseNewtonGP(Newton, SparseGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, Z, opt_z=False):
super().__init__(kernel, likelihood, X, Y, Z, opt_z=opt_z)
class MarkovNewtonGP(Newton, MarkovGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None):
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
class SparseMarkovNewtonGP(Newton, SparseMarkovGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None, Z=None):
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel, Z=Z)
class MarkovNewtonMeanFieldGP(Newton, MarkovMeanFieldGaussianProcess):
pass
class SparseMarkovNewtonMeanFieldGP(Newton, SparseMarkovMeanFieldGaussianProcess):
pass
class InfiniteHorizonNewtonGP(Newton, InfiniteHorizonGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, R=None, dare_iters=20, parallel=None):
super().__init__(kernel, likelihood, X, Y, R=R, dare_iters=dare_iters, parallel=parallel)
class SparseInfiniteHorizonNewtonGP(Newton, SparseInfiniteHorizonGaussianProcess):
pass
LaplaceGP = NewtonGP
SparseLaplaceGP = SparseNewtonGP
MarkovLaplaceGP = MarkovNewtonGP
SparseMarkovLaplaceGP = SparseMarkovNewtonGP
MarkovLaplaceGPMeanField = MarkovNewtonMeanFieldGP
SparseMarkovLaplaceGPMeanField = SparseMarkovNewtonMeanFieldGP
InfiniteHorizonLaplaceGP = InfiniteHorizonNewtonGP
SparseInfiniteHorizonLaplaceGP = SparseInfiniteHorizonNewtonGP
# ##### Posterior Linearisation #####
class PosteriorLinearisationGP(PosteriorLinearisation, GaussianProcess):
"""
[1] Garcia-Fernandez, Tronarp, Sarkka: Gaussian Process Classification
Using Posterior Linearization, IEEE Signal Processing 2019
"""
def __init__(self, kernel, likelihood, X, Y):
super().__init__(kernel, likelihood, X, Y)
# class PosteriorLinearisationNewtonGP(PosteriorLinearisationNewton, GaussianProcess):
# pass
class SparsePosteriorLinearisationGP(PosteriorLinearisation, SparseGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, Z, opt_z=False):
super().__init__(kernel, likelihood, X, Y, Z, opt_z=opt_z)
class MarkovPosteriorLinearisationGP(PosteriorLinearisation, MarkovGaussianProcess):
"""
[1] Garcia-Fernandez, Svensson, Sarkka: Iterated Posterior Linearization Smoother, IEEE Automatic Control 2016
[2] Wilkinson, Chang, Riis Andersen, Solin: State Space Expectation Propagation, ICML 2020
"""
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None):
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
class SparseMarkovPosteriorLinearisationGP(PosteriorLinearisation, SparseMarkovGaussianProcess):
"""
[1] Wilkinson, Solin, Adam: Sparse Algorithms for Markovian Gaussian Processes, AISTATS 2021
"""
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None, Z=None):
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel, Z=Z)
class MarkovPosteriorLinearisationMeanFieldGP(PosteriorLinearisation, MarkovMeanFieldGaussianProcess):
pass
class SparseMarkovPosteriorLinearisationMeanFieldGP(PosteriorLinearisation, SparseMarkovMeanFieldGaussianProcess):
pass
class InfiniteHorizonPosteriorLinearisationGP(PosteriorLinearisation, InfiniteHorizonGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, R=None, dare_iters=20, parallel=None):
super().__init__(kernel, likelihood, X, Y, R=R, dare_iters=dare_iters, parallel=parallel)
class SparseInfiniteHorizonPosteriorLinearisationGP(PosteriorLinearisation, SparseInfiniteHorizonGaussianProcess):
pass
# ##### Taylor #####
class TaylorGP(Taylor, GaussianProcess):
"""
[1] Steinberg, Bonilla: Extended and Unscented Gaussian Processes, NeurIPS 2014
"""
def __init__(self, kernel, likelihood, X, Y):
super().__init__(kernel, likelihood, X, Y)
class SparseTaylorGP(Taylor, SparseGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, Z, opt_z=False):
super().__init__(kernel, likelihood, X, Y, Z, opt_z=opt_z)
class MarkovTaylorGP(Taylor, MarkovGaussianProcess):
"""
[1] Bell: The Iterated Kalman Smoother as a Gauss-Newton method, SIAM Journal on Optimization 1994
"""
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None):
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
class SparseMarkovTaylorGP(Taylor, SparseMarkovGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None, Z=None):
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel, Z=Z)
class MarkovTaylorMeanFieldGP(Taylor, MarkovMeanFieldGaussianProcess):
pass
class SparseMarkovTaylorMeanFieldGP(Taylor, SparseMarkovMeanFieldGaussianProcess):
pass
class InfiniteHorizonTaylorGP(Taylor, InfiniteHorizonGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, R=None, dare_iters=20, parallel=None):
super().__init__(kernel, likelihood, X, Y, R=R, dare_iters=dare_iters, parallel=parallel)
class SparseInfiniteHorizonTaylorGP(Taylor, SparseInfiniteHorizonGaussianProcess):
pass
# Extensions to posterior linearisation
class MarkovPosteriorLinearisation2ndOrderGP(PosteriorLinearisation2ndOrder, MarkovGaussianProcess):
pass
class MarkovPosteriorLinearisation2ndOrderGaussNewtonGP(PosteriorLinearisation2ndOrderGaussNewton,
MarkovGaussianProcess):
pass
class MarkovPosteriorLinearisation2ndOrderRiemannGP(PosteriorLinearisation2ndOrderRiemann, MarkovGaussianProcess):
pass
# Gauss-Newton approximations
class GaussNewtonGP(GaussNewton, GaussianProcess):
def __init__(self, kernel, likelihood, X, Y):
super().__init__(kernel, likelihood, X, Y)
LaplaceGaussNewtonGP = GaussNewtonGP
class MarkovGaussNewtonGP(GaussNewton, MarkovGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None):
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
MarkovLaplaceGaussNewtonGP = MarkovGaussNewtonGP
class SparseGaussNewtonGP(GaussNewton, SparseGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, Z, opt_z=False):
super().__init__(kernel, likelihood, X, Y, Z, opt_z)
SparseLaplaceGaussNewtonGP = SparseGaussNewtonGP
class VariationalGaussNewtonGP(VariationalGaussNewton, GaussianProcess):
def __init__(self, kernel, likelihood, X, Y):
super().__init__(kernel, likelihood, X, Y)
class MarkovVariationalGaussNewtonGP(VariationalGaussNewton, MarkovGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None):
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
class SparseVariationalGaussNewtonGP(VariationalGaussNewton, SparseGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, Z, opt_z=False):
super().__init__(kernel, likelihood, X, Y, Z, opt_z)
# Quasi-Newton approximations
# --- quasi-Newton ---
class QuasiNewtonGP(QuasiNewton, GaussianProcess):
def __init__(self, kernel, likelihood, X, Y):
super().__init__(kernel, likelihood, X, Y)
self.mean_prev = objax.StateVar(self.pseudo_likelihood.mean)
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, self.func_dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(self.func_dim), [self.num_data, 1, 1]))
LaplaceQuasiNewtonGP = QuasiNewtonGP
class VariationalQuasiNewtonGP(VariationalQuasiNewton, GaussianProcess):
def __init__(self, kernel, likelihood, X, Y, fullcov=True):
self.fullcov = fullcov
super().__init__(kernel, likelihood, X, Y)
if fullcov:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
np.reshape(self.pseudo_likelihood.covariance, (self.num_data, -1, 1))],
axis=1)
)
dim = self.mean_prev.value.shape[1]
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(dim), [self.num_data, 1, 1]))
else:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
diag(self.pseudo_likelihood.covariance)[..., None]], axis=1)
)
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, 2 * self.func_dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(2 * self.func_dim), [self.num_data, 1, 1]))
class ExpectationPropagationQuasiNewtonGP(ExpectationPropagationQuasiNewton, GaussianProcess):
def __init__(self, kernel, likelihood, X, Y, power=1., fullcov=True):
self.power = power
self.fullcov = fullcov
super().__init__(kernel, likelihood, X, Y)
if fullcov:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
np.reshape(self.pseudo_likelihood.covariance, (self.num_data, -1, 1))],
axis=1)
)
dim = self.mean_prev.value.shape[1]
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(dim), [self.num_data, 1, 1]))
else:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
diag(self.pseudo_likelihood.covariance)[..., None]], axis=1)
)
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, 2 * self.func_dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(2 * self.func_dim), [self.num_data, 1, 1]))
# class PosteriorLinearisationQuasiNewtonGP(PosteriorLinearisationQuasiNewton, GaussianProcess):
# def __init__(self, kernel, likelihood, X, Y, fullcov=True):
# self.fullcov = fullcov
# super().__init__(kernel, likelihood, X, Y)
# if fullcov:
# self.mean_prev = objax.StateVar(
# np.concatenate([self.pseudo_likelihood.mean,
# np.reshape(self.pseudo_likelihood.covariance, (self.num_data, -1, 1))],
# axis=1)
# )
# dim = self.mean_prev.value.shape[1]
# self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, dim, 1]))
# self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(dim), [self.num_data, 1, 1]))
# else:
# self.mean_prev = objax.StateVar(
# np.concatenate([self.pseudo_likelihood.mean,
# diag(self.pseudo_likelihood.covariance)[..., None]], axis=1)
# )
# self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, 2 * self.func_dim, 1]))
# self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(2 * self.func_dim), [self.num_data, 1, 1]))
class PosteriorLinearisation2ndOrderQuasiNewtonGP(PosteriorLinearisation2ndOrderQuasiNewton, GaussianProcess):
def __init__(self, kernel, likelihood, X, Y, fullcov=True):
self.fullcov = fullcov
super().__init__(kernel, likelihood, X, Y)
if fullcov:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
np.reshape(self.pseudo_likelihood.covariance, (self.num_data, -1, 1))],
axis=1)
)
dim = self.mean_prev.value.shape[1]
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(dim), [self.num_data, 1, 1]))
else:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
diag(self.pseudo_likelihood.covariance)[..., None]], axis=1)
)
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, 2 * self.func_dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(2 * self.func_dim), [self.num_data, 1, 1]))
# --- Sparse Quasi-Newton ---
class SparseQuasiNewtonGP(QuasiNewton, SparseGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, Z, opt_z=False):
super().__init__(kernel, likelihood, X, Y, Z, opt_z=opt_z)
self.mean_prev = objax.StateVar(self.pseudo_likelihood.mean)
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, self.func_dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(self.func_dim), [self.num_data, 1, 1]))
SparseLaplaceQuasiNewtonGP = SparseQuasiNewtonGP
class SparseVariationalQuasiNewtonGP(VariationalQuasiNewton, SparseGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, Z, opt_z=False, fullcov=True):
self.fullcov = fullcov
super().__init__(kernel, likelihood, X, Y, Z, opt_z=opt_z)
if fullcov:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
np.reshape(self.pseudo_likelihood.covariance, (self.num_data, -1, 1))],
axis=1)
)
dim = self.mean_prev.value.shape[1]
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(dim), [self.num_data, 1, 1]))
else:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
diag(self.pseudo_likelihood.covariance)[..., None]], axis=1)
)
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, 2 * self.func_dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(2 * self.func_dim), [self.num_data, 1, 1]))
class SparseExpectationPropagationQuasiNewtonGP(ExpectationPropagationQuasiNewton, SparseGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, Z, power=1., opt_z=False, fullcov=True):
self.power = power
self.fullcov = fullcov
super().__init__(kernel, likelihood, X, Y, Z, opt_z=opt_z)
if fullcov:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
np.reshape(self.pseudo_likelihood.covariance, (self.num_data, -1, 1))],
axis=1)
)
dim = self.mean_prev.value.shape[1]
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(dim), [self.num_data, 1, 1]))
else:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
diag(self.pseudo_likelihood.covariance)[..., None]], axis=1)
)
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, 2 * self.func_dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(2 * self.func_dim), [self.num_data, 1, 1]))
# class SparsePosteriorLinearisationQuasiNewtonGP(PosteriorLinearisationQuasiNewton, SparseGaussianProcess):
# def __init__(self, kernel, likelihood, X, Y, Z, opt_z=False, fullcov=True):
# self.fullcov = fullcov
# super().__init__(kernel, likelihood, X, Y, Z, opt_z=opt_z)
# if fullcov:
# self.mean_prev = objax.StateVar(
# np.concatenate([self.pseudo_likelihood.mean,
# np.reshape(self.pseudo_likelihood.covariance, (self.num_data, -1, 1))],
# axis=1)
# )
# dim = self.mean_prev.value.shape[1]
# self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, dim, 1]))
# self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(dim), [self.num_data, 1, 1]))
# else:
# self.mean_prev = objax.StateVar(
# np.concatenate([self.pseudo_likelihood.mean,
# diag(self.pseudo_likelihood.covariance)[..., None]], axis=1)
# )
# self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, 2 * self.func_dim, 1]))
# self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(2 * self.func_dim), [self.num_data, 1, 1]))
class SparsePosteriorLinearisation2ndOrderQuasiNewtonGP(PosteriorLinearisation2ndOrderQuasiNewton,
SparseGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, Z, opt_z=False, fullcov=True):
self.fullcov = fullcov
super().__init__(kernel, likelihood, X, Y, Z, opt_z=opt_z)
if fullcov:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
np.reshape(self.pseudo_likelihood.covariance, (self.num_data, -1, 1))],
axis=1)
)
dim = self.mean_prev.value.shape[1]
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(dim), [self.num_data, 1, 1]))
else:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
diag(self.pseudo_likelihood.covariance)[..., None]], axis=1)
)
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, 2 * self.func_dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(2 * self.func_dim), [self.num_data, 1, 1]))
# --- Markov quasi-Newton ---
class MarkovQuasiNewtonGP(QuasiNewton, MarkovGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None):
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
self.mean_prev = objax.StateVar(self.pseudo_likelihood.mean)
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, self.func_dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(self.func_dim), [self.num_data, 1, 1]))
MarkovLaplaceQuasiNewtonGP = MarkovQuasiNewtonGP
class MarkovVariationalQuasiNewtonGP(VariationalQuasiNewton, MarkovGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None, fullcov=True):
self.fullcov = fullcov
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
if fullcov:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
np.reshape(self.pseudo_likelihood.covariance, (self.num_data, -1, 1))],
axis=1)
)
dim = self.mean_prev.value.shape[1]
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(dim), [self.num_data, 1, 1]))
else:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
diag(self.pseudo_likelihood.covariance)[..., None]], axis=1)
)
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, 2 * self.func_dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(2 * self.func_dim), [self.num_data, 1, 1]))
class MarkovExpectationPropagationQuasiNewtonGP(ExpectationPropagationQuasiNewton, MarkovGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, R=None, power=1., parallel=None, fullcov=True):
self.power = power
self.fullcov = fullcov
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
if fullcov:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
np.reshape(self.pseudo_likelihood.covariance, (self.num_data, -1, 1))],
axis=1)
)
dim = self.mean_prev.value.shape[1]
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(dim), [self.num_data, 1, 1]))
else:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
diag(self.pseudo_likelihood.covariance)[..., None]], axis=1)
)
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, 2 * self.func_dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(2 * self.func_dim), [self.num_data, 1, 1]))
# class MarkovPosteriorLinearisationQuasiNewtonGP(PosteriorLinearisationQuasiNewton, MarkovGaussianProcess):
# def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None, fullcov=True):
# self.fullcov = fullcov
# super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
# if fullcov:
# self.mean_prev = objax.StateVar(
# np.concatenate([self.pseudo_likelihood.mean,
# np.reshape(self.pseudo_likelihood.covariance, (self.num_data, -1, 1))],
# axis=1)
# )
# dim = self.mean_prev.value.shape[1]
# self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, dim, 1]))
# self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(dim), [self.num_data, 1, 1]))
# else:
# self.mean_prev = objax.StateVar(
# np.concatenate([self.pseudo_likelihood.mean,
# diag(self.pseudo_likelihood.covariance)[..., None]], axis=1)
# )
# self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, 2 * self.func_dim, 1]))
# self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(2 * self.func_dim), [self.num_data, 1, 1]))
class MarkovPosteriorLinearisation2ndOrderQuasiNewtonGP(PosteriorLinearisation2ndOrderQuasiNewton,
MarkovGaussianProcess):
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None, fullcov=True):
self.fullcov = fullcov
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
if fullcov:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
np.reshape(self.pseudo_likelihood.covariance, (self.num_data, -1, 1))],
axis=1)
)
dim = self.mean_prev.value.shape[1]
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(dim), [self.num_data, 1, 1]))
else:
self.mean_prev = objax.StateVar(
np.concatenate([self.pseudo_likelihood.mean,
diag(self.pseudo_likelihood.covariance)[..., None]], axis=1)
)
self.jacobian_prev = objax.StateVar(np.zeros([self.num_data, 2 * self.func_dim, 1]))
self.hessian_approx = objax.StateVar(-1e2 * np.tile(np.eye(2 * self.func_dim), [self.num_data, 1, 1]))
# PSD constraints via Riemannian gradients
class MarkovVariationalRiemannGP(VariationalInferenceRiemann, MarkovGaussianProcess):
"""
Markov variational Gaussian process: a VGP where the posterior is computed via
(spatio-temporal) filtering and smoothing [1] with PSD constraints via Riemannian gradients [2].
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
:param R: spatial inputs
:param parallel: boolean determining whether to run parallel filtering
[1] Chang, Wilkinson, Khan, Solin: Fast Variational Learning in State Space Gaussian Process Models, MLSP 2020
[2] Lin, Schmidt, Khan: Handling the Positive-Definite Constraint in the Bayesian Learning Rule, ICML 2020
"""
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None):
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
class MarkovExpectationPropagationRiemannGP(ExpectationPropagationRiemann, MarkovGaussianProcess):
"""
Markov EP Gaussian process: an EPGP where the posterior is computed via
(spatio-temporal) filtering and smoothing [1] with PSD constraints via Riemannian gradients [2].
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
:param R: spatial inputs
:param parallel: boolean determining whether to run parallel filtering
[1] Wilkinson, Chang, Riis Andersen, Solin: State Space Expectation Propagation, ICML 2020
[2] Lin, Schmidt, Khan: Handling the Positive-Definite Constraint in the Bayesian Learning Rule, ICML 2020
"""
def __init__(self, kernel, likelihood, X, Y, R=None, power=1., parallel=None):
self.power = power
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
class MarkovNewtonRiemannGP(NewtonRiemann, MarkovGaussianProcess):
"""
Markov Laplace Gaussian process with PSD constraints via Riemannian gradients [1].
:param kernel: a kernel object
:param likelihood: a likelihood object
:param X: inputs
:param Y: observations
:param R: spatial inputs
:param parallel: boolean determining whether to run parallel filtering
[1] Lin, Schmidt, Khan: Handling the Positive-Definite Constraint in the Bayesian Learning Rule, ICML 2020
"""
def __init__(self, kernel, likelihood, X, Y, R=None, parallel=None):
super().__init__(kernel, likelihood, X, Y, R=R, parallel=parallel)
MarkovLaplaceRiemannGP = MarkovNewtonRiemannGP
class TrainableDiagonalGaussianDistribution(objax.Module):
def __init__(self, mean, variance):
self.mean_ = objax.TrainVar(mean)
self.transformed_variance = objax.TrainVar(vmap(softplus_inv)(variance))
def __call__(self):
return self.mean, self.covariance
@property
def mean(self):
return self.mean_.value
@property
def variance(self):
return softplus(self.transformed_variance.value)
@property
def covariance(self):
return vmap(np.diag)(self.variance)
@property
def nat1(self):
chol = cho_factor(self.covariance, lower=True)
return cho_solve(chol, self.mean)
@property
def nat2(self):
chol = cho_factor(self.covariance, lower=True)
return cho_solve(chol, np.tile(np.eye(self.covariance.shape[1]), [self.covariance.shape[0], 1, 1]))
class TrainableGaussianDistribution(objax.Module):
def __init__(self, mean, covariance):
self.dim = mean.shape[1]
cholcov, _ = cho_factor(covariance, lower=True)
self.mean_ = objax.TrainVar(mean)
self.transformed_covariance = objax.TrainVar(vmap(self.get_tril, [0, None])(cholcov, self.dim))
def __call__(self):
return self.mean, self.covariance
@staticmethod
def get_tril(chol, dim):
return chol[np.tril_indices(dim)]
def fill_lower_tri(self, v):
idx = np.tril_indices(self.dim)
return np.zeros((self.dim, self.dim), dtype=v.dtype).at[idx].set(v)
@property
def mean(self):
return self.mean_.value
@property
def covariance(self):
chol_low = vmap(self.fill_lower_tri)(self.transformed_covariance.value)
return transpose(chol_low) @ chol_low
@property
def nat1(self):
chol = cho_factor(self.covariance, lower=True)
return cho_solve(chol, self.mean)
@property
def nat2(self):
chol = cho_factor(self.covariance, lower=True)
return cho_solve(chol, np.tile(np.eye(self.covariance.shape[1]), [self.covariance.shape[0], 1, 1]))
class FirstOrderVariationalGP(VariationalGP):
def __init__(self, kernel, likelihood, X, Y):
super().__init__(kernel, likelihood, X, Y)
if isinstance(self.kernel, Independent):
pseudo_lik_size = self.func_dim # the multi-latent case
else:
pseudo_lik_size = self.obs_dim
# self.pseudo_likelihood = TrainableDiagonalGaussianDistribution(
# mean=np.zeros([self.num_data, pseudo_lik_size, 1]),
# variance=1e2 * np.ones([self.num_data, pseudo_lik_size])
# )
self.pseudo_likelihood = TrainableGaussianDistribution(
mean=np.zeros([self.num_data, pseudo_lik_size, 1]),
covariance=1e2 * np.tile(np.eye(pseudo_lik_size), [self.num_data, 1, 1])
)
def energy(self, **kwargs):
"""
"""
self.update_posterior()
return super().energy(**kwargs)
class FirstOrderMarkovVariationalGP(MarkovVariationalGP):
def __init__(self, kernel, likelihood, X, Y):
super().__init__(kernel, likelihood, X, Y)
if isinstance(self.kernel, Independent):
pseudo_lik_size = self.func_dim # the multi-latent case
else:
pseudo_lik_size = self.obs_dim
self.pseudo_likelihood = TrainableGaussianDistribution(
mean=np.zeros([self.num_data, pseudo_lik_size, 1]),
covariance=1e2 * np.tile(np.eye(pseudo_lik_size), [self.num_data, 1, 1])
)
def energy(self, **kwargs):
"""
"""
self.update_posterior()
return super().energy(**kwargs)