GAN与WGAN

文章目录

      • GAN
        • 判别网络
        • 生成网络
        • 训练
      • GAN存在的问题
        • 训练稳定性
        • 模型坍塌
      • 改进方法:WGAN
        • Wasserstein \text{Wasserstein} Wasserstein距离
        • 评价网络
        • 生成网络
      • 开源代码

GAN

生成对抗网络(GAN, Generative Adversarial Networks)是2014年由Goodfellow提出的模型。在GAN中,有两个网络进行对抗训练,一个是判别网络,目标是判断一个样本是真实数据还是由生成网络生成的数据,一个是生成网络,目标是生成判别网络无法区分的样本。这两个网络交替训练,到最后如果判别网络再也无法区分样本的来源,那么也就等价于生成网络可以生成符合数据真实分布的样本。

判别网络

判别网络 D ( x ; ϕ ) D(\mathbf{x};\phi) D(x;ϕ)是一个二分类的分类器,目标是区分一个样本 x \mathbf{x} x是来自于真实分布 p r ( x ) p_r(\mathbf{x}) pr(x)还是模型生成分布 p θ ( x ) p_{\theta}(\mathbf{x}) pθ(x)。用标签 y = 1 y=1 y=1表示样本来自真实分布,标签 y = 0 y=0 y=0表示样本来自模型生成分布,判别网络的输出是 x \mathbf{x} x属于真实分布的概率 。
p ( y = 1 ∣ x ) = D ( x ; ϕ ) p ( y = 0 ∣ x ) = 1 − D ( x ; ϕ ) (1) \begin{aligned} p(y=1|\mathbf{x})&=D(\mathbf{x};\phi)\\ p(y=0|\mathbf{x})&=1-D(\mathbf{x};\phi) \end{aligned}\tag{1} p(y=1x)p(y=0x)=D(x;ϕ)=1D(x;ϕ)(1)
那么判别网络的目标函数就是最小化交叉熵
min ⁡ ϕ − ( E x [ y log ⁡ p ( y = 1 ∣ x ) + ( 1 − y ) p ( y = 0 ∣ x ) ] ) (2) \min_\phi-(\mathbb{E}_{\mathbf{x}}\left[y\log p(y=1|\mathbf{x})+(1-y)p(y=0|\mathbf{x})\right])\tag{2} ϕmin(Ex[ylogp(y=1x)+(1y)p(y=0x)])(2)
若分布 p ( x ) p(\mathbf{x}) p(x) p r ( x ) p_r(\mathbf{x}) pr(x) p θ ( x ) p_{\theta}(\mathbf{x}) pθ(x)等比例混合而成,即 p ( x ) = 1 2 ( p r ( x ) + p θ ( x ) ) p(\mathbf{x})=\frac{1}{2}(p_r(\mathbf{x})+p_{\theta}(\mathbf{x})) p(x)=21(pr(x)+pθ(x)),则上式等价于
max ⁡ ϕ E x ∼ p r ( x ) [ log ⁡ D ( x ; ϕ ) ] + E x ∼ p θ ( x ) [ log ⁡ ( 1 − D ( x ; ϕ ) ) ] = max ⁡ ϕ E x ∼ p r ( x ) [ log ⁡ D ( x ; ϕ ) ] + E z ∼ p ( z ) [ log ⁡ ( 1 − D ( G ( z ; θ ) ; ϕ ) ) ] (3) \begin{aligned} &\max_\phi\mathbb{E}_{\mathbf{x}\sim p_r(\mathbf{x})}\left[\log D(\mathbf{x};\phi)]+\mathbb{E}_{\mathbf{x}\sim p_{\theta}(\mathbf{x})}[\log(1-D(\mathbf{x};\phi))\right]\\ =&\max_\phi\mathbb{E}_{\mathbf{x}\sim p_r(\mathbf{x})}\left[\log D(\mathbf{x};\phi)]+\mathbb{E}_{\mathbf{z}\sim p(\mathbf{z})}[\log(1-D(G(\mathbf{z};\theta);\phi))\right] \end{aligned}\tag{3} =ϕmaxExpr(x)[logD(x;ϕ)]+Expθ(x)[log(1D(x;ϕ))]ϕmaxExpr(x)[logD(x;ϕ)]+Ezp(z)[log(1D(G(z;θ);ϕ))](3)
G ( z ; θ ) G(\mathbf{z};\theta) G(z;θ)是生成网络, z \mathbf{z} z是一个低维向量, p ( z ) p(\mathbf{z}) p(z)是它服从的分布,通常是标准正态分布

