-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPs.html
More file actions
1604 lines (1573 loc) · 99.6 KB
/
GPs.html
File metadata and controls
1604 lines (1573 loc) · 99.6 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
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.8.25">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Xenofon Karakonstantis">
<meta name="author" content="Samuel A. Verburg">
<title>Supervised learning methods - Generative models</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
html { -webkit-text-size-adjust: 100%; }
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
/* CSS for citations */
div.csl-bib-body { }
div.csl-entry {
clear: both;
margin-bottom: 0em;
}
.hanging-indent div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}</style>
<script src="GPs_template_files/libs/clipboard/clipboard.min.js"></script>
<script src="GPs_template_files/libs/quarto-html/quarto.js" type="module"></script>
<script src="GPs_template_files/libs/quarto-html/tabsets/tabsets.js" type="module"></script>
<script src="GPs_template_files/libs/quarto-html/axe/axe-check.js" type="module"></script>
<script src="GPs_template_files/libs/quarto-html/popper.min.js"></script>
<script src="GPs_template_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="GPs_template_files/libs/quarto-html/anchor.min.js"></script>
<link href="GPs_template_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="GPs_template_files/libs/quarto-html/quarto-syntax-highlighting-7b89279ff1a6dce999919e0e67d4d9ec.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="GPs_template_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="GPs_template_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="GPs_template_files/libs/bootstrap/bootstrap-942ed5fa101082e6d1d3257f14fc93b9.min.css" rel="stylesheet" append-hash="true" id="quarto-bootstrap" data-mode="light">
<script src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es6"></script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
<script type="text/javascript">
const typesetMath = (el) => {
if (window.MathJax) {
// MathJax Typeset
window.MathJax.typeset([el]);
} else if (window.katex) {
// KaTeX Render
var mathElements = el.getElementsByClassName("math");
var macros = [];
for (var i = 0; i < mathElements.length; i++) {
var texText = mathElements[i].firstChild;
if (mathElements[i].tagName == "SPAN" && texText && texText.data) {
window.katex.render(texText.data, mathElements[i], {
displayMode: mathElements[i].classList.contains('display'),
throwOnError: false,
macros: macros,
fleqn: false
});
}
}
}
}
window.Quarto = {
typesetMath
};
</script>
</head>
<body class="quarto-light">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<div class="quarto-alternate-formats"><h2>Other Formats</h2><ul><li><a href="GPs_template.html"><i class="bi bi-file-slides"></i>RevealJS (clean)</a></li></ul></div></div>
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Supervised learning methods - Generative models</h1>
<p class="subtitle lead">Non-parametric models (Gaussian Processes)</p>
</div>
<div class="quarto-title-meta-author">
<div class="quarto-title-meta-heading">Authors</div>
<div class="quarto-title-meta-heading">Affiliations</div>
<div class="quarto-title-meta-contents">
<p class="author">Xenofon Karakonstantis </p>
</div>
<div class="quarto-title-meta-contents">
<p class="affiliation">
Demant
</p>
</div>
<div class="quarto-title-meta-contents">
<p class="author">Samuel A. Verburg </p>
</div>
<div class="quarto-title-meta-contents">
<p class="affiliation">
DTU Electro
</p>
</div>
</div>
<div class="quarto-title-meta">
</div>
</header>
<section id="overview" class="level2">
<h2 class="anchored" data-anchor-id="overview">Overview</h2>
<ol type="1">
<li>Linear Regression - From Multivariate Gaussians to Gaussian processes <a href="#sec-regression"><span class="button">Linear Regression</span></a></li>
<li>Gaussian Processes - Theory and Application <a href="#sec-gps"><span class="button">GPs</span></a></li>
<li>The “Physics” - Kernels for Acoustics <a href="#sec-kernels"><span class="button">Kernels</span></a></li>
<li><strong>Sound Field Reconstruction Tutorial (Code):</strong> <a href="#sec-sfrec"><span class="button">Tutorial</span></a>
<ul>
<li>Sampling 2D Priors</li>
<li>Inference & Posterior Prediction</li>
<li>Hyperparameter Optimization (Marginal Likelihood)</li>
</ul></li>
</ol>
</section>
<section id="python-packages" class="level2">
<h2 class="anchored" data-anchor-id="python-packages">Python Packages</h2>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Required Software
</div>
</div>
<div class="callout-body-container callout-body">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>pip install torch gpytorch numpy scipy matplotlib seaborn plotly nbconvert reportlab</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
</div>
</div>
</section>
<section id="sec-regression" class="level1" data-background-color="#40666e">
<h1 data-background-color="#40666e">Linear Regression - From Multivariate Gaussians to Gaussian Processes</h1>
<section id="linear-regression" class="level2">
<h2 class="anchored" data-anchor-id="linear-regression">Linear Regression</h2>
<section id="a-basic-model" class="level3">
<h3 class="anchored" data-anchor-id="a-basic-model">A Basic Model</h3>
<ul>
<li>Consider models of the inputs <span class="math inline">\(\mathbf{x} \in \mathbb{R}^D\)</span> for continuous response variables <span class="math inline">\(y \in \mathbb{R}\)</span> of the form</li>
</ul>
<p><span class="math display">\[
y=f(\mathbf{x})+\epsilon, \quad \epsilon \sim \mathcal{N} \left(\epsilon \mid 0, \sigma^2\right)
\]</span></p>
</section>
</section>
<section id="linear-regression-1" class="level2">
<h2 class="anchored" data-anchor-id="linear-regression-1">Linear Regression</h2>
<section id="linear-parametric-function" class="level3">
<h3 class="anchored" data-anchor-id="linear-parametric-function">Linear Parametric Function</h3>
<ul>
<li>In a typical regression problem, we assume <span class="math inline">\(f\)</span> to be a linear parametric function of the inputs <span class="math inline">\(\mathbf{x}\)</span></li>
</ul>
<p><span class="math display">\[
f(\mathbf{x})=\mathbf{w}^{\top} \mathbf{x}
\]</span></p>
<ul>
<li><span class="math inline">\(\mathbf{w}\)</span> is a <span class="math inline">\(D\)</span>-dimensional vector of parameters (one weight per input dimension)</li>
<li><span class="fg" style="--col: #e64173">Likelihood</span> for a dataset <span class="math inline">\(\mathcal{D}=\left\{\mathbf{x}_n, y_n\right\}_{n=1}^N\)</span> is</li>
</ul>
<p><span class="math display">\[
p(\mathbf{y} \mid \mathbf{w}, \mathbf{X})=\prod_{n=1}^N \mathcal{N}\left(y_n \mid \mathbf{w}^{\top} \mathbf{x}_n, \sigma^2\right)
\]</span></p>
</section>
</section>
<section id="sec-bayesian" class="level2">
<h2 class="anchored" data-anchor-id="sec-bayesian">Bayesian regression</h2>
<section id="prior-and-map-estimation" class="level3">
<h3 class="anchored" data-anchor-id="prior-and-map-estimation">Prior and MAP Estimation</h3>
<ul>
<li>Prior on <span class="math inline">\(\mathbf{w}: p(\mathbf{w})=\mathcal{N}(\mathbf{w} \mid \mathbf{0}, \lambda \mathbf{l})\)</span></li>
</ul>
<p><span class="math display">\[
\hat{\mathbf{w}}_{\mathrm{MAP}}=\arg \max _{\mathbf{w}}\left(\sum_{n=1}^N \log p\left(y_n \mid \mathbf{w}, \mathbf{x}_n\right)+\log p(\mathbf{w})\right)
\]</span></p>
<ul>
<li><span class="alert">MAP estimation</span> gives us regularized point estimates</li>
<li>Term <span class="math inline">\(\log p(\mathbf{w})\)</span> acts as a <span class="fg" style="--col: #e64173">penalty term</span></li>
<li>Point prediction given by <span class="math display">\[
\hat{y}_*=\left(\hat{\mathbf{w}}_{\text {MAP }}\right)^{\top} \mathbf{x}_*
\]</span></li>
</ul>
</section>
</section>
<section id="sec-full-bayesian" class="level2">
<h2 class="anchored" data-anchor-id="sec-full-bayesian">Full Bayesian Approach</h2>
<section id="the-bayesian-way" class="level3">
<h3 class="anchored" data-anchor-id="the-bayesian-way">The Bayesian Way</h3>
<ul>
<li>Treat <span class="math inline">\(\mathbf{w}\)</span> as a latent variable and do inference:</li>
</ul>
<p><span class="math display">\[
p(\mathbf{w} \mid \mathbf{y}, \mathbf{X})=\frac{p(\mathbf{y} \mid \mathbf{w}, \mathbf{X}) p(\mathbf{w})}{p(\mathbf{y} \mid \mathbf{X})}
\]</span></p>
</section>
<section id="predictions" class="level3">
<h3 class="anchored" data-anchor-id="predictions">Predictions</h3>
<ul>
<li><span class="alert">Full posterior</span> distribution instead of point estimate!</li>
<li>Prediction by averaging over <span class="math inline">\(\mathbf{w}\)</span>:</li>
</ul>
<p><span class="math display">\[
p\left(y_* \mid \mathbf{x}_*, \mathbf{y}, \mathbf{X}\right)=\int p\left(y_* \mid \mathbf{w}, \mathbf{x}_*\right) p(\mathbf{w} \mid \mathbf{y}, \mathbf{X}) d \mathbf{w}
\]</span></p>
<p><span class="citation" data-cites="bishop2006pattern">Bishop and Nasrabadi (<a href="#ref-bishop2006pattern" role="doc-biblioref">2006</a>)</span></p>
</section>
</section>
</section>
<section id="sec-gps" class="level1" data-background-color="#40666e">
<h1 data-background-color="#40666e">Gaussian Processes</h1>
<section id="what-is-a-gaussian-process" class="level2 scrollable">
<h2 class="scrollable anchored" data-anchor-id="what-is-a-gaussian-process">What is a Gaussian Process?</h2>
<section id="definition" class="level3">
<h3 class="anchored" data-anchor-id="definition">Definition</h3>
<div class="fragment">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>A Bayesian, non-parametric approach that defines a distribution directly over functions <span class="math inline">\(f(\mathbf{x})\)</span>.</p>
<ul>
<li>Generalizes MVN to infinite-dimensional functions.</li>
<li>Any finite collection follows a joint Gaussian <span class="math inline">\(\mathbf{f} \sim \mathcal{N}(\boldsymbol\mu, \mathbf{K})\)</span></li>
</ul>
<p><span class="citation" data-cites="williams2006gaussian">Williams and Rasmussen (<a href="#ref-williams2006gaussian" role="doc-biblioref">2006</a>)</span></p>
</div>
</div>
</div>
<div class="fragment">
<ul>
<li><strong>Fully specified by two functions</strong>
<ol type="1">
<li><strong><span class="fg" style="--col: #e64173">Mean Function</span> <span class="math inline">\(\mu(\mathbf{x})\)</span></strong> The prior expectation <span class="math inline">\(\mathbb{E}[f(\mathbf{x})]\)</span>.
<ul>
<li>Usually <span class="math inline">\(\mu(\mathbf{x})=0\)</span>.</li>
</ul></li>
<li><strong><span class="fg" style="--col: #e64173">Covariance Function</span> <span class="math inline">\(k(\mathbf{x}, \mathbf{x}')\)</span> (Kernel)</strong>
<ul>
<li>Defines the prior correlation between function values at any two points.</li>
</ul></li>
</ol></li>
</ul>
</div>
<hr>
</section>
</section>
<section id="what-is-a-gaussian-process-1" class="level2">
<h2 class="anchored" data-anchor-id="what-is-a-gaussian-process-1">What is a Gaussian Process?</h2>
<section id="connection-to-bayesian-linear-models" class="level3">
<h3 class="anchored" data-anchor-id="connection-to-bayesian-linear-models">Connection to Bayesian Linear Models</h3>
<p>If <span class="math inline">\(f(\mathbf{x}) = \mathbf{w}^\top \boldsymbol{\phi}(\mathbf{x})\)</span> and <span class="math inline">\(\mathbf{w} \sim \mathcal{N}(0, \Sigma_w)\)</span>,</p>
<p>then the induced prior over functions is</p>
<p><span class="math display">\[
k(\mathbf{x}, \mathbf{x}') =
\boldsymbol{\phi}(\mathbf{x})^\top \Sigma_w \boldsymbol{\phi}(\mathbf{x}')
\]</span></p>
<p>→ Infinite-dimensional features (i.e., basis functions) ⇒ Gaussian Process.</p>
<hr>
</section>
</section>
<section id="the-kernel-function" class="level2">
<h2 class="anchored" data-anchor-id="the-kernel-function">The Kernel function</h2>
<p>The kernel <span class="math inline">\(k(\mathbf{x}, \mathbf{x}')\)</span> determines the spatial properties and must be <span class="alert">positive definite</span></p>
<section id="example-rbf-kernel" class="level3">
<h3 class="anchored" data-anchor-id="example-rbf-kernel">Example: RBF Kernel</h3>
<p>The <strong>Squared Exponential (RBF)</strong> kernel:</p>
<p><span class="math display">\[
k_{\text{RBF}}(\mathbf{x}, \mathbf{x}') =
\sigma_f^2 \exp\!\left(-\frac{\|\mathbf{x}-\mathbf{x}'\|^2}{2\ell^2}\right)
\]</span></p>
<ul>
<li><span class="math inline">\(\ell\)</span>: length-scale — controls smoothness<br>
</li>
<li><span class="math inline">\(\sigma_f^2\)</span>: signal variance — amplitude scale</li>
</ul>
<hr>
</section>
</section>
<section id="the-kernel-function-1" class="level2">
<h2 class="anchored" data-anchor-id="the-kernel-function-1">The Kernel function</h2>
<section id="positive-definiteness" class="level3">
<h3 class="anchored" data-anchor-id="positive-definiteness">Positive Definiteness</h3>
<div class="fragment">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>A valid kernel must satisfy <span class="math display">\[
\sum_{i,j} c_i c_j \, k(\mathbf{x}_i, \mathbf{x}_j) \ge 0 \quad \forall c_i \in \mathbb{R},
\]</span> ensuring <span class="math inline">\(\mathbf K\)</span> is positive semidefinite.</p>
</div>
</div>
</div>
<hr>
</section>
</section>
<section id="example-prior-samples" class="level2 scrollable">
<h2 class="scrollable anchored" data-anchor-id="example-prior-samples">Example: Prior Samples</h2>
<section id="illustration-of-1d-rbf-samples" class="level3">
<h3 class="anchored" data-anchor-id="illustration-of-1d-rbf-samples">Illustration of 1D RBF samples</h3>
<p>Before seeing data, GP prior encodes our belief about plausible functions.</p>
<div id="cell-1d-kernel-prior" class="cell" data-fig-asp="1" data-output-location="column" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> torch, numpy <span class="im">as</span> np</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> matplotlib.pyplot <span class="im">as</span> plt</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> gpytorch</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Input points</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a>X <span class="op">=</span> torch.linspace(<span class="op">-</span><span class="dv">5</span>, <span class="dv">5</span>, <span class="dv">200</span>).unsqueeze(<span class="op">-</span><span class="dv">1</span>)</span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="co"># RBF kernel</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true" tabindex="-1"></a>kernel <span class="op">=</span> gpytorch.kernels.ScaleKernel(gpytorch.kernels.RBFKernel())</span>
<span id="cb2-8"><a href="#cb2-8" aria-hidden="true" tabindex="-1"></a>K <span class="op">=</span> kernel(X, X).evaluate() <span class="op">+</span> <span class="fl">1e-5</span> <span class="op">*</span> torch.eye(X.size(<span class="dv">0</span>))</span>
<span id="cb2-9"><a href="#cb2-9" aria-hidden="true" tabindex="-1"></a><span class="co"># Draw 3 prior samples</span></span>
<span id="cb2-10"><a href="#cb2-10" aria-hidden="true" tabindex="-1"></a>f_prior <span class="op">=</span> gpytorch.distributions.MultivariateNormal(torch.zeros(X.size(<span class="dv">0</span>)), K).sample(torch.Size([<span class="dv">3</span>]))</span>
<span id="cb2-11"><a href="#cb2-11" aria-hidden="true" tabindex="-1"></a><span class="co"># plot</span></span>
<span id="cb2-12"><a href="#cb2-12" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">7</span>,<span class="dv">4</span>))</span>
<span id="cb2-13"><a href="#cb2-13" aria-hidden="true" tabindex="-1"></a>plt.plot(X.numpy(), f_prior.t().numpy())</span>
<span id="cb2-14"><a href="#cb2-14" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Samples from GP Prior (RBF Kernel)"</span>)</span>
<span id="cb2-15"><a href="#cb2-15" aria-hidden="true" tabindex="-1"></a>plt.xlabel(<span class="st">"x"</span>)<span class="op">;</span> plt.ylabel(<span class="st">"f(x)"</span>)</span>
<span id="cb2-16"><a href="#cb2-16" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-display">
<div id="d-kernel-prior" class="quarto-figure quarto-figure-center anchored">
<figure class="figure">
<p><img src="GPs_template_files/figure-html/d-kernel-prior-output-1.png" width="600" height="376" class="figure-img"></p>
<figcaption>RBF Kernel Samples</figcaption>
</figure>
</div>
</div>
</div>
<hr>
</section>
</section>
<section id="gp-regression-training-theory" class="level2">
<h2 class="anchored" data-anchor-id="gp-regression-training-theory">GP Regression — Training (Theory)</h2>
<p>For a Gaussian likelihood <span class="math display">\[
p(\mathbf y|\mathbf f) = \mathcal{N}(\mathbf y|\mathbf f, \sigma_n^2\mathbf I),
\]</span> and a GP prior <span class="math display">\[
\mathbf f \sim \mathcal{N}(0, \mathbf K)
\]</span></p>
</section>
<section id="gp-regression-training-theory-1" class="level2">
<h2 class="anchored" data-anchor-id="gp-regression-training-theory-1">GP Regression — Training (Theory)</h2>
<section id="loss-function" class="level3">
<h3 class="anchored" data-anchor-id="loss-function">Loss function</h3>
<p>the <strong>marginal likelihood</strong> (evidence) is <span class="math display">\[
p(\mathbf y|\mathbf X, \theta)
= \mathcal{N}\big(\mathbf y\big|0, \mathbf K + \sigma_n^2 \mathbf I \big).
\]</span></p>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>Marginal log-likelihood <span class="math display">\[
\log p(\mathbf y|\theta)
= -\tfrac{1}{2}\mathbf y^\top(\mathbf K+\sigma_n^2\mathbf I)^{-1}\mathbf y
-\tfrac{1}{2}\log\!\big|\mathbf K+\sigma_n^2\mathbf I\big|
-\tfrac{n}{2}\log(2\pi).
\]</span></p>
</div>
</div>
</section>
</section>
<section id="gp-regression-training-theory-2" class="level2">
<h2 class="anchored" data-anchor-id="gp-regression-training-theory-2">GP Regression — Training (Theory)</h2>
<section id="optimisation" class="level3">
<h3 class="anchored" data-anchor-id="optimisation">Optimisation</h3>
<p>Training minimises <span class="math display">\[
\mathcal{L}(\theta) = -\log p(\mathbf y|\theta),
\]</span> by gradient descent on kernel hyperparameters <span class="math inline">\(\theta = \{\ell, \sigma_f, \sigma_n\}\)</span>.</p>
</section>
</section>
<section id="why-marginal-likelihood" class="level2">
<h2 class="anchored" data-anchor-id="why-marginal-likelihood">Why Marginal Likelihood?</h2>
<div class="fragment">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>MLL decomposition (Occam’s Razor):</p>
<ul>
<li><p>Data fit: <span class="math inline">\(-\tfrac{1}{2}\mathbf y^\top(\mathbf K+\sigma_n^2\mathbf I)^{-1}\mathbf y\)</span></p></li>
<li><p>Complexity: <span class="math inline">\(-\tfrac{1}{2}\log\!\big|\mathbf K+\sigma_n^2\mathbf I\big|\)</span></p></li>
<li><p>Constant: <span class="math inline">\(-\tfrac{n}{2}\log(2\pi)\)</span></p></li>
</ul>
</div>
</div>
</div>
<div class="fragment">
<p><strong>Benefit:</strong> No need for cross-validation - the marginal likelihood is computed on training data only.</p>
</div>
</section>
<section id="gp-regression-in-1d" class="level2">
<h2 class="anchored" data-anchor-id="gp-regression-in-1d">GP Regression in 1D</h2>
<section id="code-example" class="level3">
<h3 class="anchored" data-anchor-id="code-example">Code example</h3>
<div id="de24f012" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> torch, gpytorch, matplotlib.pyplot <span class="im">as</span> plt</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>torch.manual_seed(<span class="dv">0</span>)</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Training data</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>X_train <span class="op">=</span> torch.linspace(<span class="op">-</span><span class="dv">3</span>, <span class="dv">3</span>, <span class="dv">25</span>)</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a>y_train <span class="op">=</span> torch.sin(X_train <span class="op">*</span> <span class="dv">2</span>) <span class="op">+</span> <span class="fl">0.3</span> <span class="op">*</span> torch.randn(X_train.size())</span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a><span class="co"># GP model setup</span></span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> ExactGPModel(gpytorch.models.ExactGP):</span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, train_x, train_y, likelihood):</span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a> <span class="bu">super</span>().<span class="fu">__init__</span>(train_x, train_y, likelihood)</span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.mean_module <span class="op">=</span> gpytorch.means.ZeroMean()</span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.covar_module <span class="op">=</span> gpytorch.kernels.ScaleKernel(</span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a> gpytorch.kernels.RBFKernel()</span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> forward(<span class="va">self</span>, x):</span>
<span id="cb3-17"><a href="#cb3-17" aria-hidden="true" tabindex="-1"></a> mean_x <span class="op">=</span> <span class="va">self</span>.mean_module(x)</span>
<span id="cb3-18"><a href="#cb3-18" aria-hidden="true" tabindex="-1"></a> covar_x <span class="op">=</span> <span class="va">self</span>.covar_module(x)</span>
<span id="cb3-19"><a href="#cb3-19" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> gpytorch.distributions.MultivariateNormal(mean_x, covar_x)</span>
<span id="cb3-20"><a href="#cb3-20" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-21"><a href="#cb3-21" aria-hidden="true" tabindex="-1"></a>likelihood <span class="op">=</span> gpytorch.likelihoods.GaussianLikelihood()</span>
<span id="cb3-22"><a href="#cb3-22" aria-hidden="true" tabindex="-1"></a>model <span class="op">=</span> ExactGPModel(X_train, y_train, likelihood)</span>
<span id="cb3-23"><a href="#cb3-23" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-24"><a href="#cb3-24" aria-hidden="true" tabindex="-1"></a>model.train()<span class="op">;</span> likelihood.train()</span>
<span id="cb3-25"><a href="#cb3-25" aria-hidden="true" tabindex="-1"></a>optimizer <span class="op">=</span> torch.optim.Adam(model.parameters(), lr<span class="op">=</span><span class="fl">0.1</span>)</span>
<span id="cb3-26"><a href="#cb3-26" aria-hidden="true" tabindex="-1"></a>mll <span class="op">=</span> gpytorch.mlls.ExactMarginalLogLikelihood(likelihood, model)</span>
<span id="cb3-27"><a href="#cb3-27" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-28"><a href="#cb3-28" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">200</span>):</span>
<span id="cb3-29"><a href="#cb3-29" aria-hidden="true" tabindex="-1"></a> optimizer.zero_grad()</span>
<span id="cb3-30"><a href="#cb3-30" aria-hidden="true" tabindex="-1"></a> output <span class="op">=</span> model(X_train)</span>
<span id="cb3-31"><a href="#cb3-31" aria-hidden="true" tabindex="-1"></a> loss <span class="op">=</span> <span class="op">-</span>mll(output, y_train)</span>
<span id="cb3-32"><a href="#cb3-32" aria-hidden="true" tabindex="-1"></a> loss.backward()</span>
<span id="cb3-33"><a href="#cb3-33" aria-hidden="true" tabindex="-1"></a> optimizer.step()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
</div>
<hr>
</section>
</section>
<section id="sec-gp-regression" class="level2">
<h2 class="anchored" data-anchor-id="sec-gp-regression">GP Regression - Posterior Prediction (Theory)</h2>
<section id="regression-with-a-gp-prior" class="level3">
<h3 class="anchored" data-anchor-id="regression-with-a-gp-prior">Regression with a GP Prior</h3>
<div class="fragment">
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>After training, for test inputs (<span class="math inline">\(\mathbf X_*\)</span>) <span class="math display">\[
\begin{aligned}
\mathbf f_* \mid \mathbf X_*, \mathbf X, \mathbf y
&\sim \mathcal{N}(\bar{\mathbf f}_*, \mathbf V_*), \\
\bar{\mathbf f}_* &= \mathbf K_*^\top (\mathbf K + \sigma_n^2 \mathbf I)^{-1} \mathbf y, \\
\mathbf V_* &= \mathbf K_{**} - \mathbf K_*^\top (\mathbf K + \sigma_n^2 \mathbf I)^{-1} \mathbf K_*.
\end{aligned}
\]</span> With observation noise <span class="math display">\[
p(\mathbf y_*|\mathbf X_*,\mathbf X,\mathbf y)
= \mathcal{N}\!\big(\bar{\mathbf f}_*, \mathbf V_* + \sigma_n^2 \mathbf I\big).
\]</span></p>
</div>
</div>
</div>
<hr>
</section>
</section>
<section id="gp-regression-posterior-prediction-code" class="level2 scrollable">
<h2 class="scrollable anchored" data-anchor-id="gp-regression-posterior-prediction-code">GP Regression — Posterior Prediction (Code)</h2>
<div id="cell-1d-kernel-pred" class="cell" data-fig-asp="1" data-output-location="column" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Put in eval mode for prediction</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>model.<span class="bu">eval</span>()</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a>likelihood.<span class="bu">eval</span>()</span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Test points</span></span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a>X_test <span class="op">=</span> torch.linspace(<span class="op">-</span><span class="dv">3</span>, <span class="dv">3</span>, <span class="dv">200</span>)</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Predictive distribution</span></span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> torch.no_grad(), gpytorch.settings.fast_pred_var():</span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a> pred <span class="op">=</span> likelihood(model(X_test))</span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a>mean <span class="op">=</span> pred.mean</span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a>lower, upper <span class="op">=</span> pred.confidence_region()</span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a><span class="co"># Plot predictions with uncertainty</span></span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a>plt.figure(figsize<span class="op">=</span>(<span class="dv">8</span>, <span class="dv">4</span>))</span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a>plt.plot(X_train.numpy(), y_train.numpy(), <span class="st">'k*'</span>)</span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a>plt.plot(X_test.numpy(), mean.numpy(), <span class="st">'b'</span>)</span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a>plt.fill_between(X_test.numpy(), lower.numpy(), upper.numpy(), alpha<span class="op">=</span><span class="fl">0.3</span>)</span>
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true" tabindex="-1"></a>plt.title(<span class="st">"Posterior Mean and 95% Confidence Interval"</span>)</span>
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-display">
<div id="d-kernel-pred" class="quarto-figure quarto-figure-center anchored">
<figure class="figure">
<p><img src="GPs_template_files/figure-html/d-kernel-pred-output-1.png" width="656" height="357" class="figure-img"></p>
<figcaption>1D RBF Kernel prediction</figcaption>
</figure>
</div>
</div>
</div>
<hr>
</section>
<section id="nomenclature" class="level2">
<h2 class="anchored" data-anchor-id="nomenclature">Nomenclature</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 37%">
<col style="width: 29%">
<col style="width: 32%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Symbol</th>
<th style="text-align: left;">Meaning</th>
<th style="text-align: left;">Code</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><span class="math inline">\(\bar{\mathbf f}_*\)</span></td>
<td style="text-align: left;">Posterior mean</td>
<td style="text-align: left;"><code>pred.mean</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><span class="math inline">\(\mathbf V_*\)</span></td>
<td style="text-align: left;">Posterior covariance</td>
<td style="text-align: left;"><code>pred.variance</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><span class="math inline">\(\mathbf K_*, \mathbf K_{**}\)</span></td>
<td style="text-align: left;">Cross/test covariances</td>
<td style="text-align: left;"><code>model.covar_module(x)</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><span class="math inline">\(\sigma_n^2\)</span></td>
<td style="text-align: left;">Observation noise</td>
<td style="text-align: left;"><code>likelihood.noise</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><span class="math inline">\(\mathbf y_*\)</span></td>
<td style="text-align: left;">Noisy predictive samples</td>
<td style="text-align: left;"><code>likelihood(model(X_test))</code></td>
</tr>
</tbody>
</table>
<hr>
</section>
</section>
<section id="sec-kernels" class="level1" data-background-color="#40666e">
<h1 data-background-color="#40666e">Kernels for Acoustics</h1>
<section id="kernels-for-acoustics" class="level2">
<h2 class="anchored" data-anchor-id="kernels-for-acoustics">Kernels for Acoustics</h2>
<section id="common-kernels" class="level3">
<h3 class="anchored" data-anchor-id="common-kernels">Common Kernels</h3>
<div style="font-size: 0.7em">
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Kernel Name</th>
<th style="text-align: left;">Acoustic Relevance</th>
<th style="text-align: left;">Formula</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><strong>Isotropic RBF (Squared Exponential)</strong></td>
<td style="text-align: left;">Assumes infinite smoothness; isotropic energy.</td>
<td style="text-align: left;"><span class="math inline">\(k_{SE}(\mathbf{x},\mathbf{x}') = \sigma_f^2 \exp\left(-\frac{\|\mathbf{x}-\mathbf{x}'\|^2}{2\ell^2}\right)\)</span></td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>Anisotropic RBF (ARD)</strong></td>
<td style="text-align: left;">Allows different <strong>lengthscales (<span class="math inline">\(\ell_d\)</span>)</strong> for each spatial dimension.</td>
<td style="text-align: left;"><span class="math inline">\(k_{Aniso}(\mathbf{x},\mathbf{x}') \propto \exp\left( -\sum_{d} \frac{(x_d - x_d')^2}{2\ell_d^2} \right)\)</span></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><strong>Periodic</strong></td>
<td style="text-align: left;">Explicitly models repeating patterns, i.e., standing waves or room modes.</td>
<td style="text-align: left;"><span class="math inline">\(k_{PER}(\mathbf{x},\mathbf{x}') \propto \exp\left( -\frac{2\sin^2(\frac{\pi}{p} \|\mathbf{x}-\mathbf{x}'\|)}{\ell^2} \right)\)</span></td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>Bessel Kernel</strong></td>
<td style="text-align: left;"><strong>Physics-Informed Prior.</strong> Derived solving the Helmholtz equation</td>
<td style="text-align: left;"><span class="math inline">\(k_{Bessel}(\mathbf{x},\mathbf{x}') \propto J_0(\kappa \|\mathbf{x}-\mathbf{x}'\|)\)</span></td>
</tr>
</tbody>
</table>
</div>
<p><span class="citation" data-cites="caviedes2021gaussian">Caviedes-Nozal et al. (<a href="#ref-caviedes2021gaussian" role="doc-biblioref">2021</a>)</span></p>
</section>
<section id="properties" class="level3">
<h3 class="anchored" data-anchor-id="properties">Properties</h3>
<div class="fragment">
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Composition: kernels can be added or multiplied to encode richer priors.</p>
</div>
</div>
</div>
<div class="fragment">
<ul>
<li>Each kernel encodes different <span class="fg" style="--col: #e64173">prior beliefs</span> about the sound field</li>
</ul>
</div>
</section>
</section>
</section>
<section id="sec-sfrec" class="level1" data-background-color="#40666e">
<h1 data-background-color="#40666e">Tutorial: Sound Field Reconstruction</h1>
<section id="sec-demo" class="level2">
<h2 class="anchored" data-anchor-id="sec-demo">Tutorial: Sound Field Reconstruction</h2>
<section id="code-setup---simulate-sound-fields" class="level3">
<h3 class="anchored" data-anchor-id="code-setup---simulate-sound-fields">Code Setup - Simulate Sound Fields</h3>
<div id="434b2087" class="cell" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> utils <span class="im">import</span> create_2d_grid, plot_2d_field, adjustSNR, plane_wave_field</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> math, torch, numpy <span class="im">as</span> np</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> matplotlib.pyplot <span class="im">as</span> plt</span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Spatial grid</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a>test_pts, xx, yy <span class="op">=</span> create_2d_grid(n_pts_per_dim<span class="op">=</span><span class="dv">50</span>)</span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a>n_pts <span class="op">=</span> test_pts.shape[<span class="dv">0</span>]</span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a><span class="co"># Acoustic parameters</span></span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a>freq <span class="op">=</span> <span class="dv">125</span> <span class="co"># Hz</span></span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a>c <span class="op">=</span> <span class="fl">343.0</span> <span class="co"># m/s</span></span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a>omega <span class="op">=</span> <span class="dv">2</span> <span class="op">*</span> math.pi <span class="op">*</span> freq</span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a>k <span class="op">=</span> omega <span class="op">/</span> c <span class="co"># Wavenumber</span></span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a><span class="co"># Single plane wave</span></span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a>k_vec <span class="op">=</span> k <span class="op">*</span> np.array([<span class="fl">1.0</span>, <span class="fl">0.0</span>])</span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a>pf_single <span class="op">=</span> plane_wave_field(test_pts.numpy(), k_vec, omega)</span>
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a><span class="co"># Superposition of 4 plane waves</span></span>
<span id="cb5-20"><a href="#cb5-20" aria-hidden="true" tabindex="-1"></a>k_vecs <span class="op">=</span> k <span class="op">*</span> np.array([[<span class="dv">1</span>,<span class="dv">0</span>],[<span class="dv">0</span>,<span class="dv">1</span>],[<span class="op">-</span><span class="dv">1</span>,<span class="dv">0</span>],[<span class="dv">0</span>,<span class="op">-</span><span class="dv">1</span>]])</span>
<span id="cb5-21"><a href="#cb5-21" aria-hidden="true" tabindex="-1"></a>amplitudes <span class="op">=</span> np.array([<span class="fl">1.0</span>, <span class="fl">0.8</span>, <span class="fl">0.6</span>, <span class="fl">0.4</span>])</span>
<span id="cb5-22"><a href="#cb5-22" aria-hidden="true" tabindex="-1"></a>pf_super <span class="op">=</span> <span class="bu">sum</span>(amplitudes[i] <span class="op">*</span> plane_wave_field(test_pts.numpy(), k_vecs[i], omega)</span>
<span id="cb5-23"><a href="#cb5-23" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="bu">len</span>(amplitudes)))</span>
<span id="cb5-24"><a href="#cb5-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-25"><a href="#cb5-25" aria-hidden="true" tabindex="-1"></a><span class="co"># Add noise</span></span>
<span id="cb5-26"><a href="#cb5-26" aria-hidden="true" tabindex="-1"></a>snr_db <span class="op">=</span> <span class="dv">20</span></span>
<span id="cb5-27"><a href="#cb5-27" aria-hidden="true" tabindex="-1"></a>pf_single <span class="op">=</span> adjustSNR(pf_single, snr_db, td<span class="op">=</span><span class="va">False</span>)</span>
<span id="cb5-28"><a href="#cb5-28" aria-hidden="true" tabindex="-1"></a>pf_super <span class="op">=</span> adjustSNR(pf_super, snr_db, td<span class="op">=</span><span class="va">False</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
</div>
</section>
</section>
<section id="tutorial-sound-field-reconstruction" class="level2">
<h2 class="anchored" data-anchor-id="tutorial-sound-field-reconstruction">Tutorial: Sound Field Reconstruction</h2>
<section id="sound-field-measurements" class="level3">
<h3 class="anchored" data-anchor-id="sound-field-measurements">Sound Field Measurements</h3>
<div id="cell-measurements-sf" class="cell" data-fig-asp="1" data-output-location="column" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Select random measurement locations</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>n_meas <span class="op">=</span> <span class="dv">25</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a>meas_indices <span class="op">=</span> np.random.choice(n_pts, n_meas, replace<span class="op">=</span><span class="va">False</span>)</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a>meas_pts <span class="op">=</span> test_pts[meas_indices]</span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a>pf_true <span class="op">=</span> pf_super</span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a>meas_values <span class="op">=</span> pf_true[meas_indices]</span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a>fig, ax <span class="op">=</span> plt.subplots(figsize<span class="op">=</span>(<span class="dv">6</span>, <span class="dv">5</span>))</span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a>plot_2d_field(xx, yy, pf_true.real.reshape(xx.shape),</span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a> <span class="st">"True Field with Measurement Points"</span>, ax<span class="op">=</span>ax)</span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a>ax.scatter(meas_pts[:, <span class="dv">0</span>], meas_pts[:, <span class="dv">1</span>], </span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a> c<span class="op">=</span><span class="st">"red"</span>, marker<span class="op">=</span><span class="st">"x"</span>, s<span class="op">=</span><span class="dv">100</span>, label<span class="op">=</span><span class="ss">f"N=</span><span class="sc">{</span>n_meas<span class="sc">}</span><span class="ss"> Measurements"</span>)</span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a>ax.legend()</span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a>plt.show()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-display">
<div id="measurements-sf" class="quarto-figure quarto-figure-center anchored">
<figure class="figure">
<p><img src="GPs_template_files/figure-html/measurements-sf-output-1.png" width="515" height="449" class="figure-img"></p>
<figcaption>‘Experimental’ Sound Field</figcaption>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="visualise-sound-fields" class="level2">
<h2 class="anchored" data-anchor-id="visualise-sound-fields">Visualise Sound Fields</h2>
<div id="f4729760" class="cell" data-execution_count="6">
<div class="cell-output cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="GPs_template_files/figure-html/cell-7-output-1.png" width="1127" height="374" class="figure-img"></p>
<figcaption>Acoustic fields at 125 Hz</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="define-kernels" class="level2">
<h2 class="anchored" data-anchor-id="define-kernels">Define Kernels</h2>
<div id="58fc49a8" class="cell" data-execution_count="7">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> kernel_fncs <span class="im">import</span> (</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a> IsotropicRBFKernel,</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> RotationalAnisotropicRBFKernel,</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> BesselKernel,</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> RealPartPlaneWaveKernel,</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> ImagPartPlaneWaveKernel,</span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> RBFxPeriodicKernel</span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> gpytorch.kernels <span class="im">import</span> ScaleKernel</span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> gpytorch</span>
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a><span class="co"># Isotropic RBF</span></span>
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a>kernel_iso <span class="op">=</span> IsotropicRBFKernel(params<span class="op">=</span>{<span class="st">'scale'</span>: <span class="fl">1.0</span>})</span>
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a><span class="co"># Anisotropic RBF (different lengthscales in x and y)</span></span>
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a>kernel_aniso <span class="op">=</span> ScaleKernel(</span>
<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a> RotationalAnisotropicRBFKernel(ard_num_dims<span class="op">=</span><span class="dv">2</span>, Q_matrix<span class="op">=</span><span class="va">None</span>)</span>
<span id="cb7-19"><a href="#cb7-19" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb7-20"><a href="#cb7-20" aria-hidden="true" tabindex="-1"></a>kernel_aniso.outputscale <span class="op">=</span> <span class="fl">1.0</span></span>
<span id="cb7-21"><a href="#cb7-21" aria-hidden="true" tabindex="-1"></a>kernel_aniso.base_kernel.lengthscale <span class="op">=</span> torch.tensor([<span class="fl">1.0</span>, <span class="fl">3.0</span>])</span>
<span id="cb7-22"><a href="#cb7-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-23"><a href="#cb7-23" aria-hidden="true" tabindex="-1"></a><span class="co"># Periodic RBF</span></span>
<span id="cb7-24"><a href="#cb7-24" aria-hidden="true" tabindex="-1"></a>wavelength <span class="op">=</span> <span class="dv">2</span> <span class="op">*</span> np.pi <span class="op">/</span> k</span>
<span id="cb7-25"><a href="#cb7-25" aria-hidden="true" tabindex="-1"></a>kernel_periodic <span class="op">=</span> RBFxPeriodicKernel(</span>
<span id="cb7-26"><a href="#cb7-26" aria-hidden="true" tabindex="-1"></a> period<span class="op">=</span>wavelength,</span>
<span id="cb7-27"><a href="#cb7-27" aria-hidden="true" tabindex="-1"></a> lengthscale<span class="op">=</span><span class="fl">2.0</span>,</span>
<span id="cb7-28"><a href="#cb7-28" aria-hidden="true" tabindex="-1"></a> outputscale<span class="op">=</span><span class="fl">1.0</span></span>
<span id="cb7-29"><a href="#cb7-29" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb7-30"><a href="#cb7-30" aria-hidden="true" tabindex="-1"></a><span class="co"># Bessel kernel (wave equation solution)</span></span>
<span id="cb7-31"><a href="#cb7-31" aria-hidden="true" tabindex="-1"></a>kernel_bessel <span class="op">=</span> BesselKernel(params<span class="op">=</span>{<span class="st">'sigma'</span>: <span class="fl">1.0</span>, <span class="st">'k'</span>: k})</span>
<span id="cb7-32"><a href="#cb7-32" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-33"><a href="#cb7-33" aria-hidden="true" tabindex="-1"></a><span class="co"># Plane wave kernel</span></span>
<span id="cb7-34"><a href="#cb7-34" aria-hidden="true" tabindex="-1"></a>n_basis <span class="op">=</span> <span class="dv">8</span></span>
<span id="cb7-35"><a href="#cb7-35" aria-hidden="true" tabindex="-1"></a>angles <span class="op">=</span> np.linspace(<span class="dv">0</span>, <span class="dv">2</span><span class="op">*</span>np.pi, n_basis, endpoint<span class="op">=</span><span class="va">False</span>)</span>
<span id="cb7-36"><a href="#cb7-36" aria-hidden="true" tabindex="-1"></a>directions <span class="op">=</span> torch.tensor(np.column_stack([np.cos(angles), np.sin(angles)]), </span>
<span id="cb7-37"><a href="#cb7-37" aria-hidden="true" tabindex="-1"></a> dtype<span class="op">=</span>torch.float32)</span>
<span id="cb7-38"><a href="#cb7-38" aria-hidden="true" tabindex="-1"></a>params_pw <span class="op">=</span> {<span class="st">'sigma_l_init'</span>: <span class="fl">1.0</span>, <span class="st">'k'</span>: k}</span>
<span id="cb7-39"><a href="#cb7-39" aria-hidden="true" tabindex="-1"></a>kernel_pw_real <span class="op">=</span> RealPartPlaneWaveKernel(params_pw, directions)</span>
<span id="cb7-40"><a href="#cb7-40" aria-hidden="true" tabindex="-1"></a>kernel_pw_imag <span class="op">=</span> ImagPartPlaneWaveKernel(params_pw, directions)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
</div>
</section>
<section id="sample-from-kernel-priors" class="level2">
<h2 class="anchored" data-anchor-id="sample-from-kernel-priors">Sample from kernel priors</h2>
<div id="8ed9d669" class="cell" data-execution_count="8">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Sample from isotropic RBF prior</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> torch.no_grad():</span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> K_iso <span class="op">=</span> kernel_iso(test_pts, test_pts) <span class="op">+</span> <span class="fl">1e-4</span> <span class="op">*</span> torch.eye(n_pts)</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a>prior_sample_iso <span class="op">=</span> gpytorch.distributions.MultivariateNormal(</span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a> torch.zeros(n_pts), K_iso</span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a>).sample()</span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a><span class="co"># Sample from anisotropic RBF prior</span></span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> torch.no_grad():</span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a> K_aniso <span class="op">=</span> kernel_aniso(test_pts).evaluate() <span class="op">+</span> <span class="fl">1e-4</span> <span class="op">*</span> torch.eye(n_pts)</span>
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a>prior_sample_aniso <span class="op">=</span> gpytorch.distributions.MultivariateNormal(</span>
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a> torch.zeros(n_pts), K_aniso</span>
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a>).sample()</span>
<span id="cb8-16"><a href="#cb8-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-17"><a href="#cb8-17" aria-hidden="true" tabindex="-1"></a><span class="co"># Sample from periodic RBF prior</span></span>
<span id="cb8-18"><a href="#cb8-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-19"><a href="#cb8-19" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> torch.no_grad():</span>
<span id="cb8-20"><a href="#cb8-20" aria-hidden="true" tabindex="-1"></a> K_per <span class="op">=</span> kernel_periodic(test_pts).evaluate() <span class="op">+</span> <span class="fl">1e-4</span> <span class="op">*</span> torch.eye(n_pts)</span>
<span id="cb8-21"><a href="#cb8-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-22"><a href="#cb8-22" aria-hidden="true" tabindex="-1"></a>prior_sample_period <span class="op">=</span> gpytorch.distributions.MultivariateNormal(</span>
<span id="cb8-23"><a href="#cb8-23" aria-hidden="true" tabindex="-1"></a> torch.zeros(n_pts), K_per</span>
<span id="cb8-24"><a href="#cb8-24" aria-hidden="true" tabindex="-1"></a>).sample()</span>
<span id="cb8-25"><a href="#cb8-25" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-26"><a href="#cb8-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-27"><a href="#cb8-27" aria-hidden="true" tabindex="-1"></a><span class="co"># Sample from Bessel prior</span></span>
<span id="cb8-28"><a href="#cb8-28" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> torch.no_grad():</span>
<span id="cb8-29"><a href="#cb8-29" aria-hidden="true" tabindex="-1"></a> K_bessel <span class="op">=</span> kernel_bessel(test_pts, test_pts) <span class="op">+</span> <span class="fl">1e-2</span> <span class="op">*</span> torch.eye(n_pts)</span>
<span id="cb8-30"><a href="#cb8-30" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb8-31"><a href="#cb8-31" aria-hidden="true" tabindex="-1"></a>prior_sample_bessel <span class="op">=</span> gpytorch.distributions.MultivariateNormal(</span>
<span id="cb8-32"><a href="#cb8-32" aria-hidden="true" tabindex="-1"></a> torch.zeros(n_pts), K_bessel</span>
<span id="cb8-33"><a href="#cb8-33" aria-hidden="true" tabindex="-1"></a>).sample()</span>
<span id="cb8-34"><a href="#cb8-34" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-35"><a href="#cb8-35" aria-hidden="true" tabindex="-1"></a><span class="co"># Sample from plane wave prior</span></span>
<span id="cb8-36"><a href="#cb8-36" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> torch.no_grad():</span>
<span id="cb8-37"><a href="#cb8-37" aria-hidden="true" tabindex="-1"></a> K_pw <span class="op">=</span> kernel_pw_real(test_pts, test_pts) <span class="op">+</span> <span class="fl">1e-2</span> <span class="op">*</span> torch.eye(n_pts)</span>
<span id="cb8-38"><a href="#cb8-38" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb8-39"><a href="#cb8-39" aria-hidden="true" tabindex="-1"></a>prior_sample_pw <span class="op">=</span> gpytorch.distributions.MultivariateNormal(</span>
<span id="cb8-40"><a href="#cb8-40" aria-hidden="true" tabindex="-1"></a> torch.zeros(n_pts), K_pw</span>
<span id="cb8-41"><a href="#cb8-41" aria-hidden="true" tabindex="-1"></a>).sample()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
</div>
</section>
<section id="visualise-those-priors-over-the-sound-fields" class="level2">
<h2 class="anchored" data-anchor-id="visualise-those-priors-over-the-sound-fields">Visualise those priors over the sound fields</h2>
<div class="r-fit-text">
<div id="e578d41e" class="cell" data-execution_count="9">
<div class="cell-output cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="GPs_template_files/figure-html/cell-10-output-1.png" width="1419" height="662" class="figure-img"></p>
<figcaption>Samples from different kernel priors</figcaption>
</figure>
</div>
</div>
</div>
<p><strong>Observation:</strong> Each kernel imposes different spatial structure!</p>
</div>
</section>
<section id="plane-wave-kernel-prior" class="level2 smaller">
<h2 class="smaller anchored" data-anchor-id="plane-wave-kernel-prior">Plane Wave Kernel Prior</h2>
<ul>
<li>With wavenumber <span class="math inline">\(k\)</span> and basis directions <span class="math inline">\(\{ \mathbf{d}_l \}_{l=1}^L\)</span></li>
</ul>
<p><span class="math display">\[
f(\mathbf x) \approx \sum_{l=1}^L w_l e^{-j k \mathbf d_l^\top \mathbf x},\qquad
w_l \sim \mathcal{CN}(0,\sigma_l)
\]</span></p>
<ul>
<li>Real / imaginary block kernels</li>
</ul>
<p><span class="math display">\[
K_{rr}(\mathbf x,\mathbf x') = \sum_{l=1}^L \frac{\sigma_l}{2}\cos\!\big(k\,\mathbf d_l^\top(\mathbf x-\mathbf x')\big)
\]</span> <span class="math display">\[
K_{ri}(\mathbf x,\mathbf x') = \sum_{l=1}^L \frac{\sigma_l}{2}\sin\!\big(k\,\mathbf d_l^\top(\mathbf x-\mathbf x')\big)
\]</span></p>
<p>(same for <span class="math inline">\(K_{ii}=K_{rr}\)</span>; cross-term is <span class="math inline">\(K_{ri}\)</span>.)</p>
<hr>
</section>
<section id="plane-wave-kernel-prior-1" class="level2 scrollable">
<h2 class="scrollable anchored" data-anchor-id="plane-wave-kernel-prior-1">Plane Wave Kernel Prior</h2>
<ul>
<li>Stack real and imaginary parts into a real vector <span class="math display">\[
\mathbf f_{\mathrm{stack}}(\mathbf x) =
\begin{bmatrix}
\Re\{f(\mathbf x)\} \\
\Im\{f(\mathbf x)\}
\end{bmatrix}
\]</span> then the joint covariance between stacked vectors at two sets of points has the block form <span class="math display">\[
K(\mathbf x,\mathbf x') =
\begin{bmatrix}
K_{rr}(\mathbf x,\mathbf x') & K_{ri}(\mathbf x,\mathbf x') \\
K_{ri}(\mathbf x,\mathbf x')^\top & K_{rr}(\mathbf x,\mathbf x')
\end{bmatrix},
\]</span> where each block (e.g. <span class="math inline">\(K_{rr},K_{ri}\)</span>) is an <span class="math inline">\(N\times N\)</span> covariance matrix. Evaluating this on <span class="math inline">\(N\)</span> spatial locations produces a <span class="math inline">\(2N\times 2N\)</span> covariance matrix.</li>
</ul>
<hr>
</section>
<section id="sound-field-reconstruction" class="level2 scrollable">
<h2 class="scrollable anchored" data-anchor-id="sound-field-reconstruction">Sound Field Reconstruction</h2>
<section id="training-a-rotational-anisotropic-rbf-gp" class="level3">
<h3 class="anchored" data-anchor-id="training-a-rotational-anisotropic-rbf-gp">Training a Rotational Anisotropic RBF GP</h3>
<div id="be7df00b" class="cell" data-execution_count="10">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> torch</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> torch.optim</span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> gpytorch.kernels <span class="im">import</span> ScaleKernel</span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> kernel_fncs <span class="im">import</span> RotationalAnisotropicRBFKernel, plot_gp_posterior, plot_complex_gp_posterior</span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a><span class="co"># 1. Prepare data for a single-output (real part) GP</span></span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a>X_train <span class="op">=</span> meas_pts</span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a>y_train <span class="op">=</span> torch.tensor(meas_values.real, dtype<span class="op">=</span>torch.float32)</span>
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a><span class="co"># 2. Define the Anisotropic RBF Kernel and model (re-initializing for optimization)</span></span>
<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a><span class="co"># Use a fresh kernel instance with initial parameters</span></span>
<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a>optimized_kernel_aniso <span class="op">=</span> ScaleKernel(</span>
<span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a> RotationalAnisotropicRBFKernel(ard_num_dims<span class="op">=</span><span class="dv">2</span>, Q_matrix<span class="op">=</span><span class="va">None</span>)</span>
<span id="cb9-15"><a href="#cb9-15" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb9-16"><a href="#cb9-16" aria-hidden="true" tabindex="-1"></a><span class="co"># Set initial values (can be arbitrary)</span></span>
<span id="cb9-17"><a href="#cb9-17" aria-hidden="true" tabindex="-1"></a>optimized_kernel_aniso.outputscale <span class="op">=</span> torch.tensor(<span class="fl">1.0</span>)</span>
<span id="cb9-18"><a href="#cb9-18" aria-hidden="true" tabindex="-1"></a>optimized_kernel_aniso.base_kernel.lengthscale <span class="op">=</span> torch.tensor([<span class="fl">1.0</span>, <span class="fl">1.0</span>]) <span class="co"># Start isotropic</span></span>
<span id="cb9-19"><a href="#cb9-19" aria-hidden="true" tabindex="-1"></a>likelihood <span class="op">=</span> gpytorch.likelihoods.GaussianLikelihood()</span>
<span id="cb9-20"><a href="#cb9-20" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-21"><a href="#cb9-21" aria-hidden="true" tabindex="-1"></a><span class="kw">class</span> OptimizedGP2D(gpytorch.models.ExactGP):</span>
<span id="cb9-22"><a href="#cb9-22" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> <span class="fu">__init__</span>(<span class="va">self</span>, X_train, y_train, likelihood, kernel):</span>
<span id="cb9-23"><a href="#cb9-23" aria-hidden="true" tabindex="-1"></a> <span class="bu">super</span>().<span class="fu">__init__</span>(X_train, y_train, likelihood)</span>
<span id="cb9-24"><a href="#cb9-24" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.mean_module <span class="op">=</span> gpytorch.means.ZeroMean()</span>
<span id="cb9-25"><a href="#cb9-25" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.covar_module <span class="op">=</span> kernel</span>
<span id="cb9-26"><a href="#cb9-26" aria-hidden="true" tabindex="-1"></a> <span class="kw">def</span> forward(<span class="va">self</span>, x):</span>
<span id="cb9-27"><a href="#cb9-27" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> gpytorch.distributions.MultivariateNormal(</span>
<span id="cb9-28"><a href="#cb9-28" aria-hidden="true" tabindex="-1"></a> <span class="va">self</span>.mean_module(x), <span class="va">self</span>.covar_module(x)</span>
<span id="cb9-29"><a href="#cb9-29" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb9-30"><a href="#cb9-30" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-31"><a href="#cb9-31" aria-hidden="true" tabindex="-1"></a>model_opt <span class="op">=</span> OptimizedGP2D(X_train, y_train, likelihood, optimized_kernel_aniso)</span>
<span id="cb9-32"><a href="#cb9-32" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-33"><a href="#cb9-33" aria-hidden="true" tabindex="-1"></a><span class="co"># 3. Training/Optimization setup</span></span>
<span id="cb9-34"><a href="#cb9-34" aria-hidden="true" tabindex="-1"></a>model_opt.train()<span class="op">;</span> likelihood.train()</span>
<span id="cb9-35"><a href="#cb9-35" aria-hidden="true" tabindex="-1"></a>optimizer <span class="op">=</span> torch.optim.Adam(model_opt.parameters(), lr<span class="op">=</span><span class="fl">0.1</span>)</span>
<span id="cb9-36"><a href="#cb9-36" aria-hidden="true" tabindex="-1"></a>mll <span class="op">=</span> gpytorch.mlls.ExactMarginalLogLikelihood(likelihood, model_opt)</span>
<span id="cb9-37"><a href="#cb9-37" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-38"><a href="#cb9-38" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Initial lengthscales: </span><span class="sc">{</span>model_opt<span class="sc">.</span>covar_module<span class="sc">.</span>base_kernel<span class="sc">.</span>lengthscale<span class="sc">.</span>data<span class="sc">.</span>numpy()<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb9-39"><a href="#cb9-39" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Initial noise (sigma_n): </span><span class="sc">{</span>math<span class="sc">.</span>sqrt(model_opt.likelihood.noise.data.item())<span class="sc">:.4f}</span><span class="ss">"</span>)</span>
<span id="cb9-40"><a href="#cb9-40" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-41"><a href="#cb9-41" aria-hidden="true" tabindex="-1"></a><span class="co"># 4. Optimization loop</span></span>
<span id="cb9-42"><a href="#cb9-42" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> i <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">100</span>):</span>
<span id="cb9-43"><a href="#cb9-43" aria-hidden="true" tabindex="-1"></a> optimizer.zero_grad()</span>
<span id="cb9-44"><a href="#cb9-44" aria-hidden="true" tabindex="-1"></a> out <span class="op">=</span> model_opt(X_train)</span>
<span id="cb9-45"><a href="#cb9-45" aria-hidden="true" tabindex="-1"></a> loss <span class="op">=</span> <span class="op">-</span>mll(out, y_train)</span>
<span id="cb9-46"><a href="#cb9-46" aria-hidden="true" tabindex="-1"></a> loss.backward()</span>
<span id="cb9-47"><a href="#cb9-47" aria-hidden="true" tabindex="-1"></a> <span class="co"># if (i+1) % 20 == 0:</span></span>
<span id="cb9-48"><a href="#cb9-48" aria-hidden="true" tabindex="-1"></a> <span class="co"># print(f"Iter {i+1}: Loss={loss.item():.3f}, ls={model_opt.covar_module.base_kernel.lengthscale.data.numpy()}")</span></span>
<span id="cb9-49"><a href="#cb9-49" aria-hidden="true" tabindex="-1"></a> optimizer.step()</span>
<span id="cb9-50"><a href="#cb9-50" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-51"><a href="#cb9-51" aria-hidden="true" tabindex="-1"></a><span class="co"># 5. Print final optimized parameters</span></span>
<span id="cb9-52"><a href="#cb9-52" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"</span><span class="ch">\n</span><span class="ss">Final optimized lengthscales: </span><span class="sc">{</span>model_opt<span class="sc">.</span>covar_module<span class="sc">.</span>base_kernel<span class="sc">.</span>lengthscale<span class="sc">.</span>data<span class="sc">.</span>numpy()<span class="sc">}</span><span class="ss">"</span>)</span>
<span id="cb9-53"><a href="#cb9-53" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Final optimized signal variance (sigma_f^2): </span><span class="sc">{</span>model_opt<span class="sc">.</span>covar_module<span class="sc">.</span>outputscale<span class="sc">.</span>data<span class="sc">.</span>item()<span class="sc">:.4f}</span><span class="ss">"</span>)</span>
<span id="cb9-54"><a href="#cb9-54" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(<span class="ss">f"Final optimized noise (sigma_n): </span><span class="sc">{</span>math<span class="sc">.</span>sqrt(model_opt.likelihood.noise.data.item())<span class="sc">:.4f}</span><span class="ss">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Initial lengthscales: [[1. 1.]]
Initial noise (sigma_n): 0.8326