-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs231n_lec12.html
executable file
·1081 lines (736 loc) · 46.9 KB
/
cs231n_lec12.html
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>
<head>
<!-- Document Settings -->
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<!-- On Post front-matter YAML, set "use_math: true" to use LaTex -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: {
autoNumber: "AMS"
}
},
tex2jax: {
inlineMath: [ ['$', '$'], ["\\(","\\)"] ],
displayMath: [ ['$$', '$$'], ["\\[","\\]"] ],
processEscapes: true,
}
});
MathJax.Hub.Register.MessageHook("Math Processing Error",function (message) {
alert("Math Processing Error: "+message[1]);
});
MathJax.Hub.Register.MessageHook("TeX Jax - parse error",function (message) {
alert("Math Processing Error: "+message[1]);
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<!-- Base Meta -->
<!-- dynamically fixing the title for tag/author pages -->
<title>cs231n - Lecture 12. Generative Models</title>
<meta name="HandheldFriendly" content="True" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Styles'n'Scripts -->
<link rel="stylesheet" type="text/css" href="/assets/built/screen.css" />
<link rel="stylesheet" type="text/css" href="/assets/built/screen.edited.css" />
<link rel="stylesheet" type="text/css" href="/assets/built/syntax.css" />
<!-- syntax.css -->
<link rel="stylesheet" type="text/css" href="/assets/built/syntax.css" />
<!-- highlight.js -->
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css">
<style>.hljs { background: none; }</style>
<!--[if IE]>
<style>
p, ol, ul{
width: 100%;
}
blockquote{
width: 100%;
}
</style>
<![endif]-->
<!-- This tag outputs SEO meta+structured data and other important settings -->
<meta name="description" content="" />
<link rel="shortcut icon" href="http://0.0.0.0:4000/assets/built/images/favicon.jpg" type="image/png" />
<link rel="canonical" href="http://0.0.0.0:4000/cs231n_lec12" />
<meta name="referrer" content="no-referrer-when-downgrade" />
<!--title below is coming from _includes/dynamic_title-->
<meta property="og:site_name" content="Darron's Devlog" />
<meta property="og:type" content="website" />
<meta property="og:title" content="cs231n - Lecture 12. Generative Models" />
<meta property="og:description" content="Supervised vs. Unsupervised Supervised Learning: Data: $(x,y)$; y is label Goal: Learn a function to map $x\rightarrow y$ Unsupervised Learning: Data: x; no labels Goal: Learn some underlying hidden structure of the data Generative Modeling Given training data, generate new samples from same distribution Objectives: Learn $p_{\scriptstyle\text{model}}(x)$ that approximates $p_{\scriptstyle\text{data}}(x)$" />
<meta property="og:url" content="http://0.0.0.0:4000/cs231n_lec12" />
<meta property="og:image" content="http://0.0.0.0:4000/assets/built/images/blog-cover1.png" />
<meta property="article:publisher" content="https://www.facebook.com/" />
<meta property="article:author" content="https://www.facebook.com/" />
<meta property="article:published_time" content="2022-01-09T15:00:00+00:00" />
<meta property="article:modified_time" content="2022-01-09T15:00:00+00:00" />
<meta property="article:tag" content="cs231n" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="cs231n - Lecture 12. Generative Models" />
<meta name="twitter:description" content="Supervised vs. Unsupervised Supervised Learning: Data: $(x,y)$; y is label Goal: Learn a function to map $x\rightarrow y$ Unsupervised Learning: Data: x; no labels Goal: Learn some underlying hidden structure of the data Generative Modeling Given training data, generate new samples from same distribution Objectives: Learn $p_{\scriptstyle\text{model}}(x)$ that approximates $p_{\scriptstyle\text{data}}(x)$" />
<meta name="twitter:url" content="http://0.0.0.0:4000/" />
<meta name="twitter:image" content="http://0.0.0.0:4000/assets/built/images/blog-cover1.png" />
<meta name="twitter:label1" content="Written by" />
<meta name="twitter:data1" content="Darron's Devlog" />
<meta name="twitter:label2" content="Filed under" />
<meta name="twitter:data2" content="cs231n" />
<meta name="twitter:site" content="@" />
<meta name="twitter:creator" content="@" />
<meta property="og:image:width" content="1400" />
<meta property="og:image:height" content="933" />
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Website",
"publisher": {
"@type": "Organization",
"name": "Darron's Devlog",
"logo": "http://0.0.0.0:4000/"
},
"url": "http://0.0.0.0:4000/cs231n_lec12",
"image": {
"@type": "ImageObject",
"url": "http://0.0.0.0:4000/assets/built/images/blog-cover1.png",
"width": 2000,
"height": 666
},
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "http://0.0.0.0:4000/cs231n_lec12"
},
"description": "Supervised vs. Unsupervised Supervised Learning: Data: $(x,y)$; y is label Goal: Learn a function to map $x\rightarrow y$ Unsupervised Learning: Data: x; no labels Goal: Learn some underlying hidden structure of the data Generative Modeling Given training data, generate new samples from same distribution Objectives: Learn $p_{\scriptstyle\text{model}}(x)$ that approximates $p_{\scriptstyle\text{data}}(x)$"
}
</script>
<!-- <script type="text/javascript" src="https://demo.ghost.io/public/ghost-sdk.min.js?v=724281a32e"></script>
<script type="text/javascript">
ghost.init({
clientId: "ghost-frontend",
clientSecret: "f84a07a72b17"
});
</script> -->
<meta name="generator" content="Jekyll 3.6.2" />
<link rel="alternate" type="application/rss+xml" title="cs231n - Lecture 12. Generative Models" href="/feed.xml" />
</head>
<body class="post-template">
<div class="site-wrapper">
<!-- All the main content gets inserted here, index.hbs, post.hbs, etc -->
<!-- default -->
<!-- The tag above means: insert everything in this file
into the {body} of the default.hbs template -->
<header class="site-header outer">
<div class="inner">
<nav class="site-nav">
<div class="site-nav-left">
<a class="site-nav-logo" href="/">Darron's Devlog</a>
<ul class="nav" role="menu">
<li class="nav-home" role="menuitem"><a href="/">Home</a></li>
<li class="nav-about" role="menuitem"><a href="/about/">About</a></li>
<li class="nav-projects" role="menuitem"><a href="/tag/projects/">Projects</a></li>
<li class="nav-studies" role="menuitem"><a href="/tag/studies/">Studies</a></li>
<li class="nav-blog" role="menuitem"><a href="/tag/blog/">Blog</a></li>
<li class="nav-archive" role="menuitem">
<a href="/archive.html">All Posts</a>
</li>
</ul>
</div>
<div class="site-nav-right">
<div class="social-links">
</div>
<a class="subscribe-button" href="#subscribe">Search</a>
</div>
</nav>
</div>
</header>
<!-- Everything inside the #post tags pulls data from the post -->
<!-- #post -->
<main id="site-main" class="site-main outer" role="main">
<div class="inner">
<article class="post-full tag-cs231n no-image">
<header class="post-full-header">
<section class="post-full-meta">
<time class="post-full-meta-date" datetime=" 9 January 2022"> 9 January 2022</time>
<span class="date-divider">/</span>
<a href='/tag/cs231n/'>CS231N</a>
</section>
<h1 class="post-full-title">cs231n - Lecture 12. Generative Models</h1>
</header>
<!--
-->
<section class="post-full-content">
<div class="kg-card-markdown">
<h3 id="supervised-vs-unsupervised">Supervised vs. Unsupervised</h3>
<ul>
<li>
<p>Supervised Learning:<br />
Data: $(x,y)$; <em>y</em> is label<br />
Goal: Learn a function to map $x\rightarrow y$</p>
</li>
<li>
<p>Unsupervised Learning:<br />
Data: <em>x</em>; no labels<br />
Goal: Learn some underlying hidden structure of the data</p>
</li>
</ul>
<h3 id="generative-modeling">Generative Modeling</h3>
<p>Given training data, generate new samples from same distribution</p>
<ul>
<li>Objectives:
<ol>
<li>Learn $p_{\scriptstyle\text{model}}(x)$ that approximates $p_{\scriptstyle\text{data}}(x)$</li>
<li>Sampling new <em>x</em> from $p_{\scriptstyle\text{model}}(x)$</li>
</ol>
</li>
<li>Formulate as density estimation problems:
<ul>
<li>Explicit density estimation: explicitly define and solve for $p_{\scriptstyle\text{model}}(x)$.</li>
<li>Implicit density estimation: learn model that can sample from $p_{\scriptstyle\text{model}}(x)$ without explicitly defining it.</li>
</ul>
</li>
<li>Why Generative Models?<br />
Realistic samples for artwork, super-resolution, colorization, etc.<br />
Learn useful features for downstream tasks such as classification.<br />
Getting insights from high-dimensional data (physics, medical imaging, etc.)<br />
Modeling physical world for simulation and planning (robotics and reinforcement learning applications)<br />
…</li>
</ul>
<h2 id="pixelrnn-and-pixelcnn-a-brief-overview">PixelRNN and PixelCNN; a brief overview</h2>
<ul>
<li>Fully visible belief network (FVBN)<br />
is an explicit density model, defines tractable density function using chain rule to decompose the likelihood of an image <em>x</em> into product of <em>1</em>-d distributions:<br />
<img src="/assets/images/cs231n_lec12_0.png" alt="png" width="40%", height="40%" /><br />
Then maximize likelihood of training data. It is a complex distribution over pixel values, express using a neural network.</li>
</ul>
<h3 id="pixelrnn-van-der-oord-et-al-2016">PixelRNN, <em>van der Oord et al., 2016</em></h3>
<ul>
<li>Generate image pixels starting from corner, dependency on previous pixels modeled using an RNN(LSTM).
<img src="/assets/images/cs231n_lec12_1.png" alt="png" width="35%", height="35%" /><br />
Drawback: sequential generation is slow in both training and inference</li>
</ul>
<h3 id="pixelcnn-van-der-oord-et-al-2016">PixelCNN, <em>van der Oord et al., 2016</em></h3>
<ul>
<li>
<p>Generate image pixels starting from corner, ependency on previous pixels modeled using a CNN over context region(<strong>masked convolution</strong>)<br />
<img src="/assets/images/cs231n_lec12_2.png" alt="png" width="35%", height="35%" /><br />
Training is faster than PixelRNN: it can parallelize convolutions since context region values known from training images.<br />
Generation is still slow: for a 32x32 image, we need to do forward passes of the network <em>1024</em> times for a single image.</p>
</li>
<li>
<p>Improving PixelCNN performance<br />
Gated convolutional layers, Short-cut connections, Discretized logistic loss, Multi-scale, Training tricks, etc.<br />
See also PixelCNN++, <em>Salimans et al., 2017</em></p>
</li>
</ul>
<h3 id="summary">Summary</h3>
<ul>
<li>Pros:<br />
Can explicitly compute likelihood <em>p(x)</em><br />
Easy to optimize<br />
Good samples</li>
<li>Cons:<br />
Sequential generation is slow</li>
</ul>
<h2 id="variational-autoencodervae">Variational Autoencoder(VAE)</h2>
<ul>
<li>VAE is an explicit density model, defines intractable(approximate) density function with latent <strong>z</strong>:<br />
$p_\theta(x) = \int p_\theta(z)p_\theta(x|z)\, dz$<br />
No dependencies among pixels, can generate all pixels at the same time. But cannot optimize directly, derive and optimize lower bound on likelihood instead</li>
</ul>
<h3 id="background-autoencoders">Background: Autoencoders</h3>
<ul>
<li>Unsupervised approach for learning a lower-dimensional feature representation from unlabeled training data<br />
<img src="/assets/images/cs231n_lec12_3.png" alt="png" width="35%", height="35%" />
<ul>
<li><strong>z</strong> usually smaller than <strong>x</strong>: with dimensionality reduction to capture meaningful factors of variation in data. Train such that features can be used to reconstruct original data($\hat{x}$)</li>
<li>“Autoencoding”; encoding input itself(<em>L2</em> loss)</li>
<li>After training, throw away decoder and adjust to the final task</li>
</ul>
</li>
<li>
<p>Encoder can be used to initialize a supervised model;
Transfer from large, unlabeled dataset(Autoencoder) to small, labeled dataset and fine-tune; train for final task.<br />
<img src="/assets/images/cs231n_lec12_4.png" alt="png" width="40%", height="40%" /></p>
</li>
<li>But we can’t generate new images from an autoencoder because we don’t know the space of <strong>z</strong>. $\rightarrow$ Variational Autoencoders for a generative model.</li>
</ul>
<h3 id="variational-autoencoders-probabilistic-spin-on-autoencoders">Variational Autoencoders: Probabilistic spin on autoencoders</h3>
<ul>
<li>Assume training data \(\left\{ x^{(i)}\right\} _{i=1}^N\) is generated from the distribution of unobserved (latent) representation <strong>z</strong></li>
<li>Intuition from autoencoders: <strong>x</strong> is an image, <strong>z</strong> is latent factors used to generate <strong>x</strong>: attributes, orientation, etc.</li>
</ul>
<p><img src="/assets/images/cs231n_lec12_5.png" alt="png" width="40%", height="40%" /></p>
<ul>
<li>We want to estimate the true parameters $\theta^*$ of this generative model given training data <em>x</em>.</li>
<li>Model representation:
<ul>
<li><em>p(z)</em>: Choose prior to be simple, e.g. Gaussian.</li>
<li><strong>z</strong>: Reasonable for latent attributes, e.g. pose, how much smile.</li>
<li><em>p(x|z)</em>: Generating images, conditional probability is complex<br />
$\rightarrow$ represent with neural network</li>
<li>$p_\theta(x)$: Learn model parameters to maximize likelihood of training data</li>
</ul>
</li>
</ul>
<h3 id="variational-autoencoders-intractability">Variational Autoencoders: Intractability</h3>
<ul>
<li>Data likelihood:<br />
$p_\theta(x) = \int p_\theta(z)p_\theta(x|z)\, dz$<br />
where $p_\theta(z)$ is a Simple Gaussian prior and $p_\theta(x|z)$ is a decoder neural network, it is intractable to compute <em>p(x|z)</em> for every <em>z</em>.<br />
while <em>Monte Carlo estimation</em>-$\log p(x) \approx \log\frac{1}{k}\sum_{i=1}^k p(x|z^{(i)})$, where $z^{(i)}\sim p(z)$- is too high variance.</li>
<li>divided by intractable $p_\theta(x)$, Posterior density also intractable:<br />
$p_\theta(z|x) = p_\theta(x|z)p_\theta(z)/p_\theta(x)$</li>
<li>Solution:<br />
In addition to modeling $p_\theta(x|z)$, learn $q_\phi(z|x)$ that approximates the true posterior $p_\theta(z|x)$. $q_\phi$, approximate posterior allows us to derive a lower bound on the data likelihood that is tractable, which can be optimized.<br />
<strong>Variational inference</strong> is to approximate the unknown posterior distribution from only the observed data <em>x</em></li>
</ul>
\[\begin{align*}
\log p_\theta(x^{(i)})
&= \mathbf{E}_{z~q_\phi(z|x^{(i)})}\left[ \log p_\theta(x^{(i)}) \right] \quad \textit(p_\theta(x^{(i)})\ does\ not\ depend\ on\ z) \\
&= \mathbf{E}_z \left[
\log\frac{p_\theta(x^{(i)}|z)p_\theta(z)}{p_\theta(z|x^{(i)})} \right] \quad \textit(Bayes'\ Rule) \\
&= \mathbf{E}_z \left[
\log\frac{p_\theta(x^{(i)}|z)p_\theta(z)}{p_\theta(z|x^{(i)})}
\frac{q_\phi(z|x^{(i)})}{q_\phi(z|x^{(i)})} \right] \quad \textit(Multiply\ by\ constant)\\
&= \mathbf{E}_z \left[\log p_\theta(x^{(i)}|z) \right]
- \mathbf{E}_z \left[ \log\frac{q_\phi(z|x^{(i)})}{p_\theta(z)}\right]
+ \mathbf{E}_z \left[ \log\frac{q_\phi(z|x^{(i)})}{p_\theta(z|x^{(i)})}\right] \quad \textit(Logarithms) \\
&= \mathbf{E}_z \left[\log p_\theta(x^{(i)}|z) \right]
- D_{KL}(q_\phi(z|x^{(i)})|p_\theta(z)) + D_{KL}(q_\phi(z|x^{(i)})|p_\theta(z|x^{(i)}))
\end{align*}\]
<ul>
<li>With taking expectation with respect to <em>z</em>(using encoder network) let us write nice <em>KL</em> terms;
<ul>
<li>\(\mathbf{E}_z \left[\log p_\theta(x^{(i)}\vert z) \right]\): <strong>Decoder</strong> network gives $p_\theta(x\vert z)$, can compute estimate of this term through sampling(need some trick to differentiate through sampling). It reconstruct the input data.</li>
<li>\(D_{KL}(q_\phi(z\vert x^{(i)})\vert p_\theta(z))\): KL term between Gaussian for encoder and <em>z</em> prior has nice closed-form solution. <strong>Encoder</strong> makes approximate posterior distribution close to prior.</li>
<li>\(D_{KL}(q_\phi(z\vert x^{(i)})\vert p_\theta(z\vert x^{(i)}))\): is intractable, we can’t compute this term; but we know KL divergence always greater than <em>0</em>.</li>
</ul>
</li>
<li>
<p>To maximize the data likelihood, we can rewrite<br />
\(\begin{align*}
\log p_\theta (x^{(i)}) &= \mathbf{E}_z \left[ \log p _\theta (x^{(i)}\vert z) \right]
- D_{KL}(q_\phi (z\vert x^{(i)})\vert p_\theta (z)) + D_{KL}(q_\phi (z\vert x^{(i)})\vert p_\theta (z\vert x^{(i)})) \\
&= \mathcal{L}(x^{(i)},\theta ,\phi ) + (C\ge 0)
\end{align*}\)</p>
</li>
<li>\(\mathcal{L}(x^{(i)},\theta,\phi)\): <em>Decoder - Encoder</em><br />
<strong>Tractable lower bound</strong> which we can take gradient of and optimize. Maximizing this <em>evidence lower bound(ELBO)</em>, we can maximize $\log p_\theta(x)$. Later, we take minus on this term for the loss function of a neural network.</li>
</ul>
<p><img src="/assets/images/cs231n_lec12_6.png" alt="png" width="45%", height="45%" /></p>
<ul>
<li>Encoder part; \(D_{KL}(q_\phi(z\vert x^{(i)})\vert p_\theta(z))\)<br />
We choose <em>q(z)</em> as a Gaussian distribution, $q(z\vert x) = N(\mu_{z\vert x}, \Sigma_{z\vert x})$. Computing the KL divergence, \(D_{KL}(N(\mu_{z\vert x}, \Sigma_{z\vert x}))\vert N(0,I))\), having analytical solution.</li>
<li>Reparameterization trick <em>z</em>:<br />
to make sampling differentiable, input sample $\epsilon\sim N(0,I)$ to the graph $z = \mu_{z\vert x} + \epsilon\sigma_{z\vert x}$; where $\mu, \sigma$ are the part of computation graph.</li>
<li>Decoder part;<br />
Maximize likelihood of original input being reconstructed, $\hat{x}-x$.</li>
<li>For every minibatch of input data, compute $\mathcal{L}$ graph forward pass and backprop.</li>
</ul>
<p><img src="/assets/images/cs231n_lec12_7.png" alt="png" width="75%", height="75%" /></p>
<h3 id="variational-autoencoders-generating-data">Variational Autoencoders: Generating Data</h3>
<p><img src="/assets/images/cs231n_lec12_8.png" alt="png" width="55%", height="55%" /></p>
<ul>
<li>Diagonal prior on <strong>z</strong> for independent latent variables</li>
<li>Different dimensions of <strong>z</strong> encode interpretable factors of variation;<br />
Also good feature representation taht can be computed using $q_\phi(z\vert x)$.</li>
</ul>
<h3 id="summary-1">Summary</h3>
<ul>
<li>Probabilistic spin to traditional autoencoders, allows generating data</li>
<li>
<p>Defines an intractable density; derive and optimize a (variational) lower bound</p>
</li>
<li>Pros:<br />
Principled approach to generative models<br />
Interpretable latent space<br />
Allows inference of $q(z\vert x)$, can be useful feature representation for other tasks - Cons:<br />
Maximizes lower bound of likelihood: not as good evaluation as tractable model<br />
Samples <em>mean</em>; blurrier and lower quality compared to state-of-the-art (GANs)</li>
</ul>
<h2 id="generative-adversarial-networksgans">Generative Adversarial Networks(GANs)</h2>
<p><img src="/assets/images/cs231n_lec12_9.png" alt="png" width="40%", height="40%" /><br />
idea: Use a discriminator network to tell whether the generate image is within data distribution (“real”) or not</p>
<h3 id="training-gans-two-player-game">Training GANs: Two-player game</h3>
<p><img src="/assets/images/cs231n_lec12_10.png" alt="png" width="60%", height="60%" /><br />
Discriminator network: try to distinguish between real and fake images<br />
Generator network: try to fool discriminator by generating real-looking images</p>
<ul>
<li>Train jointly in <strong>minimax game</strong>;<br />
Minimax objective function:<br />
\(\mbox{min}_{\theta_g} \mbox{max}_{\theta_d}\left[\mathbb{E}_{x\sim {p_{data}}}\log D_{\theta_d}(x) + \mathbb{E}_{z\sim p(z)}\log(1-D_{\theta_d}(G_{\theta_g}(z))) \right]\)<br />
where $\theta_g$ is an objective for the generator objective and $\theta_d$ for the discriminator
<ul>
<li>$D_{\theta_d}(x)$: Discriminator outputs likelihood in <em>(0,1)</em> of real image</li>
<li>$D_{\theta_d}(G_{\theta_g}(z))$: Discriminator output for generated fake data <em>G(z)</em></li>
</ul>
</li>
<li>Discriminator($\theta_d$) wants to <strong>maximize objective</strong> such that <em>D(x)</em> is close to <em>1</em>(real) and <em>D(G(z))</em> is close to <em>0</em>(fake)</li>
<li>Generator($\theta_g$) wants to <em>minimize objective</em> such that <em>D(G(z))</em> is close to <em>1</em>(to fool discriminator)</li>
</ul>
<p>We alternate the minimax objection function with:</p>
<ol>
<li><strong>Gradient ascent</strong> on discriminator<br />
\(\mbox{max}_{\theta_d}\left[\mathbb{E}_{x\sim p_{data}}\log D_{\theta_d}(x) + \mathbb{E}_{z\sim p(z)}\log(1-D_{\theta_d}(G_{\theta_g}(z))) \right]\)</li>
<li>1) <strong>Gradient descent</strong> on generator<br />
\(\mbox{min}_{\theta_g}\mathbb{E}_{z\sim p(z)}\log(1-D_{\theta_d}(G_{\theta_g}(z)))\)
<ul>
<li>In practice, optimizing this generator objective does not work well;<br />
When sample is likely fake, want to learn from it to improve generator (move to the right on <em>X</em> axis), but gradient near <em>0</em> in <em>X</em> axis is relatively flat; Gradient signal is dominated by region where sample is already good(near <em>1</em>).<br />
<img src="/assets/images/cs231n_lec12_11.png" alt="png" width="30%", height="30%" /></li>
</ul>
<p>2) <strong>Instead: Gradient ascent</strong> on generator, different objective<br />
\(\mbox{max}_{\theta_d}\mathbb{E}_{z\sim p(z)}\log(D_{\theta_d}(G_{\theta_g}(z)))\)</p>
<ul>
<li>Rather than minimizing likelihood of discriminator being correct, maximize likelihood of discriminator being wrong. Same objective of fooling discriminator, but now higher gradient signal for bad samples.<br />
<img src="/assets/images/cs231n_lec12_12.png" alt="png" width="35%", height="35%" /></li>
</ul>
</li>
</ol>
<ul>
<li>
<p>GAN training Algorithm<br />
<img src="/assets/images/cs231n_lec12_13.png" alt="png" width="70%", height="70%" /></p>
</li>
<li>
<p>After training, use generator network to generate new images</p>
</li>
</ul>
<h3 id="gan-convolutional-architectures">GAN: Convolutional Architectures</h3>
<ul>
<li>
<p>Generator is an upsampling network with fractionally-strided convolutions<br />
Discriminator is a convolutional network</p>
</li>
<li>
<p>Architecture guidelines for stable Deep Conv GANs</p>
<ul>
<li>Replace any pooling layers with strided convolutions(discriminator) and fractional-strided convolutions(generator).</li>
<li>Use batchnorm in both network.</li>
<li>Remove fully connected hidden layers for deeper architecture.</li>
<li>Use ReLU activation in generator for all layers except for the output, which uses Tanh.</li>
<li>Use LeakyReLU activation in the discriminator for all layers.</li>
</ul>
</li>
</ul>
<h3 id="gan-interpretable-vector-math">GAN: Interpretable Vector Math</h3>
<p><img src="/assets/images/cs231n_lec12_14.png" alt="png" width="80%", height="80%" /></p>
<ul>
<li>works similar to a language model</li>
</ul>
<h3 id="2017-explosion-of-gans">2017: Explosion of GANs</h3>
<ul>
<li>“The GAN Zoo”, <code class="language-plaintext highlighter-rouge">https://github.com/hindupuravinash/the-gan-zoo</code></li>
<li>check <code class="language-plaintext highlighter-rouge">https://github.com/soumith/ganhacks</code> for tips and tricks for training GANs</li>
</ul>
<h3 id="scene-graphs-to-gans">Scene graphs to GANs</h3>
<p><img src="/assets/images/cs231n_lec12_15.png" alt="png" width="30%", height="30%" /></p>
<ul>
<li>Specifying exactly what kind of image you want to generate. The explicit structure in scene graphs provides better image generation for complex scenes.</li>
</ul>
<h3 id="summary-gans">Summary: GANs</h3>
<ul>
<li>Don’t work with an explicit density function</li>
<li>
<p>Take game-theoretic approach: learn to generate from training distribution through 2-player game</p>
</li>
<li>Pros:
<ul>
<li>Beautiful, state-of-the-art samples</li>
</ul>
</li>
<li>Cons:
<ul>
<li>Trickier / more unstable to train</li>
<li>Can’t solve inference queries such as $p(x)$, $p(z\vert x)$</li>
</ul>
</li>
<li>Active areas of research:
<ul>
<li>Better loss functions, more stable training (Wasserstein GAN, LSGAN, many others)</li>
<li>Conditional GANs, GANs for all kinds of applications</li>
</ul>
</li>
<li>Useful Resources on Generative Models<br />
CS236: Deep Generative Models (Stanford)<br />
CS 294-158 Deep Unsupervised Learning (Berkeley)</li>
</ul>
</div>
</section>
<!-- Email subscribe form at the bottom of the page -->
<!--
<section class="subscribe-form">
<h3 class="subscribe-form-title">Subscribe to Darron's Devlog</h3>
<p>Get the latest posts delivered right to your inbox</p>
<span id="searchform" method="post" action="/subscribe/" class="">
<input class="confirm" type="hidden" name="confirm" />
<input class="location" type="hidden" name="location" />
<input class="referrer" type="hidden" name="referrer" />
<div class="form-group">
<input class="subscribe-email" onkeyup="myFunc()"
id="searchtext" type="text" name="searchtext"
placeholder="Search..." />
</div>
<script type="text/javascript">
function myFunc() {
if(event.keyCode == 13) {
var url = encodeURIComponent($("#searchtext").val());
location.href = "/search.html?query=" + url;
}
}
</script>
</span>
</section>
-->
<footer class="post-full-footer">
<!-- Everything inside the #author tags pulls data from the author -->
<!-- #author-->
<!-- /author -->
</footer>
<!-- If you use Disqus comments, just uncomment this block.
The only thing you need to change is "test-apkdzgmqhj" - which
should be replaced with your own Disqus site-id. -->
<section class="post-full-comments">
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
var this_page_url = 'http://0.0.0.0:4000/cs231n_lec12';
var this_page_identifier = '/cs231n_lec12';
var this_page_title = 'cs231n - Lecture 12. Generative Models';
};
(function() {
var d = document, s = d.createElement('script');
s.src = 'https://.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
</section>
</article>
</div>
</main>
<!-- Links to Previous/Next posts -->
<aside class="read-next outer">
<div class="inner">
<div class="read-next-feed">
<article class="read-next-card"
style="background-image: url(/assets/built/images/blog-cover1.png)"
>
<header class="read-next-card-header">
<small class="read-next-card-header-sitetitle">— Darron's Devlog —</small>
<h3 class="read-next-card-header-title"><a href="/tag/cs231n/">Cs231n</a></h3>
</header>
<div class="read-next-divider"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M13 14.5s2 3 5 3 5.5-2.463 5.5-5.5S21 6.5 18 6.5c-5 0-7 11-12 11C2.962 17.5.5 15.037.5 12S3 6.5 6 6.5s4.5 3.5 4.5 3.5"/></svg>
</div>
<div class="read-next-card-content">
<ul>
<li><a href="/cs231n_lec15">cs231n - Lecture 15. Detection and Segmentation</a></li>
<li><a href="/cs231n_lec14">cs231n - Lecture 14. Visualizing and Understanding</a></li>
<li><a href="/cs231n_lec13">cs231n - Lecture 13. Self-Supervised Learning</a></li>
</ul>
</div>
<footer class="read-next-card-footer">
<a href="/tag/cs231n/">
See all 13 posts →
</a>
</footer>
</article>
<!-- If there's a next post, display it using the same markup included from - partials/post-card.hbs -->
<article class="post-card post-template no-image">
<div class="post-card-content">
<a class="post-card-content-link" href="/cs231n_lec13">
<header class="post-card-header">
<span class="post-card-tags">Cs231n</span>
<h2 class="post-card-title">cs231n - Lecture 13. Self-Supervised Learning</h2>
</header>
<section class="post-card-excerpt">
<p>Self-Supervised Learning Generative vs. Self-supervised Learning Both aim to learn from data without manual label annotation Generative learning aims to model data distribution $p_{data}(x)$, e.g., generating realistic images. Self-supervised learning methods solve “pretext”</p>
</section>
</a>
<footer class="post-card-meta">
</footer>
</div>
</article>
<!-- If there's a previous post, display it using the same markup included from - partials/post-card.hbs -->
<article class="post-card post-template no-image">
<div class="post-card-content">
<a class="post-card-content-link" href="/cs231n_lec11">
<header class="post-card-header">
<span class="post-card-tags">Cs231n</span>
<h2 class="post-card-title">cs231n - Lecture 11. Attention and Transformers</h2>
</header>
<section class="post-card-excerpt">
<p>Attention with RNNs Image Captioning using spatial features Input: Image I Output: Sequence y $= y_1, y_2, \ldots, y_T$ Encoder: $h_0 = f_W(z)$, where z is spatial CNN features, $f_W(\cdot)$ is an MLP</p>
</section>
</a>
<footer class="post-card-meta">
</footer>
</div>
</article>
</div>
</div>
</aside>
<!-- Floating header which appears on-scroll, included from includes/floating-header.hbs -->
<div class="floating-header">
<div class="floating-header-logo">
<a href="/">
<span>Darron's Devlog</span>
</a>
</div>
<span class="floating-header-divider">—</span>
<div class="floating-header-title">cs231n - Lecture 12. Generative Models</div>
<div class="floating-header-share">
<div class="floating-header-share-label">Share this <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M7.5 15.5V4a1.5 1.5 0 1 1 3 0v4.5h2a1 1 0 0 1 1 1h2a1 1 0 0 1 1 1H18a1.5 1.5 0 0 1 1.5 1.5v3.099c0 .929-.13 1.854-.385 2.748L17.5 23.5h-9c-1.5-2-5.417-8.673-5.417-8.673a1.2 1.2 0 0 1 1.76-1.605L7.5 15.5zm6-6v2m-3-3.5v3.5m6-1v2"/>
</svg>
</div>
<a class="floating-header-share-tw" href="https://twitter.com/share?text=cs231n+-+Lecture+12.+Generative+Models&url=https://12kdh43.github.io/cs231n_lec12"
onclick="window.open(this.href, 'share-twitter', 'width=550,height=235');return false;">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path d="M30.063 7.313c-.813 1.125-1.75 2.125-2.875 2.938v.75c0 1.563-.188 3.125-.688 4.625a15.088 15.088 0 0 1-2.063 4.438c-.875 1.438-2 2.688-3.25 3.813a15.015 15.015 0 0 1-4.625 2.563c-1.813.688-3.75 1-5.75 1-3.25 0-6.188-.875-8.875-2.625.438.063.875.125 1.375.125 2.688 0 5.063-.875 7.188-2.5-1.25 0-2.375-.375-3.375-1.125s-1.688-1.688-2.063-2.875c.438.063.813.125 1.125.125.5 0 1-.063 1.5-.25-1.313-.25-2.438-.938-3.313-1.938a5.673 5.673 0 0 1-1.313-3.688v-.063c.813.438 1.688.688 2.625.688a5.228 5.228 0 0 1-1.875-2c-.5-.875-.688-1.813-.688-2.75 0-1.063.25-2.063.75-2.938 1.438 1.75 3.188 3.188 5.25 4.25s4.313 1.688 6.688 1.813a5.579 5.579 0 0 1 1.5-5.438c1.125-1.125 2.5-1.688 4.125-1.688s3.063.625 4.188 1.813a11.48 11.48 0 0 0 3.688-1.375c-.438 1.375-1.313 2.438-2.563 3.188 1.125-.125 2.188-.438 3.313-.875z"/></svg>
</a>
<a class="floating-header-share-fb" href="https://www.facebook.com/sharer/sharer.php?u=https://12kdh43.github.io/cs231n_lec12"
onclick="window.open(this.href, 'share-facebook','width=580,height=296');return false;">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"><path d="M19 6h5V0h-5c-3.86 0-7 3.14-7 7v3H8v6h4v16h6V16h5l1-6h-6V7c0-.542.458-1 1-1z"/></svg>
</a>
</div>
<progress class="progress" value="0">
<div class="progress-container">
<span class="progress-bar"></span>
</div>
</progress>
</div>
<!-- /post -->
<!-- The #contentFor helper here will send everything inside it up to the matching #block helper found in default.hbs -->
<!-- Previous/next page links - displayed on every page -->
<!-- The footer at the very bottom of the screen -->
<footer class="site-footer outer">
<div class="site-footer-content inner">
<section class="copyright"><a href="/">Darron's Devlog</a> © 2022</section>
<!--
<section class="poweredby">Proudly published with <a href="https://jekyllrb.com/">Jekyll</a> &
<a href="https://pages.github.com/" target="_blank" rel="noopener">GitHub Pages</a> using
<a href="https://github.com/jekyllt/jasper2" target="_blank" rel="noopener">Jasper2</a></section>
-->
<nav class="site-footer-nav">
<a href="/">Latest Posts</a>
</nav>
</div>
</footer>
</div>
<!-- The big email subscribe modal content -->
<div id="subscribe" class="subscribe-overlay">
<a class="subscribe-overlay-close" href="#"></a>
<div class="subscribe-overlay-content">
<h1 class="subscribe-overlay-title">Search Darron's Devlog</h1>
<p class="subscribe-overlay-description">
</p>
<span id="searchform" method="post" action="/subscribe/" class="">
<input class="confirm" type="hidden" name="confirm" />
<input class="location" type="hidden" name="location" />
<input class="referrer" type="hidden" name="referrer" />
<div class="form-group">
<input class="subscribe-email" onkeyup="myFunc()"
id="searchtext" type="text" name="searchtext"
placeholder="Search..." />
</div>
<script type="text/javascript">
function myFunc() {
if(event.keyCode == 13) {
var url = encodeURIComponent($("#searchtext").val());
location.href = "/search.html?query=" + url;
}
}
</script>
</span>
</div>
</div>
<!-- highlight.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.10.0/components/prism-abap.min.js"></script>
<script>$(document).ready(function() {
$('pre code').each(function(i, block) {
hljs.highlightBlock(block);
});
});</script>
<!-- jQuery + Fitvids, which makes all video embeds responsive -->
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous">
</script>
<script type="text/javascript" src="/assets/js/jquery.fitvids.js"></script>
<script type="text/javascript" src="https://demo.ghost.io/assets/js/jquery.fitvids.js?v=724281a32e"></script>
<!-- Paginator increased to "infinit" in _config.yml -->
<!-- if paginator.posts -->
<!-- <script>
var maxPages = parseInt('');
</script>
<script src="/assets/js/infinitescroll.js"></script> -->
<!-- /endif -->
<!-- Add Google Analytics -->
<!-- Google Analytics Tracking code -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)