生成网络

生成网络的目标是生成判别网络无法区分的样本,即让判别网络将自己生成的样本判别为真实样本
max ⁡ θ E z ∼ p ( z ) [ log ⁡ ( D ( G ( z ; θ ) ; ϕ ) ) ] = min ⁡ θ E z ∼ p ( z ) [ log ⁡ ( 1 − D ( G ( z ; θ ) ; ϕ ) ) ] (4) \begin{aligned} &\max_\theta\mathbb{E}_{\mathbf{z}\sim p(\mathbf{z})}\left[\log(D(G(\mathbf{z};\theta);\phi))\right]\\ =&\min_\theta\mathbb{E}_{\mathbf{z}\sim p(\mathbf{z})}\left[\log(1-D(G(\mathbf{z};\theta);\phi))\right] \end{aligned}\tag{4} =θmaxEzp(z)[log(D(G(z;θ);ϕ))]θminEzp(z)[log(1D(G(z;θ);ϕ))](4)
这两个目标函数是等价的,但一般使用前者

训练

GAN的两个网络的优化目标相反,因此很难训练,需要平衡两个网络的能力。判别网络不能太强,否则生成网络会出现梯度消失,也不能太弱,否则生成网络无法提升能力。通常每次迭代时,判别网络更新 K K K次,生成网络更新一次, K K K是一个超参数。

GAN存在的问题

将判别网络和生成网络合并为一个整体,这个模型仅用来分析,通常实际中不会使用
min ⁡ θ max ⁡ ϕ ( E x ∼ p r ( x ) [ log ⁡ D ( x ; ϕ ) ] + E x ∼ p θ ( x ) [ log ⁡ ( 1 − D ( x ; ϕ ) ) ] ) = min ⁡ θ max ⁡ ϕ ( E x ∼ p r ( x ) [ log ⁡ D ( x ; ϕ ) ] + E z ∼ p ( z ) [ log ⁡ ( 1 − D ( G ( z ; θ ) ; ϕ ) ) ] ) (5) \begin{aligned} &\min_{\theta}\max_{\phi}(\mathbb{E}_{\mathbf{x}\sim p_r(\mathbf{x})}\left[\log D(\mathbf{x};\phi)]+\mathbb{E}_{\mathbf{x}\sim p_{\theta}(\mathbf{x})}[\log(1-D(\mathbf{x};\phi))\right])\\ =&\min_{\theta}\max_{\phi}(\mathbb{E}_{\mathbf{x}\sim p_r(\mathbf{x})}\left[\log D(\mathbf{x};\phi)]+\mathbb{E}_{\mathbf{z}\sim p(\mathbf{z})}[\log(1-D(G(\mathbf{z};\theta);\phi))\right]) \end{aligned}\tag{5} =θminϕmax(Expr(x)[logD(x;ϕ)]+Expθ(x)[log(1D(x;ϕ))])θminϕmax(Expr(x)[logD(x;ϕ)]+Ezp(z)[log(1D(G(z;θ);ϕ))])(5)
p r ( x ) p_r(\mathbf{x}) pr(x) p θ ( x ) p_{\theta}(\mathbf{x}) pθ(x)已知时,上式的积分项
p r ( x ) log ⁡ D ( x ; ϕ ) + p θ ( x ) log ⁡ ( 1 − D ( x ; ϕ ) ) (6) p_r(\mathbf{x})\log D(\mathbf{x};\phi)+p_{\theta}(\mathbf{x})\log(1-D(\mathbf{x};\phi))\tag{6} pr(x)logD(x;ϕ)+pθ(x)log(1D(x;ϕ))(6)

