该博客是根据台大李宏毅老师的关于GAN的****所整理的笔记,建议大家可以直接看这个老师的视频 (因在youtube,故需翻墙)
Generative Adversarial Network
回顾
Auto-encoder (AE)
简单,但生成的图像的质量很no realistic,视觉上看会很模糊,更像是图像的求均运算
Variational Auto encoder (VAE)
NN Encoder会产生两个向量m , σ ∈ R 3 m, \sigma \in R^3 m , σ ∈ R 3 , 然后再从一个正态分布中采样一个噪声 e ∈ R 3 e \in R^3 e ∈ R 3 ,做 c = exp ( σ ) ⊙ e + m ∈ R 3 c=\exp{(\sigma)} \odot e + m \in R^3 c = exp ( σ ) ⊙ e + m ∈ R 3 ,最后将 c c c 输入到NN Decoder中生成图像。优化的目标是使得 output 与 input 要尽可能地接近,同时为了引入正则项的目的是为了让 σ , m \sigma, m σ , m 尽可能地接近于 0,这样在测试时就可以去掉NN Encoder而直接使用 e e e 作为NN Decoder的输入了。
AE/VAE存在的问题:
即NN Decoder并不会产生真正realisctic的数据。例如图中的两张“7”,二者仅差了一个像素点,人会偏爱左边的,但对该模型来说,二者与真实的“7”均差了一个像素点,故无优劣之分
GAN
Maximum Likelihood Estimation
Given a data distribution P d a t a ( x ) P_{data}(x) P d a t a ( x )
We have a distribution P G ( x ; θ ) P_G(x; \theta) P G ( x ; θ ) parameterized by θ \theta θ
E . g . P G ( x ; θ ) \it{E.g.}\ P_G(x; \theta) E . g . P G ( x ; θ ) is a Gaussian Mixture Model, then θ \theta θ are means and variances of the mixture Gaussian.
goal: find θ \theta θ that P G ( x ; θ ) P_G(x; \theta) P G ( x ; θ ) close to P d a t a ( x ) P_{data}(x) P d a t a ( x )
note: the x here donates the data sampling from (output of) distribution P d a t a ( P G ) , not the input . \color{#F00}{\text{the }x\text{ here donates the data sampling from (output of) distribution }P_{data} (P_G)\text{, not the input}}. the x here donates the data sampling from (output of) distribution P d a t a ( P G ) , not the input .
Sampling { x 1 , x 2 , … , x m } \{x^1,x^2,\dots,x^m\} { x 1 , x 2 , … , x m } from P d a t a ( x ) P_{data}(x) P d a t a ( x ) , we can compute P G ( x i ; θ ) , i = 1 , 2 , … , m P_G(x^i;\theta), i=1,2,\dots,m P G ( x i ; θ ) , i = 1 , 2 , … , m . Then Likelihood of generating the samples is: L = ∏ i = 1 m P G ( x i ; θ ) L=\prod_{i=1}^mP_G(x^i;\theta) L = ∏ i = 1 m P G ( x i ; θ ) . Our goal is to find θ ∗ \theta^* θ ∗ maximizing the likelihood:θ ∗ = arg max θ ∏ i = 1 m P G ( x i ; θ ) = arg max θ log ∏ i = 1 m P G ( x i ; θ ) = arg max θ ∑ i = 1 m log P G ( x i ; θ ) ( ∵ { x i , … , x m } from P d a t a ) ≈ arg max θ E x ∼ P d a t a log P G ( x ; θ ) ( when m = + ∞ ) = arg max θ ∫ x P d a t a ( x ) log P G ( x ; θ ) d x − ∫ x P d a t a ( x ) log P d a t a ( x ) d x ⎵ 与 θ 无 关 , 为 一 个 常 数 项 , 不 会 影 响 θ ∗ 的 求 值 = arg max θ ∫ x − P d a t a ( x ) log P d a t a ( x ) P G ( x ; θ ) d x = arg min θ KL ( P d a t a ( x ) ∥ P G ( x ; θ ) )
\begin{array}{lll}
\theta^* &=& \arg\max_{\theta} \prod_{i=1}^m P_G(x^i; \theta) \\
&=& \arg\max_{\theta} \log \prod_{i=1}^m P_G(x^i; \theta) \\
&=& \arg\max_{\theta} \sum_{i=1}^m \log P_G(x^i;\theta)\ (\because \{x^i,\dots,x^m\}\text{ from }P_{data}) \\
&\approx& \arg\max_{\theta} E_{x \sim P_{data}}\log P_G(x;\theta)\ (\text{when }m=+\infty) \\
&=& \arg\max_{\theta} \int_x P_{data}(x) \log P_G(x;\theta) dx - \underbrace{\int_x P_{data}(x) \log P_{data}(x) dx}_{与\theta无关,为一个常数项,不会影响\theta^*的求值} \\
&=& \arg\max_{\theta} \int_x -P_{data}(x) \log \frac{P_{data}(x)}{P_G(x;\theta)}dx\\
&=& \arg\min_{\theta} \text{KL}(P_{data}(x) \|P_G(x;\theta))
\end{array}
θ ∗ = = = ≈ = = = arg max θ ∏ i = 1 m P G ( x i ; θ ) arg max θ log ∏ i = 1 m P G ( x i ; θ ) arg max θ ∑ i = 1 m log P G ( x i ; θ ) ( ∵ { x i , … , x m } from P d a t a ) arg max θ E x ∼ P d a t a log P G ( x ; θ ) ( when m = + ∞ ) arg max θ ∫ x P d a t a ( x ) log P G ( x ; θ ) d x − 与 θ 无 关 , 为 一 个 常 数 项 , 不 会 影 响 θ ∗ 的 求 值 ∫ x P d a t a ( x ) log P d a t a ( x ) d x arg max θ ∫ x − P d a t a ( x ) log P G ( x ; θ ) P d a t a ( x ) d x arg min θ KL ( P d a t a ( x ) ∥ P G ( x ; θ ) )
即 θ ∗ \theta^* θ ∗ 其实就是使得 P d a t a ( x ) P_{data}(x) P d a t a ( x ) 与 P G ( x ) P_G(x) P G ( x ) 尽可能的接近。在实际中,Gaussian mixture model无法完全拟合 P d a t a P_{data} P d a t a ,故用 NN 来生成 P G P_G P G 从而来拟合 P d a t a P_{data} P d a t a
然而,这会很难计算 likelihood(因为 P d a t a P_{data} P d a t a 和 P G P_G P G 均是未知的),而 GAN 则可解决该问题
Baisc Idea of GAN
Generator G G G
G G G is a function, input z z z , output x x x
Given a prior discribution P prior ( z ) P_\text{prior}(z) P prior ( z ) , a probability discribution P G ( x ) P_G(x) P G ( x ) is defined by G G G
Discriminator D D D
D D D is a function, input x x x , output a scalar probability whether x x x comes from a real dataset or not, i . e . \it{i.e.} i . e . , from P d a t a ( x ) P_{data}(x) P d a t a ( x ) or P G ( x ) P_G(x) P G ( x )
Optimization function:(1) arg min G max D E x ∼ P d a t a [ log D ( x ) ] + E x ∼ P G [ log ( 1 − D ( x ) ) ] = arg min G max D V ( G , D )
\begin{array}{ll}
&\arg \min_G \max_D E_{x \sim P_{data}}[\log D(x)] + E_{x \sim P_G}[\log(1-D(x))] \\
=&\arg \min_G \max_D V(G, D)
\end{array} \tag{1}
= arg min G max D E x ∼ P d a t a [ log D ( x ) ] + E x ∼ P G [ log ( 1 − D ( x ) ) ] arg min G max D V ( G , D ) ( 1 )
给定 G G G ,求解 D ∗ D^* D ∗ 使其最大化 V ( G , V ) V(G,V) V ( G , V ) (2) max D V = max D E x ∼ P d a t a [ log D ( x ) ] + E x ∼ P G [ log ( 1 − D ( x ) ) ] = max D ∫ x P d a t a ( x ) log D ( x ) + P G ( x ) log ( 1 − D ( x ) ) d x ( 积分号可合并的原因:理想中, P G ≈ P d a t a ,即分别来自 P d a t a 和 P G 的 x 可视为同一个 x ) = max D f ( D )
\begin{array}{lll}
\max_D V &=& \max_D E_{x \sim P_{data}}[\log D(x)] + E_{x \sim P_G}[\log(1-D(x))] \\
&=& \max_D \int_x P_{data}(x) \log D(x) + P_G(x) \log(1-D(x)) dx \\
& & (\text{积分号可合并的原因:理想中,$P_G \approx P_{data}$,即分别来自 $P_{data}$ 和 $P_G$ 的 $x$ 可视为同一个 $x$ }) \\
&=& \max_D f(D)
\end{array} \tag{2}
max D V = = = max D E x ∼ P d a t a [ log D ( x ) ] + E x ∼ P G [ log ( 1 − D ( x ) ) ] max D ∫ x P d a t a ( x ) log D ( x ) + P G ( x ) log ( 1 − D ( x ) ) d x ( 积分号可合并的原因:理想中, P G ≈ P d a t a ,即分别来自 P d a t a 和 P G 的 x 可视为同一个 x ) max D f ( D ) ( 2 )
然后对 f ( D ) f(D) f ( D ) 对于自变量 D D D 求导使其为0即可求解出D ∗ D^* D ∗ ,结果为 D ∗ ( x ) = P d a t a ( x ) P d a t a ( x ) + P G ( x ) D^*(x)=\displaystyle\frac{P_{data}(x)}{P_{data}(x)+P_G(x)} D ∗ ( x ) = P d a t a ( x ) + P G ( x ) P d a t a ( x )
在得出 D ∗ D^* D ∗ 后,将其代入 V ( G , D ) V(G,D) V ( G , D ) 然后求解 G ∗ G^* G ∗ 使其最小化 V ( G , D ∗ ) V(G,D^*) V ( G , D ∗ ) (3) min G V ( G , D ∗ ) = min G E x ∼ P d a t a [ log D ∗ ( x ) ] + E x ∼ P G [ log ( 1 − D ∗ ( x ) ) ] = − 2 log 2 + min G ∫ x P d a t a ( x ) log P d a t a ( x ) ( P d a t a ( x ) + P G ( x ) ) / 2 + P G ( x ) log P G ( x ) ( P d a t a ( x ) + P G ( x ) ) / 2 d x = − 2 log 2 + min G ( KL ( P d a t a ( x ) ∥ P d a t a ( x ) + P G ( x ) 2 ) + KL ( P G ( x ) ∥ P d a t a ( x ) + P G ( x ) 2 ) ) = − 2 log 2 + min G 2 JSD ( P d a t a ( x ) ∥ P G ( x ) ) ≤ 0
\begin{array}{lll}
\min_G V(G, D^*) &=& \min_G E_{x \sim P_{data}}[\log D^*(x)] + E_{x \sim P_G}[\log(1-D^*(x))] \\[.6em]
&=& -2 \log 2 + \min_G \int_x P_{data}(x) \log \frac{P_{data}(x)}{(P_{data}(x)+P_G(x))/2} + P_G(x) \log \frac{P_G(x)}{(P_{data}(x)+P_G(x))/2} dx \\[.6em]
&= &-2 \log 2 + \min_G (\text{KL}(P_{data}(x)\|\frac{P_{data}(x)+P_G(x)}{2}) + \text{KL}(P_G(x)\|\frac{P_{data}(x)+P_G(x)}{2})) \\[.6em]
&=& -2 \log 2 + \min_G 2 \text{JSD}(P_{data}(x)\|P_G(x)) \leq 0
\end{array} \tag{3}
min G V ( G , D ∗ ) = = = = min G E x ∼ P d a t a [ log D ∗ ( x ) ] + E x ∼ P G [ log ( 1 − D ∗ ( x ) ) ] − 2 log 2 + min G ∫ x P d a t a ( x ) log ( P d a t a ( x ) + P G ( x ) ) / 2 P d a t a ( x ) + P G ( x ) log ( P d a t a ( x ) + P G ( x ) ) / 2 P G ( x ) d x − 2 log 2 + min G ( KL ( P d a t a ( x ) ∥ 2 P d a t a ( x ) + P G ( x ) ) + KL ( P G ( x ) ∥ 2 P d a t a ( x ) + P G ( x ) ) ) − 2 log 2 + min G 2 JSD ( P d a t a ( x ) ∥ P G ( x ) ) ≤ 0 ( 3 )
其中 JSD ( P ∥ Q ) = 1 2 KL ( P ∥ M ) + 1 2 KL ( Q ∥ M ) , M = P + Q 2 \text{JSD}(P\|Q)=\frac{1}{2} \text{KL}(P\|M) + \frac{1}{2} \text{KL}(Q\|M),\ M=\frac{P+Q}{2} JSD ( P ∥ Q ) = 2 1 KL ( P ∥ M ) + 2 1 KL ( Q ∥ M ) , M = 2 P + Q ,它是对称的,其值域为[ 0 , log 2 ] [0,\log2] [ 0 , log 2 ] ,若 P , Q P,Q P , Q 完全一样,则值为0,若完全无关,则为log 2 \log2 log 2 。相反,KL \text{KL} KL 是非对称的。注意。从该式上看,最小化 G G G 其实就是使得 P G → P d a t a P_G \to P_{data} P G → P d a t a
上面是从理论上进行的求解分析,而在实际中,无需那么复杂,实际的优化过程如下:
存在问题
Discriminator通过JS散度能告诉Generator的有效信息很少 。在实际的训练中可发现,即使generator能产生相对realistic的图片,discriminator也总是能够区分出哪些是真实的图片,哪些是伪造的图片。同时,随着迭代进行,即使generator以及能够产生越来越realistic的图片,它的loss其实会维持在一个特定的值,如下图所示:
这会造成在实际中难以确定合适的迭代次数,且无法让generator提高效果。原因如下:
在实际中,开始训练时,P d a t a ∩ P G → ∅ P_{data} \cap P_G \to \empty P d a t a ∩ P G → ∅ ,即两个分布几乎没有交集,这就造成JSD ( P d a t a ∥ P G ) = log 2 \text{JSD}(P_{data}\|P_G)=\log2 JSD ( P d a t a ∥ P G ) = log 2 ,即公式(3)的 min G V ( G , D ∗ ) = 0 \min_GV(G,D^*)=0 min G V ( G , D ∗ ) = 0 ,因此 G 就无法得到优化:
一种比较常见的解决方案:
Mode Collapse. 模型坍塌 ,即许多个不同的noise会生成同样一幅图片(即generator只会生成一些简单的、重复的样本,缺乏多样性)。例如,原数据集中10个类,但Generator只会生成其中1/2个类的图片,此时就称出现了模型坍塌问题