-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGANs.html
More file actions
1421 lines (1357 loc) · 74.3 KB
/
Copy pathGANs.html
File metadata and controls
1421 lines (1357 loc) · 74.3 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 lang="en"><head>
<script src="GANs_files/libs/clipboard/clipboard.min.js"></script>
<script src="GANs_files/libs/quarto-html/tabby.min.js"></script>
<script src="GANs_files/libs/quarto-html/popper.min.js"></script>
<script src="GANs_files/libs/quarto-html/tippy.umd.min.js"></script>
<link href="GANs_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="GANs_files/libs/quarto-html/light-border.css" rel="stylesheet">
<link href="GANs_files/libs/quarto-html/quarto-syntax-highlighting-7b89279ff1a6dce999919e0e67d4d9ec.css" rel="stylesheet" id="quarto-text-highlighting-styles"><meta charset="utf-8">
<meta name="generator" content="quarto-1.8.25">
<meta name="author" content="Xenofon Karakonstantis">
<meta name="author" content="Samuel A. Verburg">
<title>Supervised learning methods - Generative models</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="GANs_files/libs/revealjs/dist/reset.css">
<link rel="stylesheet" href="GANs_files/libs/revealjs/dist/reveal.css">
<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;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ color: #003b4f; background-color: #f1f3f5; }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span { color: #003b4f; } /* Normal */
code span.al { color: #ad0000; } /* Alert */
code span.an { color: #5e5e5e; } /* Annotation */
code span.at { color: #657422; } /* Attribute */
code span.bn { color: #ad0000; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #003b4f; font-weight: bold; } /* ControlFlow */
code span.ch { color: #20794d; } /* Char */
code span.cn { color: #8f5902; } /* Constant */
code span.co { color: #5e5e5e; } /* Comment */
code span.cv { color: #5e5e5e; font-style: italic; } /* CommentVar */
code span.do { color: #5e5e5e; font-style: italic; } /* Documentation */
code span.dt { color: #ad0000; } /* DataType */
code span.dv { color: #ad0000; } /* DecVal */
code span.er { color: #ad0000; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #ad0000; } /* Float */
code span.fu { color: #4758ab; } /* Function */
code span.im { color: #00769e; } /* Import */
code span.in { color: #5e5e5e; } /* Information */
code span.kw { color: #003b4f; font-weight: bold; } /* Keyword */
code span.op { color: #5e5e5e; } /* Operator */
code span.ot { color: #003b4f; } /* Other */
code span.pp { color: #ad0000; } /* Preprocessor */
code span.sc { color: #5e5e5e; } /* SpecialChar */
code span.ss { color: #20794d; } /* SpecialString */
code span.st { color: #20794d; } /* String */
code span.va { color: #111111; } /* Variable */
code span.vs { color: #20794d; } /* VerbatimString */
code span.wa { color: #5e5e5e; font-style: italic; } /* Warning */
/* 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>
<link rel="stylesheet" href="GANs_files/libs/revealjs/dist/theme/quarto-5ae9c59b2727fcd5357d4f034d95f9e0.css">
<link href="GANs_files/libs/revealjs/plugin/quarto-line-highlight/line-highlight.css" rel="stylesheet">
<link href="GANs_files/libs/revealjs/plugin/reveal-menu/menu.css" rel="stylesheet">
<link href="GANs_files/libs/revealjs/plugin/reveal-menu/quarto-menu.css" rel="stylesheet">
<link href="GANs_files/libs/revealjs/plugin/quarto-support/footer.css" rel="stylesheet">
<style type="text/css">
.reveal div.sourceCode {
margin: 0;
overflow: auto;
}
.reveal div.hanging-indent {
margin-left: 1em;
text-indent: -1em;
}
.reveal .slide:not(.center) {
height: 100%;
}
.reveal .slide.scrollable {
overflow-y: auto;
}
.reveal .footnotes {
height: 100%;
overflow-y: auto;
}
.reveal .slide .absolute {
position: absolute;
display: block;
}
.reveal .footnotes ol {
counter-reset: ol;
list-style-type: none;
margin-left: 0;
}
.reveal .footnotes ol li:before {
counter-increment: ol;
content: counter(ol) ". ";
}
.reveal .footnotes ol li > p:first-child {
display: inline-block;
}
.reveal .slide ul,
.reveal .slide ol {
margin-bottom: 0.5em;
}
.reveal .slide ul li,
.reveal .slide ol li {
margin-top: 0.4em;
margin-bottom: 0.2em;
}
.reveal .slide ul[role="tablist"] li {
margin-bottom: 0;
}
.reveal .slide ul li > *:first-child,
.reveal .slide ol li > *:first-child {
margin-block-start: 0;
}
.reveal .slide ul li > *:last-child,
.reveal .slide ol li > *:last-child {
margin-block-end: 0;
}
.reveal .slide .columns:nth-child(3) {
margin-block-start: 0.8em;
}
.reveal blockquote {
box-shadow: none;
}
.reveal .tippy-content>* {
margin-top: 0.2em;
margin-bottom: 0.7em;
}
.reveal .tippy-content>*:last-child {
margin-bottom: 0.2em;
}
.reveal .slide > img.stretch.quarto-figure-center,
.reveal .slide > img.r-stretch.quarto-figure-center {
display: block;
margin-left: auto;
margin-right: auto;
}
.reveal .slide > img.stretch.quarto-figure-left,
.reveal .slide > img.r-stretch.quarto-figure-left {
display: block;
margin-left: 0;
margin-right: auto;
}
.reveal .slide > img.stretch.quarto-figure-right,
.reveal .slide > img.r-stretch.quarto-figure-right {
display: block;
margin-left: auto;
margin-right: 0;
}
</style>
</head>
<body class="quarto-light">
<script src="mathjax-config.js"></script>
<div class="reveal">
<div class="slides">
<section id="title-slide" data-background-image="figs/logo_without_leaf-1.png" data-background-position="bottom 70px right 100px" data-background-size="30%" class="quarto-title-block center">
<h1 class="title">Supervised learning methods - Generative models</h1>
<p class="subtitle">Generative Adversarial Networks</p>
<div class="quarto-title-authors">
<div class="quarto-title-author">
<div class="quarto-title-author-name">
Xenofon Karakonstantis
</div>
<p class="quarto-title-affiliation">
Demant
</p>
</div>
<div class="quarto-title-author">
<div class="quarto-title-author-name">
Samuel A. Verburg
</div>
<p class="quarto-title-affiliation">
DTU Electro
</p>
</div>
</div>
</section>
<section id="generative-adversarial-networks-gans" class="slide level2">
<h2>Generative Adversarial Networks (GANs)</h2>
<h3 id="motivation-why-gans">Motivation: Why GANs?</h3>
<h4 id="limitations-of-likelihood-based-models"><strong>Limitations of likelihood-based models</strong></h4>
<ul>
<li>VAEs: tractable likelihood <span class="math inline">\(p_\theta(\mathbf{x}|\mathbf{z})\)</span> but samples might not have the best quality due to variational approximation</li>
<li>Autoregressive models are nice but slow in generation (sequential)</li>
</ul>
<h4 id="gan-approach"><strong>GAN approach</strong></h4>
<ul>
<li>Learn an <em>implicit</em> generative model through adversarial training</li>
<li>Generator <span class="math inline">\(G_\theta: \mathcal{Z} \to \mathcal{X}\)</span> transforms simple noise to complex data</li>
</ul>
<p><span class="math display">\[
\mathbf{x} = G_\theta(\mathbf{z}), \quad \mathbf{z} \sim p_z = \mathcal{N}(0, I)
\]</span></p>
<aside class="notes">
<ul>
<li>Implicit model: no tractable likelihood, we learn pushforward of Gaussian latent via adversarial signals.</li>
<li>Contrast: VAEs optimize ELBO (blurry), autoregressive = exact but slow sampling.</li>
<li>Adversarial game targets distributional alignment, not pointwise reconstruction.</li>
<li>Latent prior choice (usually isotropic Gaussian) encourages smooth interpolation manifold.</li>
</ul>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
</section>
<section id="gan-architecture" class="slide level2 smaller">
<h2>GAN Architecture</h2>
<p><img data-src="./figs/simple_gan.png" style="width:60.0%" data-fig-cap="GAN architecture: Generator produces samples, Discriminator distinguishes real from fake" alt="Simple GAN architecture"></p>
<ul>
<li><p>Replace maximum likelihood with a discriminative objective</p>
<ul>
<li>Generator <span class="math inline">\(G\)</span> learns to fool discriminator</li>
<li>Discriminator <span class="math inline">\(D\)</span> learns to distinguish real from generated samples</li>
<li>Training converges when <span class="math inline">\(D\)</span> cannot distinguish (Nash equilibrium)</li>
</ul></li>
</ul>
<aside><div>
<p>Figure adapted from www.solulab.com</p>
</div></aside></section>
<section id="gans-and-minimax-game" class="slide level2">
<h2>GANs and “Minimax” Game</h2>
<h4 id="objective-function"><strong>Objective function</strong></h4>
<p><span class="math display">\[
\min_G \max_D \; V(D,G) = \mathbb{E}_{\mathbf{x}\sim p_{data}}[\log D(\mathbf{x})] + \mathbb{E}_{\mathbf{z}\sim p_z}[\log(1 - D(G(\mathbf{z})))]
\]</span></p>
<h4 id="interpretation"><strong>Interpretation</strong></h4>
<ul>
<li><span class="math inline">\(D(\mathbf{x}) \in [0,1]\)</span> is the probability that <span class="math inline">\(\mathbf{x}\)</span> is real</li>
<li>Discriminator maximizes for correct classification of real vs. fake</li>
<li>Generator minimizes for probability of discriminator being correct on fake samples</li>
</ul>
<aside class="notes">
<ul>
<li>Objective = two-player minimax; saddle point sought with alternating stochastic gradients.</li>
<li>Discriminator: logistic classifier; generator: maps latent noise to data space.</li>
</ul>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
</section>
<section id="gans-and-minimax-game-1" class="slide level2">
<h2>GANs and “Minimax” Game</h2>
<h4 id="training-alternates-between"><strong>Training alternates between</strong></h4>
<ol type="1">
<li>Update <span class="math inline">\(D\)</span>: maximize <span class="math inline">\(V(D,G)\)</span> for fixed <span class="math inline">\(G\)</span></li>
<li>Update <span class="math inline">\(G\)</span>: minimize <span class="math inline">\(V(D,G)\)</span> for fixed <span class="math inline">\(D\)</span></li>
</ol>
<p><span class="citation" data-cites="goodfellow2014generative">(<a href="#/references" role="doc-biblioref" onclick="">Goodfellow et al. 2014</a>)</span></p>
<aside class="notes">
<ul>
<li>Architecture can be minimal: MLP/CNN symmetry often helps; avoid excessive discriminator capacity early.</li>
<li>Training instantiates alternating (approximate) best responses—no global convexity.</li>
</ul>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
</section>
<section id="optimal-discriminator-derivation" class="slide level2 smaller">
<h2>Optimal Discriminator Derivation</h2>
<p>For fixed <span class="math inline">\(G\)</span>, the optimal discriminator <span class="math inline">\(D^*\)</span> maximises</p>
<p><span class="math display">\[
V(D,G) = \int_{\mathbf{x}} p_{data}(\mathbf{x})\log D(\mathbf{x}) + p_G(\mathbf{x})\log(1-D(\mathbf{x})) \, d\mathbf{x}
\]</span></p>
<p>Taking the functional derivative and setting to zero</p>
<p><span class="math display">\[
\frac{\delta V}{\delta D(\mathbf{x})} = \frac{p_{data}(\mathbf{x})}{D(\mathbf{x})} - \frac{p_G(\mathbf{x})}{1-D(\mathbf{x})} = 0
\]</span></p>
<p>Finally</p>
<p><span class="math display">\[
D^*(\mathbf{x}) = \frac{p_{data}(\mathbf{x})}{p_{data}(\mathbf{x}) + p_G(\mathbf{x})}
\]</span></p>
<ul>
<li>Optimal discriminator “encodes” the density ratio between real and generated distributions.</li>
</ul>
<aside class="notes">
<ul>
<li>Goal: for fixed generator, find D that maximizes expected log-likelihood over real + fake samples.</li>
<li>Write objective pointwise; optimize each x independently (integral separates).</li>
<li>Set derivative wrt D(x) to zero ⇒ solve simple rational equation.</li>
<li>Result is density ratio: probability real divided by total (real + fake).</li>
</ul>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
</section>
<section id="generator-objective-is-the-js-divergence" class="slide level2 smaller">
<h2>Generator Objective is the JS Divergence</h2>
<p>Substituting <span class="math inline">\(D^*\)</span> into the value function</p>
<p><span class="math display">\[
\begin{align}
V(D^*,G) &= \mathbb{E}_{\mathbf{x}\sim p_{data}}\left[\log \frac{p_{data}(\mathbf{x})}{p_{data}(\mathbf{x}) + p_G(\mathbf{x})}\right] + \mathbb{E}_{\mathbf{x}\sim p_G}\left[\log \frac{p_G(\mathbf{x})}{p_{data}(\mathbf{x}) + p_G(\mathbf{x})}\right] \\
&= \text{KL}\left(p_{data} \,\|\, \frac{p_{data}+p_G}{2}\right) + \text{KL}\left(p_G \,\|\, \frac{p_{data}+p_G}{2}\right) - \log 4 \\
&= 2 \cdot \text{JS}(p_{data} \| p_G) - \log 4
\end{align}
\]</span></p>
<h4 id="jensen-shannon-divergence"><strong>Jensen-Shannon divergence</strong></h4>
<p><span class="math display">\[
\text{JS}(p \| q) = \frac{1}{2}\text{KL}\left(p \,\|\, \frac{p+q}{2}\right) + \frac{1}{2}\text{KL}\left(q \,\|\, \frac{p+q}{2}\right)
\]</span></p>
<ul>
<li>At equilibrium, GAN training minimises JS divergence.</li>
<li>Ideally <span class="math inline">\(p_G = p_{data}\)</span> with <span class="math inline">\(D^*(\mathbf{x}) = \frac{1}{2}\)</span> everywhere.</li>
</ul>
<aside class="notes">
<ul>
<li>At equilibrium p_G = p_data ⇒ D*(x)=1/2 everywhere (discriminator becomes maximally uncertain).</li>
<li>Provides intuition: training discriminator well approximates local ratios; generator then adjusts to flatten these toward 1/2.</li>
</ul>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
</section>
<section id="training-instabilities" class="slide level2">
<h2>Training Instabilities</h2>
<h3 id="vanishing-gradients">Vanishing Gradients</h3>
<ul>
<li>When <span class="math inline">\(D\)</span> is optimal early in training, gradients for <span class="math inline">\(G\)</span> vanish.</li>
</ul>
<p>If <span class="math inline">\(p_G\)</span> and <span class="math inline">\(p_{data}\)</span> have disjoint support (common early in training),</p>
<p><span class="math display">\[
D^*(\mathbf{x}) = \begin{cases} 1 & \text{if } \mathbf{x} \in \text{supp}(p_{data}) \\ 0 & \text{if } \mathbf{x} \in \text{supp}(p_G) \end{cases},
\]</span></p>
<p>Then <span class="math inline">\(\log(1-D^*(G(\mathbf{z}))) \approx 0\)</span> which leads to vanishing gradients for <span class="math inline">\(G\)</span>.</p>
<aside class="notes">
<ul>
<li>Early disjoint supports: discriminator becomes perfect ⇒ generator sees near-zero gradient.</li>
<li>Pathology aggravated by high-capacity D, small batch, improper init.</li>
<li>Mitigations: label smoothing, adding noise (instance noise), weaker D schedule.</li>
</ul>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
</section>
<section id="training-instabilities-1" class="slide level2">
<h2>Training Instabilities</h2>
<h3 id="non-saturating-alternative"><strong>Non-saturating alternative</strong></h3>
<p>Instead of minimising <span class="math inline">\(\mathbb{E}_{\mathbf{z}}[\log(1-D(G(\mathbf{z})))]\)</span>, maximise</p>
<p><span class="math display">\[
\mathcal{L}_G = \mathbb{E}_{\mathbf{z}\sim p_z}[\log D(G(\mathbf{z}))]
\]</span></p>
<ul>
<li>Same fixed point (both maximized when <span class="math inline">\(D(G(\mathbf{z})) = \frac{1}{2}\)</span>)</li>
<li>Provides stronger gradients when <span class="math inline">\(D\)</span> is confident</li>
<li>No longer minimises JS divergence, but KL<span class="math inline">\((p_G \| p_{data})\)</span></li>
</ul>
<aside class="notes">
<ul>
<li>Non-saturating trick flips objective sign to amplify gradients when D confident.</li>
<li>Changes implicit divergence toward forward KL flavor around optimum.</li>
<li>Empirically accelerates convergence without altering fixed point set.</li>
</ul>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
</section>
<section id="wasserstein-gan" class="slide level2">
<h2>Wasserstein GAN</h2>
<h3 id="just-a-better-loss-function">Just a Better Loss Function</h3>
<h4 id="earth-mover-wasserstein-1-distance"><strong>Earth-Mover (Wasserstein-1) distance</strong></h4>
<p><span class="math display">\[
W_1(p_{data}, p_G) = \inf_{\gamma \in \Pi(p_{data}, p_G)} \mathbb{E}_{(\mathbf{x},\mathbf{y})\sim\gamma} \|\mathbf{x} - \mathbf{y}\|
\]</span></p>
<p>where <span class="math inline">\(\Pi(p_{data}, p_G)\)</span> is the set of all joint distributions with marginals <span class="math inline">\(p_{data}\)</span> and <span class="math inline">\(p_G\)</span>.</p>
<div id="cell-fig-w1-1d" class="cell" data-execution_count="1">
<div class="cell-output cell-output-display">
<div id="fig-w1-1d" class="quarto-float quarto-figure quarto-figure-center">
<figure class="quarto-float quarto-float-fig">
<div aria-describedby="fig-w1-1d-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img data-src="GANs_files/figure-revealjs/fig-w1-1d-output-1.png" width="950" height="278">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-w1-1d-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure 1: 1D Wasserstein-1 distance: shaded area between CDFs equals W1 (≈ mean transport cost).
</figcaption>
</figure>
</div>
</div>
</div>
<aside class="notes">
<ul>
<li>I promise this will make sense afterwards.</li>
<li>What do we want from GANs? To match distributions!</li>
<li>Earth-mover distance equation: inf (greatest lower bound) over couplings γ chooses optimal “transport plan” moving fake mass onto real with minimal total cost ||x−y||.</li>
<li>Intuition: How much “work” needed to reshape p_G into p_data; large support shifts ⇒ larger distance.</li>
<li>Hard directly because we must search over all joint distributions with correct marginals.</li>
</ul>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
</section>
<section id="wasserstein-gan-1" class="slide level2">
<h2>Wasserstein GAN</h2>
<h3 id="move-one-distribution-closer-to-the-other-with-the-least-cost">Move one distribution closer to the other, with the least cost!</h3>
<h4 id="kantorovich-rubinstein-duality"><strong>Kantorovich-Rubinstein duality</strong></h4>
<p><span class="math display">\[
W_1(p_{data}, p_G) = \sup_{\|f\|_L \leq 1} \mathbb{E}_{\mathbf{x}\sim p_{data}}[f(\mathbf{x})] - \mathbb{E}_{\mathbf{x}\sim p_G}[f(\mathbf{x})]
\]</span></p>
<p>where <span class="math inline">\(\|f\|_L \leq 1\)</span> means <span class="math inline">\(f\)</span> is 1-Lipschitz continuous.<sup>1</sup></p>
<aside class="notes">
<ul>
<li>I was a bit liberal with the nomenclature here, but this is what people use in the literature (f could be D)</li>
<li>Dual form replaces search over γ with a supremum (least upper bound) over 1-Lipschitz critic f: critic assigns higher scores to real vs fake.</li>
<li>Lipschitz bound (||f(x) − f(y)|| ≤ ||x − y||) prevents critic from exploding scores—otherwise distance meaningless.</li>
<li>the Better we enforce Lipschitz constraint (penalty / spectral norm), the better W1 estimate correlates with sample quality.</li>
</ul>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
<aside><div>
<p>A 1‑Lipschitz function means that if you nudge the input a little, the output moves by at most the same amount—no sudden jumps or spikes. (Formally <span class="math inline">\(\|f(x) − f(y)\| \le \|x − y\|\)</span>).</p>
</div><ol class="aside-footnotes"><li id="fn1"><p><span class="citation" data-cites="arjovsky2017wasserstein">Arjovsky, Chintala, and Bottou (<a href="#/references" role="doc-biblioref" onclick="">2017</a>)</span></p></li></ol></aside></section>
<section id="wgan-training-objective" class="slide level2 smaller">
<h2>WGAN Training Objective</h2>
<h4 id="practical-formulation"><strong>Practical formulation</strong></h4>
<p><span class="math display">\[
\min_G \max_{D \in \mathcal{D}} \; \mathbb{E}_{\mathbf{x}\sim p_{data}}[D(\mathbf{x})] - \mathbb{E}_{\mathbf{z}\sim p_z}[D(G(\mathbf{z}))]
\]</span></p>
<p>where <span class="math inline">\(\mathcal{D}\)</span> is the set of 1-Lipschitz functions.</p>
<h4 id="key-differences-from-vanilla-gan"><strong>Key differences from vanilla GAN</strong></h4>
<ol type="1">
<li><span class="math inline">\(D\)</span> is now a <strong>critic</strong> (no sigmoid, outputs real values)</li>
<li>Must enforce Lipschitz constraint on <span class="math inline">\(D\)</span></li>
<li>Provides meaningful loss metric (approximates Wasserstein distance)</li>
</ol>
<h4 id="why-is-this-good">Why is this good?</h4>
<ul>
<li>Continuous, differentiable loss even with disjoint supports</li>
<li>No mode collapse (in theory 🫥)</li>
<li>Loss (kinda-) correlates with sample quality</li>
<li>More stable training</li>
</ul>
</section>
<section id="enforcing-lipschitz-constraints" class="slide level2">
<h2>Enforcing Lipschitz Constraints</h2>
<h4 id="main-approaches"><strong>Main approaches</strong></h4>
<ol type="1">
<li><strong>Weight clipping</strong> (original WGAN) <span class="math inline">\(w \leftarrow \text{clip}(w, -c, c)\)</span>
<ul>
<li>Simple but leads to optimization difficulties</li>
<li>Biases weights toward extreme values</li>
</ul></li>
</ol>
<aside class="notes">
<ul>
<li>Weight clipping: very simple but blunt; it squeezes the critic’s weights, harms expressiveness, and biases training.</li>
<li>Gradient penalty: adds a cost so the critic’s gradient norm stays near 1 on points between real and fake, keeping it smooth.</li>
<li>Interpolation trick: drawing straight lines between real and generated samples is a practical shortcut to enforce smoothness where transport likely happens.</li>
<li>Spectral normalization: scale each layer’s weight matrix by its largest singular value so the layer cannot change outputs too sharply (enforces smoothness).</li>
<li>Cheaper than gradient penalty (single power iteration) and stabilizes large conditional GANs.</li>
<li>Can still add a light gradient or Lipschitz penalty for extra safety if needed.</li>
<li>Reference corpus anchors historical evolution: adversarial → stability (WGAN/SN) → conditional / domain-specific adaptations.</li>
</ul>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
<ol start="2" type="1">
<li><strong>Gradient penalty</strong> (WGAN-GP)<span class="citation" data-cites="gulrajani2017improved">(<a href="#/references" role="doc-biblioref" onclick="">Gulrajani et al. 2017</a>)</span> <span class="math display">\[
\mathcal{L}_D = \mathbb{E}_{\tilde{\mathbf{x}}}[D(\tilde{\mathbf{x}})] - \mathbb{E}_{\mathbf{x}}[D(\mathbf{x})] + \lambda \mathbb{E}_{\hat{\mathbf{x}}}\left[(\|\nabla_{\hat{\mathbf{x}}} D(\hat{\mathbf{x}})\|_2 - 1)^2\right]
\]</span> where <span class="math inline">\(\hat{\mathbf{x}} = \epsilon \mathbf{x} + (1-\epsilon)\tilde{\mathbf{x}}\)</span> with <span class="math inline">\(\epsilon \sim U[0,1]\)</span></li>
</ol>
</section>
<section id="enforcing-lipschitz-constraints-1" class="slide level2">
<h2>Enforcing Lipschitz Constraints</h2>
<h4 id="main-approaches-1"><strong>Main approaches</strong></h4>
<ol start="3" type="1">
<li><strong>Spectral normalization</strong> (SN-GAN)<span class="citation" data-cites="miyato2018spectral">(<a href="#/references" role="doc-biblioref" onclick="">Miyato et al. 2018</a>)</span>
<ul>
<li>Normalize weights by their largest singular value
<ul>
<li><span class="math inline">\(W_{SN} = W/\sigma(W)\)</span></li>
</ul></li>
<li>Ensures <span class="math inline">\(\|D\|_L \leq 1\)</span> by constraining operator norm</li>
</ul></li>
</ol>
</section>
<section id="comparison-of-gan-loss-functions" class="slide level2 smaller">
<h2>Comparison of GAN Loss Functions</h2>
<table class="caption-top">
<colgroup>
<col style="width: 11%">
<col style="width: 24%">
<col style="width: 20%">
<col style="width: 26%">
<col style="width: 17%">
</colgroup>
<thead>
<tr class="header">
<th>Variant</th>
<th>Discriminator Loss</th>
<th>Generator Loss</th>
<th>Distance/Divergence</th>
<th>Key Property</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Vanilla GAN</strong></td>
<td><span class="math inline">\(-\mathbb{E}[\log D(\mathbf{x})] - \newline \mathbb{E}[\log(1-D(G(\mathbf{z})))]\)</span></td>
<td><span class="math inline">\(-\mathbb{E}[\log D(G(\mathbf{z}))]\)</span></td>
<td>KL<span class="math inline">\((p_G \| p_{data})\)</span></td>
<td>Non-saturating</td>
</tr>
<tr class="even">
<td><strong>Minimax GAN</strong></td>
<td><span class="math inline">\(-\mathbb{E}[\log D(\mathbf{x})] - \newline \mathbb{E}[\log(1-D(G(\mathbf{z})))]\)</span></td>
<td><span class="math inline">\(\mathbb{E}[\log(1-D(G(\mathbf{z})))]\)</span></td>
<td>JS<span class="math inline">\((p_{data} \| p_G)\)</span></td>
<td>Saturating gradients</td>
</tr>
<tr class="odd">
<td><strong>LSGAN</strong></td>
<td><span class="math inline">\(\frac{1}{2}\mathbb{E}[(D(\mathbf{x})-1)^2 + \newline D(G(\mathbf{z}))^2]\)</span></td>
<td><span class="math inline">\(\frac{1}{2}\mathbb{E}[(D(G(\mathbf{z}))-1)^2]\)</span></td>
<td>Pearson <span class="math inline">\(\chi^2\)</span></td>
<td>Stable, no sigmoid</td>
</tr>
<tr class="even">
<td><strong>Hinge GAN</strong></td>
<td><span class="math inline">\(\mathbb{E}[\max(0,1-D(\mathbf{x}))] + \newline \mathbb{E}[\max(0,1+D(G(\mathbf{z})))]\)</span></td>
<td><span class="math inline">\(-\mathbb{E}[D(G(\mathbf{z}))]\)</span></td>
<td>Margin-based</td>
<td>Used in BigGAN</td>
</tr>
<tr class="odd">
<td><strong>WGAN-GP</strong></td>
<td><span class="math inline">\(\mathbb{E}[D(G(\mathbf{z}))] - \mathbb{E}[D(\mathbf{x})] + \newline \lambda_{GP}\)</span></td>
<td><span class="math inline">\(-\mathbb{E}[D(G(\mathbf{z}))]\)</span></td>
<td><span class="math inline">\(W_1\)</span> distance</td>
<td>Most stable</td>
</tr>
</tbody>
</table>
<p><span class="citation" data-cites="goodfellow2014generative mao2017least miyato2018spectral arjovsky2017wasserstein">(<a href="#/references" role="doc-biblioref" onclick="">Goodfellow et al. 2014</a>; <a href="#/references" role="doc-biblioref" onclick="">Mao et al. 2017</a>; <a href="#/references" role="doc-biblioref" onclick="">Miyato et al. 2018</a>; <a href="#/references" role="doc-biblioref" onclick="">Arjovsky, Chintala, and Bottou 2017</a>)</span></p>
</section>
<section id="geometric-intuition-why-wasserstein-works" class="slide level2">
<h2>Geometric Intuition: Why Wasserstein Works</h2>
<div id="cell-fig-gan-loss-geometry" class="cell" data-execution_count="2">
<div class="cell-output cell-output-display">
<div id="fig-gan-loss-geometry" class="quarto-float quarto-figure quarto-figure-center">
<figure class="quarto-float quarto-float-fig">
<div aria-describedby="fig-gan-loss-geometry-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img data-src="GANs_files/figure-revealjs/fig-gan-loss-geometry-output-1.png" width="949" height="325">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-gan-loss-geometry-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure 2: Gradient behavior: WGAN provides smooth, informative gradients even when distributions are far apart, unlike JS divergence which saturates.
</figcaption>
</figure>
</div>
</div>
</div>
<ul>
<li>Wasserstein distance provides useful gradients even when <span class="math inline">\(p_{data}\)</span> and <span class="math inline">\(p_G\)</span> have disjoint supports.</li>
</ul>
<aside class="notes">
<ul>
<li>JS gradient saturates when supports separate ⇒ generator stalls.</li>
<li>Wasserstein gives distance proportional to mass transport ⇒ informative gradients everywhere.</li>
<li>Smoother landscape reduces chaotic oscillations in updates.</li>
</ul>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
</section>
<section id="d-gan-concrete-example" class="slide level2 smaller">
<h2>1D GAN: Concrete Example</h2>
<h4 id="learn-a-bimodal-gaussian-mixture-in-1d">Learn a bimodal Gaussian mixture in 1D</h4>
<p><span class="math display">\[
p_{data}(x) = \frac{1}{2}\mathcal{N}(-2, 0.4^2) + \frac{1}{2}\mathcal{N}(2, 0.4^2)
\]</span></p>
<div id="cell-fig-gan-1d-toy" class="cell" data-execution_count="3">
<div class="cell-output cell-output-display">
<div id="fig-gan-1d-toy" class="quarto-float quarto-figure quarto-figure-center">
<figure class="quarto-float quarto-float-fig">
<div aria-describedby="fig-gan-1d-toy-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img data-src="GANs_files/figure-revealjs/fig-gan-1d-toy-output-1.png" width="1045" height="325">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-gan-1d-toy-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure 3: 1D GAN training: Generator learns to match a bimodal target distribution. Shows convergence after ~2000 iterations.
</figcaption>
</figure>
</div>
</div>
</div>
<ul>
<li>Generator can capture both modes of the bimodal distribution.</li>
</ul>
</section>
<section id="toy-code-for-1d-gan" class="slide level2">
<h2>Toy code for 1D GAN</h2>
<h3 id="disclamer-i-asked-chatgpt-to-create-it.">Disclamer: I asked ChatGPT to create it.</h3>
<div id="cf09af12" class="cell" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href=""></a><span class="im">import</span> torch</span>
<span id="cb1-2"><a href=""></a><span class="im">import</span> torch.nn <span class="im">as</span> nn</span>
<span id="cb1-3"><a href=""></a><span class="im">import</span> torch.optim <span class="im">as</span> optim</span>
<span id="cb1-4"><a href=""></a><span class="im">import</span> numpy <span class="im">as</span> np</span>
<span id="cb1-5"><a href=""></a><span class="im">import</span> matplotlib.pyplot <span class="im">as</span> plt</span>
<span id="cb1-6"><a href=""></a></span>
<span id="cb1-7"><a href=""></a>torch.manual_seed(<span class="dv">42</span>)</span>
<span id="cb1-8"><a href=""></a>np.random.seed(<span class="dv">42</span>)</span>
<span id="cb1-9"><a href=""></a></span>
<span id="cb1-10"><a href=""></a><span class="kw">def</span> sample_real(n):</span>
<span id="cb1-11"><a href=""></a> <span class="co">"""Sample from bimodal target distribution"""</span></span>
<span id="cb1-12"><a href=""></a> mask <span class="op">=</span> np.random.rand(n) <span class="op">></span> <span class="fl">0.5</span></span>
<span id="cb1-13"><a href=""></a> x <span class="op">=</span> np.zeros(n)</span>
<span id="cb1-14"><a href=""></a> x[mask] <span class="op">=</span> np.random.normal(<span class="op">-</span><span class="dv">2</span>, <span class="fl">0.4</span>, mask.<span class="bu">sum</span>())</span>
<span id="cb1-15"><a href=""></a> x[<span class="op">~</span>mask] <span class="op">=</span> np.random.normal(<span class="dv">2</span>, <span class="fl">0.4</span>, (<span class="op">~</span>mask).<span class="bu">sum</span>())</span>
<span id="cb1-16"><a href=""></a> <span class="cf">return</span> torch.tensor(x, dtype<span class="op">=</span>torch.float32).unsqueeze(<span class="dv">1</span>)</span>
<span id="cb1-17"><a href=""></a></span>
<span id="cb1-18"><a href=""></a><span class="kw">def</span> sample_noise(n): </span>
<span id="cb1-19"><a href=""></a> <span class="cf">return</span> torch.randn(n, <span class="dv">1</span>)</span>
<span id="cb1-20"><a href=""></a></span>
<span id="cb1-21"><a href=""></a><span class="co"># Generator: 1D → 32 → 32 → 1D</span></span>
<span id="cb1-22"><a href=""></a>G <span class="op">=</span> nn.Sequential(</span>
<span id="cb1-23"><a href=""></a> nn.Linear(<span class="dv">1</span>, <span class="dv">32</span>), nn.LeakyReLU(<span class="fl">0.2</span>),</span>
<span id="cb1-24"><a href=""></a> nn.Linear(<span class="dv">32</span>, <span class="dv">32</span>), nn.LeakyReLU(<span class="fl">0.2</span>),</span>
<span id="cb1-25"><a href=""></a> nn.Linear(<span class="dv">32</span>, <span class="dv">1</span>)</span>
<span id="cb1-26"><a href=""></a>)</span>
<span id="cb1-27"><a href=""></a></span>
<span id="cb1-28"><a href=""></a><span class="co"># Discriminator: 1D → 32 → 32 → 1D → sigmoid</span></span>
<span id="cb1-29"><a href=""></a>D <span class="op">=</span> nn.Sequential(</span>
<span id="cb1-30"><a href=""></a> nn.Linear(<span class="dv">1</span>, <span class="dv">32</span>), nn.LeakyReLU(<span class="fl">0.2</span>),</span>
<span id="cb1-31"><a href=""></a> nn.Linear(<span class="dv">32</span>, <span class="dv">32</span>), nn.LeakyReLU(<span class="fl">0.2</span>),</span>
<span id="cb1-32"><a href=""></a> nn.Linear(<span class="dv">32</span>, <span class="dv">1</span>), nn.Sigmoid()</span>
<span id="cb1-33"><a href=""></a>)</span>
<span id="cb1-34"><a href=""></a></span>
<span id="cb1-35"><a href=""></a>opt_G <span class="op">=</span> optim.Adam(G.parameters(), lr<span class="op">=</span><span class="fl">1e-3</span>, betas<span class="op">=</span>(<span class="fl">0.5</span>, <span class="fl">0.999</span>))</span>
<span id="cb1-36"><a href=""></a>opt_D <span class="op">=</span> optim.Adam(D.parameters(), lr<span class="op">=</span><span class="fl">1e-3</span>, betas<span class="op">=</span>(<span class="fl">0.5</span>, <span class="fl">0.999</span>))</span>
<span id="cb1-37"><a href=""></a>bce <span class="op">=</span> nn.BCELoss()</span>
<span id="cb1-38"><a href=""></a></span>
<span id="cb1-39"><a href=""></a><span class="co"># Training loop</span></span>
<span id="cb1-40"><a href=""></a>d_losses, g_losses <span class="op">=</span> [], []</span>
<span id="cb1-41"><a href=""></a><span class="cf">for</span> step <span class="kw">in</span> <span class="bu">range</span>(<span class="dv">2000</span>):</span>
<span id="cb1-42"><a href=""></a> <span class="co"># Train discriminator</span></span>
<span id="cb1-43"><a href=""></a> real <span class="op">=</span> sample_real(<span class="dv">64</span>)</span>
<span id="cb1-44"><a href=""></a> z <span class="op">=</span> sample_noise(<span class="dv">64</span>)</span>
<span id="cb1-45"><a href=""></a> fake <span class="op">=</span> G(z).detach()</span>
<span id="cb1-46"><a href=""></a> </span>
<span id="cb1-47"><a href=""></a> d_real_loss <span class="op">=</span> bce(D(real), torch.ones(<span class="dv">64</span>, <span class="dv">1</span>))</span>
<span id="cb1-48"><a href=""></a> d_fake_loss <span class="op">=</span> bce(D(fake), torch.zeros(<span class="dv">64</span>, <span class="dv">1</span>))</span>
<span id="cb1-49"><a href=""></a> D_loss <span class="op">=</span> d_real_loss <span class="op">+</span> d_fake_loss</span>
<span id="cb1-50"><a href=""></a> </span>
<span id="cb1-51"><a href=""></a> opt_D.zero_grad()</span>
<span id="cb1-52"><a href=""></a> D_loss.backward()</span>
<span id="cb1-53"><a href=""></a> opt_D.step()</span>
<span id="cb1-54"><a href=""></a> </span>
<span id="cb1-55"><a href=""></a> <span class="co"># Train generator</span></span>
<span id="cb1-56"><a href=""></a> z <span class="op">=</span> sample_noise(<span class="dv">64</span>)</span>
<span id="cb1-57"><a href=""></a> fake <span class="op">=</span> G(z)</span>
<span id="cb1-58"><a href=""></a> G_loss <span class="op">=</span> bce(D(fake), torch.ones(<span class="dv">64</span>, <span class="dv">1</span>))</span>
<span id="cb1-59"><a href=""></a> </span>
<span id="cb1-60"><a href=""></a> opt_G.zero_grad()</span>
<span id="cb1-61"><a href=""></a> G_loss.backward()</span>
<span id="cb1-62"><a href=""></a> opt_G.step()</span>
<span id="cb1-63"><a href=""></a> </span>
<span id="cb1-64"><a href=""></a> <span class="cf">if</span> step <span class="op">%</span> <span class="dv">100</span> <span class="op">==</span> <span class="dv">0</span>:</span>
<span id="cb1-65"><a href=""></a> d_losses.append(D_loss.item())</span>
<span id="cb1-66"><a href=""></a> g_losses.append(G_loss.item())</span>
<span id="cb1-67"><a href=""></a></span>
<span id="cb1-68"><a href=""></a><span class="co"># Visualization</span></span>
<span id="cb1-69"><a href=""></a>fig, axes <span class="op">=</span> plt.subplots(<span class="dv">1</span>, <span class="dv">2</span>, figsize<span class="op">=</span>(<span class="dv">11</span>, <span class="fl">3.5</span>))</span>
<span id="cb1-70"><a href=""></a></span>
<span id="cb1-71"><a href=""></a><span class="co"># Left: distributions</span></span>
<span id="cb1-72"><a href=""></a><span class="cf">with</span> torch.no_grad():</span>
<span id="cb1-73"><a href=""></a> real_samples <span class="op">=</span> sample_real(<span class="dv">3000</span>).numpy()</span>
<span id="cb1-74"><a href=""></a> fake_samples <span class="op">=</span> G(sample_noise(<span class="dv">3000</span>)).numpy()</span>
<span id="cb1-75"><a href=""></a></span>
<span id="cb1-76"><a href=""></a>axes[<span class="dv">0</span>].hist(real_samples, bins<span class="op">=</span><span class="dv">50</span>, density<span class="op">=</span><span class="va">True</span>, alpha<span class="op">=</span><span class="fl">0.6</span>, label<span class="op">=</span><span class="st">"Real $p_</span><span class="sc">{data}</span><span class="st">$"</span>, color<span class="op">=</span><span class="st">'#2ecc71'</span>)</span>
<span id="cb1-77"><a href=""></a>axes[<span class="dv">0</span>].hist(fake_samples, bins<span class="op">=</span><span class="dv">50</span>, density<span class="op">=</span><span class="va">True</span>, alpha<span class="op">=</span><span class="fl">0.6</span>, label<span class="op">=</span><span class="st">"Generated $p_G$"</span>, color<span class="op">=</span><span class="st">'#3498db'</span>)</span>
<span id="cb1-78"><a href=""></a>axes[<span class="dv">0</span>].set_xlabel(<span class="st">"x"</span>, fontsize<span class="op">=</span><span class="dv">11</span>)</span>
<span id="cb1-79"><a href=""></a>axes[<span class="dv">0</span>].set_ylabel(<span class="st">"Density"</span>, fontsize<span class="op">=</span><span class="dv">11</span>)</span>
<span id="cb1-80"><a href=""></a>axes[<span class="dv">0</span>].set_title(<span class="st">"Learned Distribution (2000 steps)"</span>, fontsize<span class="op">=</span><span class="dv">12</span>, fontweight<span class="op">=</span><span class="st">'bold'</span>)</span>
<span id="cb1-81"><a href=""></a>axes[<span class="dv">0</span>].legend(frameon<span class="op">=</span><span class="va">False</span>, fontsize<span class="op">=</span><span class="dv">10</span>)</span>
<span id="cb1-82"><a href=""></a>axes[<span class="dv">0</span>].grid(alpha<span class="op">=</span><span class="fl">0.2</span>)</span>
<span id="cb1-83"><a href=""></a></span>
<span id="cb1-84"><a href=""></a><span class="co"># Right: loss curves</span></span>
<span id="cb1-85"><a href=""></a>steps <span class="op">=</span> np.arange(<span class="dv">0</span>, <span class="dv">2000</span>, <span class="dv">100</span>)</span>
<span id="cb1-86"><a href=""></a>axes[<span class="dv">1</span>].plot(steps, d_losses, label<span class="op">=</span><span class="st">"Discriminator loss"</span>, lw<span class="op">=</span><span class="dv">2</span>, color<span class="op">=</span><span class="st">'#e74c3c'</span>)</span>
<span id="cb1-87"><a href=""></a>axes[<span class="dv">1</span>].plot(steps, g_losses, label<span class="op">=</span><span class="st">"Generator loss"</span>, lw<span class="op">=</span><span class="dv">2</span>, color<span class="op">=</span><span class="st">'#3498db'</span>)</span>
<span id="cb1-88"><a href=""></a>axes[<span class="dv">1</span>].set_xlabel(<span class="st">"Training step"</span>, fontsize<span class="op">=</span><span class="dv">11</span>)</span>
<span id="cb1-89"><a href=""></a>axes[<span class="dv">1</span>].set_ylabel(<span class="st">"Loss"</span>, fontsize<span class="op">=</span><span class="dv">11</span>)</span>
<span id="cb1-90"><a href=""></a>axes[<span class="dv">1</span>].set_title(<span class="st">"Training Dynamics"</span>, fontsize<span class="op">=</span><span class="dv">12</span>, fontweight<span class="op">=</span><span class="st">'bold'</span>)</span>
<span id="cb1-91"><a href=""></a>axes[<span class="dv">1</span>].legend(frameon<span class="op">=</span><span class="va">False</span>, fontsize<span class="op">=</span><span class="dv">10</span>)</span>
<span id="cb1-92"><a href=""></a>axes[<span class="dv">1</span>].grid(alpha<span class="op">=</span><span class="fl">0.2</span>)</span>
<span id="cb1-93"><a href=""></a></span>
<span id="cb1-94"><a href=""></a>plt.tight_layout()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
</div>
</section>
<section id="training-pathologies-and-practical-solutions" class="slide level2 smaller">
<h2>Training Pathologies and Practical Solutions</h2>
<h4 id="why-are-gans-notoriously-difficult-to-train">Why are GANs notoriously difficult to train?</h4>
<ol type="1">
<li><strong>Mode collapse:</strong> <span class="math inline">\(G\)</span> maps many <span class="math inline">\(\mathbf{z}\)</span> values to few <span class="math inline">\(\mathbf{x}\)</span> values
<ul>
<li>Cause: <span class="math inline">\(G\)</span> exploits weaknesses in <span class="math inline">\(D\)</span> rather than covering full distribution</li>
<li>Solution: Unrolled optimization, minibatch discrimination</li>
</ul></li>
<li><strong>Oscillation/non-convergence:</strong> Losses oscillate without settling
<ul>
<li>Cause: Simultaneous gradient descent doesn’t guarantee convergence in minimax games</li>
<li>Solution: Two-timescale update rule (TTUR), different learning rates</li>
</ul></li>
<li><strong>Gradient explosion/vanishing</strong>
<ul>
<li>Cause: Deep networks, poor conditioning</li>
<li>Solution: Spectral normalization, gradient penalties, batch normalization</li>
</ul></li>
</ol>
<h4 id="best-practices">Best practices</h4>
<ul>
<li>Use WGAN-GP or SN-GAN for stability</li>
<li>Careful learning rate tuning (<span class="math inline">\(\text{lr}_D > \text{lr}_G\)</span> often helps)</li>
<li>Label smoothing: use <span class="math inline">\(0.9\)</span> instead of <span class="math inline">\(1.0\)</span> for real labels</li>
<li>Architecture matters: use residual connections, avoid pooling</li>
</ul>
<p><span class="citation" data-cites="heusel2017gans">(<a href="#/references" role="doc-biblioref" onclick="">Heusel et al. 2017</a>)</span></p>
</section>
<section id="conditional-gans-cgan" class="slide level2 smaller">
<h2>Conditional GANs (cGAN)</h2>
<ul>
<li>Extend vanilla GANs by conditioning on auxiliary information <span class="math inline">\(\mathbf{y}\)</span> (class labels, text, data, etc.)</li>
</ul>
<p><span class="math display">\[
\min_G \max_D \; \mathbb{E}_{\mathbf{x},\mathbf{y}}[\log D(\mathbf{x}|\mathbf{y})] + \mathbb{E}_{\mathbf{z},\mathbf{y}}[\log(1-D(G(\mathbf{z}|\mathbf{y})|\mathbf{y}))]
\]</span></p>
<h4 id="implementation"><strong>Implementation:</strong></h4>
<ul>
<li>Concatenate condition to both generator input and discriminator input</li>
<li>Or use conditional batch normalization in <span class="math inline">\(G\)</span></li>
</ul>
<h4 id="applications"><strong>Applications:</strong></h4>
<ul>
<li>Class-conditional image/audio generation</li>
<li>Domain translation/adaptation (e.g., piano sound to guitar sound)</li>
<li>Text-to-audio and text-to-image synthesis</li>
<li>Super-resolution, inpainting, etc.</li>
</ul>
</section>
<section id="evaluating-gans-metrics" class="slide level2">
<h2>Evaluating GANs: Metrics</h2>
<ul>
<li>No likelihood to evaluate directly</li>
</ul>
<h3 id="common-metrics">Common metrics</h3>
<ul>
<li><p>Inception Score<sup>1</sup></p></li>
<li><p>Frechet Audio Distance<sup>2</sup></p>
<ul>
<li>These require large pretrained models…</li>
</ul></li>
<li><p>Kernel Embeddings (i.e., Maximum Mean Discrepancy)<sup>3</sup></p></li>
</ul>
<aside><ol class="aside-footnotes"><li id="fn2"><p><span class="citation" data-cites="salimans2016improved">Salimans et al. (<a href="#/references" role="doc-biblioref" onclick="">2016</a>)</span></p></li><li id="fn3"><p><span class="citation" data-cites="kilgour2018fr">Kilgour et al. (<a href="#/references" role="doc-biblioref" onclick="">2018</a>)</span></p></li><li id="fn4"><p><span class="citation" data-cites="gretton2012kernel">Gretton et al. (<a href="#/references" role="doc-biblioref" onclick="">2012</a>)</span></p></li></ol></aside></section>
<section id="takeaways" class="slide level2">
<h2>Takeaways</h2>
<h4 id="gans-are-implicit-generative-models-via-adversarial-training">- <strong>GANs are implicit generative models</strong> via adversarial training</h4>
<ul>
<li>No explicit likelihood, but generate quite realistic samples</li>
</ul>
<h4 id="minimax-game-minimises-js-divergence">- <strong>Minimax game minimises JS divergence</strong></h4>
<ul>
<li>Optimal discriminator provides density ratio</li>
<li>Non-saturating loss provides better gradients</li>
</ul>
<h4 id="wasserstein-gans-are-easier-to-train-properly-because-of-w_1-distance">- Wasserstein GANs are easier to train (properly) because of <span class="math inline">\(W_1\)</span> distance</h4>
<ul>
<li>Gradients have mostly non-zero support</li>
<li>Require enforcing Lipschitz constraint</li>
</ul>
</section>
<section id="takeaways-1" class="slide level2">
<h2>Takeaways</h2>
<h4 id="training-gans-is-not-trivial-but-well-understood">- Training GANs is not trivial but well-understood</h4>
<ul>
<li>Mode collapse, instability, and gradient issues</li>
<li>Many stabilisation techniques available</li>
</ul>
<h4 id="gans-remain-influential-in-scientific-literature-despite-rise-of-diffusion-models">- GANs remain influential in scientific literature despite rise of diffusion models</h4>
<ul>
<li>Foundation for many modern generative approaches</li>
<li>Excellent for conditional generation and domain translation</li>
</ul>
<aside class="notes">
<ul>
<li>GANs remain influential in scientific literature despite rise of diffusion models.</li>
<li>Foundation for many modern generative approaches.</li>
<li>Excellent for conditional generation and domain translation.</li>
</ul>
<style type="text/css">
span.MJX_Assistive_MathML {
position:absolute!important;
clip: rect(1px, 1px, 1px, 1px);
padding: 1px 0 0 0!important;
border: 0!important;
height: 1px!important;
width: 1px!important;
overflow: hidden!important;
display:block!important;
}</style></aside>
</section>
<section id="gans-in-acoustics-applications" class="slide level2 smaller">
<h2>GANs in Acoustics Applications</h2>
<h4 id="music-source-separationspeech-enhancement"><strong>Music Source Separation/Speech Enhancement</strong></h4>
<ul>
<li><p><strong>SEGAN/MetricGAN+</strong>: Initial studies of GANs for speech enhancement <span class="citation" data-cites="pascual2017segan fu2021metricgan">(<a href="#/references" role="doc-biblioref" onclick="">Pascual, Bonafonte, and Serra 2017</a>; <a href="#/references" role="doc-biblioref" onclick="">Fu et al. 2021</a>)</span></p></li>
<li><p><strong>HiFi-GAN</strong>: Speech enhancement GAN which uses several discriminators operating on various time/frequency resolutions <span class="citation" data-cites="kong2020hifi">(<a href="#/references" role="doc-biblioref" onclick="">Kong, Kim, and Bae 2020</a>)</span></p></li>
<li><p>ICASSP/Interspeech URGENT Challenge<sup>1</sup></p></li>
<li><p>Many, many more…</p></li>
</ul>
<h4 id="sound-field-reconstruction-room-acoustics"><strong>Sound Field Reconstruction & Room Acoustics:</strong></h4>
<ul>
<li><p><strong>PWD-GAN</strong>: Plane-wave decomposition using a GAN reconstructs sound fields from sparse measurements, outperforms classical methods <span class="citation" data-cites="karakonstantis2023generative">(<a href="#/references" role="doc-biblioref" onclick="">Karakonstantis and Fernandez-Grande 2023</a>)</span></p></li>
<li><p><strong>IR-GAN</strong>: Room impulse response generation conditioned on acoustic parameters for speech augmentation <span class="citation" data-cites="ratnarajah2020ir">(<a href="#/references" role="doc-biblioref" onclick="">Ratnarajah, Tang, and Manocha 2020</a>)</span></p></li>
<li><p><strong>Bandwidth Extension for RIRs with GANs</strong>: Combine simulated acoustic wave propagation for training GANs for RIR reconstruction <span class="citation" data-cites="fernandez2023generative">(<a href="#/references" role="doc-biblioref" onclick="">Fernandez-Grande et al. 2023</a>)</span></p></li>
</ul>
<aside><ol class="aside-footnotes"><li id="fn5"><p>https://urgent-challenge.com/</p></li></ol></aside></section>
<section id="gans-in-acoustic-applications" class="slide level2 smaller">
<h2>GANs in Acoustic Applications</h2>
<h4 id="ocean-underwater-acoustics"><strong>Ocean & Underwater Acoustics:</strong></h4>
<ul>
<li><strong>GAN data augmentation</strong>: GAN-based model for synthesising realistic underwater acoustic noise patterns to augment training data for marine vehicle detection and classification systems <span class="citation" data-cites="zhou2021generative">(<a href="#/references" role="doc-biblioref" onclick="">Zhou et al. 2021</a>)</span></li>
</ul>
<h4 id="acoustic-metamaterial-absorber-design"><strong>Acoustic Metamaterial & Absorber Design:</strong></h4>
<ul>
<li><strong>CGAN for metamaterials</strong>: Conditional GAN for inverse design of acoustic metamaterial unit cells for sound insulation, eliminating the need for expert-driven trial-and-error design <span class="citation" data-cites="gurbuz2021generative">(<a href="#/references" role="doc-biblioref" onclick="">Gurbuz et al. 2021</a>)</span></li>
</ul>
</section>
<section id="references" class="slide level2 smaller scrollable">
<h2>References</h2>
<div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0" role="list">
<div id="ref-arjovsky2017wasserstein" class="csl-entry" role="listitem">
Arjovsky, Martin, Soumith Chintala, and Léon Bottou. 2017. <span>“Wasserstein GAN.”</span> In <em>ICML</em>.
</div>
<div id="ref-fernandez2023generative" class="csl-entry" role="listitem">
Fernandez-Grande, Efren, Xenofon Karakonstantis, Diego Caviedes-Nozal, and Peter Gerstoft. 2023. <span>“Generative Models for Sound Field Reconstruction.”</span> <em>The Journal of the Acoustical Society of America</em> 153 (2): 1179–90.
</div>
<div id="ref-fu2021metricgan" class="csl-entry" role="listitem">
Fu, Szu-Wei, Cheng Yu, Tsun-An Hsieh, Peter Plantinga, Mirco Ravanelli, Xugang Lu, and Yu Tsao. 2021. <span>“Metricgan+: An Improved Version of Metricgan for Speech Enhancement.”</span> <em>arXiv Preprint arXiv:2104.03538</em>.
</div>
<div id="ref-goodfellow2014generative" class="csl-entry" role="listitem">
Goodfellow, Ian et al. 2014. <span>“Generative Adversarial Nets.”</span> In <em>NeurIPS</em>.
</div>
<div id="ref-gretton2012kernel" class="csl-entry" role="listitem">
Gretton, Arthur, Karsten M Borgwardt, Malte J Rasch, Bernhard Scholkopf, and Alexander Smola. 2012. <span>“A Kernel Two-Sample Test.”</span> <em>The Journal of Machine Learning Research</em> 13 (1): 723–73.
</div>
<div id="ref-gulrajani2017improved" class="csl-entry" role="listitem">
Gulrajani, Ishaan, Faruk Ahmed, Martin Arjovsky, Vincent Dumoulin, and Aaron Courville. 2017. <span>“Improved Training of Wasserstein GANs.”</span> <em>NeurIPS</em>.
</div>
<div id="ref-gurbuz2021generative" class="csl-entry" role="listitem">
Gurbuz, Caglar, Felix Kronowetter, Christoph Dietz, Martin Eser, Jonas Schmid, and Steffen Marburg. 2021. <span>“Generative Adversarial Networks for the Design of Acoustic Metamaterials.”</span> <em>The Journal of the Acoustical Society of America</em> 149 (2): 1162–74.