D ( x ; ϕ ) = p r ( x ) p r ( x ) + p θ ( x ) (7) D(\mathbf{x};\phi)=\frac{p_r(\mathbf{x})}{p_r(\mathbf{x})+p_{\theta}(\mathbf{x})}\tag{7} D(x;ϕ)=pr(x)+pθ(x)pr(x)(7)
取得最大值,这也就是最优判别器,将其代入 ( 5 ) (5) (5)式得
L ( G ∣ D ⋆ ) = E x ∼ p r ( x ) [ log ⁡ D ⋆ ( x ) ] + E x ∼ p θ ( x ) [ log ⁡ ( 1 − D ⋆ ( x ) ) ] = E x ∼ p r ( x ) [ log ⁡ p r ( x ) p r ( x ) + p θ ( x ) ] + E x ∼ p θ ( x ) [ log ⁡ ( p θ ( x ) p r ( x ) + p θ ( x ) ) ] = KL ( p r , p a ) + KL ( p θ , p a ) − 2 log ⁡ 2 = 2 JS ( p r , p θ ) − 2 log ⁡ 2 (8) \begin{aligned} \mathcal{L}(G|D^{\star})&=\mathbb{E}_{\mathbf{x}\sim p_r(\mathbf{x})}\left[\log D^{\star}(\mathbf{x})]+\mathbb{E}_{\mathbf{x}\sim p_{\theta}(\mathbf{x})}[\log(1-D^{\star}(\mathbf{x}))\right]\\ &=\mathbb{E}_{\mathbf{x}\sim p_r(\mathbf{x})}\left[\log \frac{p_r(\mathbf{x})}{p_r(\mathbf{x})+p_{\theta}(\mathbf{x})}\right]+\mathbb{E}_{\mathbf{x}\sim p_{\theta}(\mathbf{x})}\left[\log(\frac{p_{\theta}(\mathbf{x})}{p_r(\mathbf{x})+p_{\theta}(\mathbf{x})})\right]\\ &=\text{KL}(p_r,p_a)+\text{KL}(p_{\theta},p_a)-2\log2\\ &=2\text{JS}(p_r, p_{\theta})-2\log2 \end{aligned}\tag{8} L(GD)=Expr(x)[logD(x)]+Expθ(x)[log(1D(x))]=Expr(x)[logpr(x)+pθ(x)pr(x)]+Expθ(x)[log(pr(x)+pθ(x)pθ(x))]=KL(pr,pa)+KL(pθ,pa)2log2=2JS(pr,pθ)2log2(8)
其中 JS(.) \text{JS(.)} JS(.) JS \text{JS} JS散度, p a ( x ) = 1 2 ( p r ( x ) + p θ ( x ) ) p_a(\mathbf{x})=\frac{1}{2}(p_r(\mathbf{x})+p_{\theta}(\mathbf{x})) pa(x)=21(pr(x)+pθ(x))为一个平均分布。

当判别网络最优时,生成网络的目标是最小化真实分布与生成分布之间的 JS \text{JS} JS散度。当两个分布相同时, JS \text{JS} JS散度为 0 0 0,最优生成器对应的损失为 − 2 log ⁡ 2 -2\log2 2log2

训练稳定性

当两个分布没有重叠时, JS \text{JS} JS散度等于常数 log ⁡ 2 \log2 log2
2 JS ( p r , p θ ) = ∫ p r ( x ) log ⁡ 2 p r ( x ) p r ( x ) + p θ ( x ) d x + ∫ p θ ( x ) log ⁡ 2 p θ ( x ) p r ( x ) + p θ ( x ) d x = ∫ p r ( x ) ≠ 0 , p θ ( x ) = 0 p r ( x ) log ⁡ 2 p r ( x ) p r ( x ) + p θ ( x ) d x + ∫ p θ ( x ) ≠ 0 , p r ( x ) = 0 p θ ( x ) log ⁡ 2 p θ ( x ) p r ( x ) + p θ ( x ) d x = ∫ p r ( x ) ≠ 0 , p θ ( x ) = 0 p r ( x ) log ⁡ 2 d x + ∫ p θ ( x ) ≠ 0 , p r ( x ) = 0 p θ ( x ) log ⁡ 2 d x = 2 log ⁡ 2 (9) \begin{aligned} 2\text{JS}(p_r, p_{\theta})&=\int p_r(\mathbf{x})\log\frac{2p_r(\mathbf{x})}{p_r(\mathbf{x})+p_{\theta}(\mathbf{x})}d\mathbf{x}+\int p_{\theta}(\mathbf{x})\log\frac{2p_{\theta}(\mathbf{x})}{p_r(\mathbf{x})+p_{\theta}(\mathbf{x})}d\mathbf{x}\\ &=\int_{p_r(\mathbf{x})\neq0,p_{\theta}(\mathbf{x})=0} p_r(\mathbf{x})\log\frac{2p_r(\mathbf{x})}{p_r(\mathbf{x})+p_{\theta}(\mathbf{x})}d\mathbf{x}+\int_{p_{\theta}(\mathbf{x})\neq0,p_r(\mathbf{x})=0} p_{\theta}(\mathbf{x})\log\frac{2p_{\theta}(\mathbf{x})}{p_r(\mathbf{x})+p_{\theta}(\mathbf{x})}d\mathbf{x}\\ &=\int_{p_r(\mathbf{x})\neq0,p_{\theta}(\mathbf{x})=0} p_r(\mathbf{x})\log2d\mathbf{x}+\int_{p_{\theta}(\mathbf{x})\neq0,p_r(\mathbf{x})=0} p_{\theta}(\mathbf{x})\log2d\mathbf{x}\\ &=2\log2 \end{aligned}\tag{9} 2JS(pr,pθ)=pr(x)logpr(x)+pθ(x)2pr(x)dx+pθ(x)logpr(x)+pθ(x)2pθ(x)dx=pr(x)=0,pθ(x)=0pr(x)logpr(x)+pθ(x)2pr(x)dx+pθ(x)=0,pr(x)=0pθ(x)logpr(x)+pθ(x)2pθ(x)dx=pr(x)=0,pθ(x)=0pr(x)log2dx+pθ(x)=0,pr(x)=0pθ(x)log2dx=2log2(9)
对生成网络来说,目标函数关于参数的梯度为0,因此生成网络出现梯度消失。因此,在实际训练生成对抗网络时,一般不会将判别网络训练到最优,使得生成网络的梯度依然存在,但是判别网络也不能太差,否则生成网络的梯度为错误的梯度。但是如果在两者之间权衡并不容易,因此GAN训练稳定性较差。

模型坍塌

如果使用公式 ( 3 ) (3) (3)中的前者作为生成网络的目标函数,将最优判别器 D ⋆ D^{\star} D代入,并结合公式 ( 8 ) (8) (8)可得
L ′ ( G ∣ D ⋆ ) = E x ∼ p θ ( x ) [ log ⁡ D ⋆ ( x ) ] = E x ∼ p θ ( x ) [ log ⁡ p r ( x ) p r ( x ) + p θ ( x ) ⋅ p θ ( x ) p θ ( x ) ] = − E x ∼ p θ ( x ) [ log ⁡ p θ ( x ) p r ( x ) ] + E x ∼ p θ ( x ) [ log ⁡ p θ ( x ) p r ( x ) + p θ ( x ) ] = − KL ( p θ , p r ) + 2 JS ( p r , p θ ) − 2 log ⁡ 2 − E x ∼ p r ( x ) [ log ⁡ D ⋆ ( x ) ] (10) \begin{aligned} \mathcal{L'}(G|D^{\star})&=\mathbb{E}_{\mathbf{x}\sim p_{\theta}(\mathbf{x})}\left[\log D^{\star}(\mathbf{x})\right]\\ &=\mathbb{E}_{\mathbf{x}\sim p_{\theta}(\mathbf{x})}\left[\log \frac{p_r(\mathbf{x})}{p_r(\mathbf{x})+p_{\theta}(\mathbf{x})}\cdot\frac{p_{\theta}(\mathbf{x})}{p_{\theta}(\mathbf{x})}\right]\\ &=-\mathbb{E}_{\mathbf{x}\sim p_{\theta}(\mathbf{x})}\left[\log\frac{p_{\theta}(\mathbf{x})}{p_r(\mathbf{x})}\right]+\mathbb{E}_{\mathbf{x}\sim p_{\theta}(\mathbf{x})}\left[\log\frac{p_{\theta}(\mathbf{x})}{p_r(\mathbf{x})+p_{\theta}(\mathbf{x})}\right]\\ &=-\text{KL}(p_{\theta},p_r)+2\text{JS}(p_r,p_{\theta})-2\log2-\mathbb{E}_{\mathbf{x}\sim p_r(\mathbf{x})}\left[\log D^{\star}(\mathbf{x})\right] \end{aligned}\tag{10} L(GD)=Expθ(x)[logD(x)]=Expθ(x)[logpr(x)+pθ(x)pr(x)pθ(x)pθ(x)]=Expθ(x)[logpr(x)pθ(x)]+Expθ(x)[logpr(x)+pθ(x)pθ(x)]=KL(pθ,pr)+2JS(pr,pθ)2log2Expr(x)[logD(x)](10)
忽略后两项,可以写作
arg ⁡ max ⁡ θ L ′ ( G ∣ D ⋆ ) = arg ⁡ min ⁡ θ KL ( p θ , p r ) − 2 JS ( p r , p θ ) (11) \mathop{\arg\max}\limits_{\theta}\enspace\mathcal{L'}(G|D^{\star})=\mathop{\arg\min}\limits_{\theta}\enspace\text{KL}(p_{\theta},p_r)-2\text{JS}(p_r,p_{\theta})\tag{11} θargmaxL(GD)=θargminKL(pθ,pr)2JS(pr,pθ)(11)
JS \text{JS} JS散度有界,因此生成网络的目标更多的是受 KL ( p θ , p r ) \text{KL}(p_{\theta},p_r) KL(pθ,pr)的影响。

前向和逆向 KL \text{KL} KL散度 KL \text{KL} KL散度非对称,在计算真实分布 p r p_r pr和模型生成分布 p θ p_{\theta} pθ之间的 KL \text{KL} KL散度时就会有两种:前向 KL \text{KL} KL散度 KL ( p r , p θ ) \text{KL}(p_r,p_{\theta}) KL(pr,pθ)和逆向 KL \text{KL} KL散度 KL ( p θ , p r ) \text{KL}(p_{\theta},p_r) KL(pθ,pr),分别定义为
KL ( p r , p θ ) = ∫ p r ( x ) log ⁡ p r ( x ) p θ ( x ) d x KL ( p θ , p r ) = ∫ p θ ( x ) log ⁡ p θ ( x ) p r ( x ) d x (12) \text{KL}(p_r,p_{\theta})=\int p_r(\mathbf{x})\log\frac{p_r(\mathbf{x})}{p_{\theta}(\mathbf{x})}d\mathbf{x}\\ \text{KL}(p_{\theta},p_r)=\int p_{\theta}(\mathbf{x})\log\frac{p_{\theta}(\mathbf{x})}{p_r(\mathbf{x})}d\mathbf{x}\\\tag{12} KL(pr,pθ)=pr(x)logpθ(x)pr(x)dxKL(pθ,pr)=pθ(x)logpr(x)pθ(x)dx(12)
在前向 KL \text{KL} KL散度中,

  • p r ( x ) → 0 p_r(\mathbf{x})\to0 pr(x)0 p θ ( x ) > 0 p_{\theta}(\mathbf{x})>0 pθ(x)>0时, p r ( x ) log ⁡ p r ( x ) p θ ( x ) → 0 p_r(\mathbf{x})\log\frac{p_r(\mathbf{x})}{p_{\theta}(\mathbf{x})}\to 0 pr(x)logpθ(x)pr(x)0,前向 KL \text{KL} KL散度变为0
  • p θ ( x ) → 0 p_{\theta}(\mathbf{x})\to0 pθ(x)0 p r ( x ) > 0 p_r(\mathbf{x})>0 pr(x)>0是, p r ( x ) log ⁡ p r ( x ) p θ ( x ) → ∞ p_r(\mathbf{x})\log\frac{p_r(\mathbf{x})}{p_{\theta}(\mathbf{x})}\to \infin pr(x)logpθ(x)pr(x),前向 KL \text{KL} KL散度变得非常大

因此前向 KL \text{KL} KL散度会让 p θ ( x ) p_{\theta}(\mathbf{x}) pθ(x)尽量覆盖 p r ( x ) p_r(\mathbf{x}) pr(x),生成的样本更具有多样性。

在逆向 KL \text{KL} KL散度中

  • p r ( x ) → 0 p_r(\mathbf{x})\to0 pr(x)0 p θ ( x ) > 0 p_{\theta}(\mathbf{x})>0 pθ(x)>0时, p θ ( x ) log ⁡ p θ ( x ) p r ( x ) → ∞ p_{\theta}(\mathbf{x})\log\frac{p_{\theta}(\mathbf{x})}{p_r(\mathbf{x})}\to\infin pθ(x)logpr(x)pθ(x),逆向 KL \text{KL} KL散度变得非常大
  • p θ ( x ) → 0 p_{\theta}(\mathbf{x})\to0 pθ(x)0 p r ( x ) > 0 p_r(\mathbf{x})>0 pr(x)>0时, p θ ( x ) log ⁡ p θ ( x ) p r ( x ) → 0 p_{\theta}(\mathbf{x})\log\frac{p_{\theta}(\mathbf{x})}{p_r(\mathbf{x})}\to 0 pθ(x)logpr(x)pθ(x)0,逆向 KL \text{KL} KL散度变为0

因此逆向 KL \text{KL} KL散度会让 p θ ( x ) p_{\theta}(\mathbf{x}) pθ(x)尽量避开 p r ( x ) ≈ 0 p_r(\mathbf{x})\approx0 pr(x)0的点,即倾向于生成更安全的样本。

生成网络中使用的是逆向 KL \text{KL} KL散度,倾向于生成更安全的样本,也就是所谓的模型坍塌问题。

改进方法:WGAN

Wasserstein \text{Wasserstein} Wasserstein距离

WGAN通过 Wasserstein \text{Wasserstein} Wasserstein距离替代 JS \text{JS} JS散度。 Wasserstein \text{Wasserstein} Wasserstein距离也用于衡量两个分布之间的距离,对于两个分布 q 1 q_1 q1 q 2 q_2 q2 p t h p^{th} pth- Wasserstein \text{Wasserstein} Wasserstein定义为
W p ( q 1 , q 2 ) = ( inf ⁡ γ ( x , y ) ∈ Γ ( q 1 , q 2 ) E ( x , y ) ∼ γ ( x , y ) [ d ( x , y ) p ] ) 1 p (13) W_p(q_1, q_2)=\left(\inf_{\gamma(x,y)\in\Gamma(q_1,q_2)}\mathbb{E}_{(x,y)\sim\gamma(x,y)}\left[d(x,y)^p\right]\right)^{\frac{1}{p}}\tag{13} Wp(q1,q2)=(γ(x,y)Γ(q1,q2)infE(x,y)γ(x,y)[d(x,y)p])p1(13)
Γ ( q 1 , q 2 ) \Gamma(q_1,q_2) Γ(q1,q2)为边缘分布为 q 1 q_1 q1 q 2 q_2 q2的所有可能的联合分布集合, d ( x , y ) d(x,y) d(x,y) x x x y y y的距离,如 l p \mathcal{l}_p lp距离。当两个分布没有重叠或重叠非常少时, JS \text{JS} JS散度和 KL \text{KL} KL散度均不能再反映两个分布之间的距离,但 Wasserstein \text{Wasserstein} Wasserstein距离仍然可以。

对于真实分布 p r p_r pr和模型分布 p θ p_{\theta} pθ 1st \text{1st} 1st- Wasserstein \text{Wasserstein} Wasserstein距离定义为
W 1 ( p r , p θ ) = inf ⁡ γ ( x , y ) ∈ Γ ( p r , p θ ) E ( x , y ) ∼ γ ( x , y ) [ ∣ ∣ x − y ∣ ∣ ] (14) W_1(p_r,p_{\theta})=\inf_{\gamma(\mathbf{x},\mathbf{y})\in\Gamma(p_r,p_{\theta})}\mathbb{E}_{(\mathbf{x},\mathbf{y})\sim\gamma(\mathbf{x},\mathbf{y})}\left[\vert\vert \mathbf{x}-\mathbf{y}\vert\vert\right]\tag{14} W1(pr,pθ)=γ(x,y)Γ(pr,pθ)infE(x,y)γ(x,y)[xy](14)
显然这是很难计算的,但 1st \text{1st} 1st- Wasserstein \text{Wasserstein} Wasserstein距离有一个对偶形式
W 1 ( p r , p θ ) = sup ⁡ ∣ ∣ f ∣ ∣ L ≤ 1 ( E x ∼ p r ( x ) [ f ( x ) ] − E x ∼ p θ ( x ) [ f ( x ) ] ) (15) W_1(p_r,p_{\theta})=\sup_{\vert\vert f\vert\vert_L\leq1}\left(\mathbb{E}_{\mathbf{x}\sim p_r(\mathbf{x})}\left[f(\mathbf{x})\right]-\mathbb{E}_{\mathbf{x}\sim p_{\theta}(\mathbf{x})}\left[f(\mathbf{x})\right]\right)\tag{15} W1(pr,pθ)=fL1sup(Expr(x)[f(x)]Expθ(x)[f(x)])(15)
其中 f f f R d → R \mathbb{R}^d\to\mathbb{R} RdR 1 1 1- Lipschitz \text{Lipschitz} Lipschitz连续函数,对任意 x ≠ y \mathbf{x}\neq\mathbf{y} x=y满足
∣ f ( x ) − f ( y ) ∣ ∣ x − y ∣ ≤ 1 (16) \frac{\vert f(\mathbf{x})-f(\mathbf{y})\vert}{\vert\mathbf{x}-\mathbf{y} \vert}\leq1\tag{16} xyf(x)f(y)1(16)
根据公式 ( 15 ) (15) (15),两个分布 p r p_r pr p θ p_{\theta} pθ之间的 1st \text{1st} 1st- Wasserstein \text{Wasserstein} Wasserstein距离可以转换为一个满足 1 1 1- Lipschitz \text{Lipschitz} Lipschitz连续的函数在分布 p r p_r pr p θ p_{\theta} pθ下期望差的上界,通常 1 1 1- Lipschitz \text{Lipschitz} Lipschitz连续可以宽松为 K \text{K} K- Lipschitz \text{Lipschitz} Lipschitz连续,则 1st \text{1st} 1st- Wasserstein \text{Wasserstein} Wasserstein距离为
W 1 ( p r , p θ ) = 1 K sup ⁡ ∣ ∣ f ∣ ∣ L ≤ K ( E x ∼ p r ( x ) [ f ( x ) ] − E x ∼ p θ ( x ) [ f ( x ) ] ) (17) W_1(p_r,p_{\theta})=\frac{1}{K}\sup_{\vert\vert f\vert\vert_L\leq K}\left(\mathbb{E}_{\mathbf{x}\sim p_r(\mathbf{x})}\left[f(\mathbf{x})\right]-\mathbb{E}_{\mathbf{x}\sim p_{\theta}(\mathbf{x})}\left[f(\mathbf{x})\right]\right)\tag{17} W1(pr,pθ)=K1fLKsup(Expr(x)[f(x)]Expθ(x)[f(x)])(17)

评价网络

当然公式 ( 17 ) (17) (17)也不好算,根据神经网络的通用近似定理,可以假设存在一个神经网络可以达到这个上界,令 f ( x ; ϕ ) f(\mathbf{x};\phi) f(x;ϕ)为一个神经网络,假设存在参数集合 Φ \Phi Φ,对所有 ϕ ∈ Φ \phi\in\Phi ϕΦ f ( x ; ϕ ) f(\mathbf{x};\phi) f(x;ϕ)满足 K \text{K} K- Lipschitz \text{Lipschitz} Lipschitz连续,那公式 ( 17 ) (17) (17)可以转换为
max ⁡ ϕ ∈ Φ ( E x ∼ p r ( x ) [ f ( x ; ϕ ) ] − E x ∼ p θ ( x ) [ f ( x ; ϕ ) ] ) (18) \max_{\phi\in\Phi}\left(\mathbb{E}_{\mathbf{x}\sim p_r(\mathbf{x})}\left[f(\mathbf{x};\phi)\right]-\mathbb{E}_{\mathbf{x}\sim p_{\theta}(\mathbf{x})}\left[f(\mathbf{x};\phi)\right]\right)\tag{18} ϕΦmax(Expr(x)[f(x;ϕ)]Expθ(x)[f(x;ϕ)])(18)
f ( x ; ϕ ) f(\mathbf{x};\phi) f(x;ϕ)称为评价网络,评价网络的最后一层为线性层,其输出不再是GAN中判别网络的 [ 0 , 1 ] [0,1] [0,1],值域没有限制。那么只需要找到一个网络使公式 ( 18 ) (18) (18)的值尽可能大,即 f ( x ; ϕ ) f(\mathbf{x};\phi) f(x;ϕ)对真实样本打分尽量高,对模型生成样本打分尽量低,

为使 f ( x ; ϕ ) f(\mathbf{x};\phi) f(x;ϕ)满足 K \text{K} K- Lipschitz \text{Lipschitz} Lipschitz连续,一种近似的方法是限制参数的取值范围,因为神经网络连续可导,满足 K \text{K} K- Lipschitz \text{Lipschitz} Lipschitz连续可以近似为其关于 x \mathbf{x} x的偏导数的模 ∣ ∣ ∂ f ( x ; ϕ ) ∂ x ∣ ∣ \vert\vert\frac{\partial f(\mathbf{x};\phi)}{\partial \mathbf{x}}\vert\vert xf(x;ϕ)小于某个上界,由于偏导数大小一般和参数的取值范围相关,可以通过限制 ϕ \phi ϕ的取值范围来近似,令 ϕ ∈ [ − c , c ] \phi\in\left[-c,c\right] ϕ[c,c] c c c是一个较小的正数。

生成网络

此时生成网络的目标就是使评价网络对生成样本的打分尽可能高
max ⁡ θ E z ∼ p ( z ) [ f ( G ( x ; θ ) ; ϕ ) ] (19) \max_{\theta}\mathbb{E}_{\mathbf{z}\sim p(\mathbf{z})}\left[f(G(\mathbf{x};\theta);\phi)\right]\tag{19} θmaxEzp(z)[f(G(x;θ);ϕ)](19)
因为 f ( x ; ϕ ) f(\mathbf{x};\phi) f(x;ϕ)是不饱和函数,所以生成网络的梯度不会消失,从而解决了GAN训练不稳定的问题,并且目标函数不再含有 KL \text{KL} KL散度,在一定程度上缓解了模型坍塌的问题。

开源代码

https://github.com/tjwei/GANotebooks

你可能感兴趣的:(深度学习,深度生成模型)