李宏毅學習筆記35.GAN.06.Tips for Improving GAN

簡介

Martin Arjovsky, Soumith Chintala, Leon Bottou, Wasserstein GAN, arXiv prepring, 2017
Ishaan Gulrajani, Faruk Ahmed,Martin Arjovsky, Vincent Dumoulin, Aaron Courville, “Improved Training of Wasserstein GANs”,arXiv prepring,2017
這節講GAN的優化。
公式輸入請參考:在線Latex公式

JS divergence來衡量分佈的問題

JS divergence is not suitable. 原因在於:
In most cases, PGP_G and PdataP_{data} are not overlapped.
就是生成的數據和真實數據是不重疊的。不重疊有兩方面的原因:
1、數據本身的問題The nature of data
Both PdataP_{data} and PGP_G are low-dim manifold in high-dim space.The overlap can be ignored.
例如:圖片是高(三)維空間中的低(二)維空間的manifold。
如下圖所示,可以看到兩個曲線overlap的部分是很小的。
在這裏插入圖片描述
2、採樣Sampling的原因
儘管數據本來是有overlap,但是我們不是取所有的數據:
Even though PdataP_{data} and PGP_G have overlap.
在這裏插入圖片描述
我們是對這兩堆分佈進行採樣,我們不會採樣全部,而是部分:
在這裏插入圖片描述
以上兩個分佈採樣的結果是不會有overlap的,除非你採樣超多點,因此這兩個採樣結果可以看做是兩個不同的分佈:
在這裏插入圖片描述
也就是說If you do not have enough sampling ……結果就是沒有重合。
沒有重合的時候,用JS divergence來衡量分佈,會發生什麼?

What is the problem of JS divergence?

只要兩個分佈不重合,那麼算出來的結果都一樣:log2log2
JS divergence is log2log2 if two distributions do not overlap
在這裏插入圖片描述
按理說PG1P_{G_1}要比PG0P_{G_0}好,因爲它比PG0P_{G_0}要更加接近PdataP_{data},JS divergence都一樣,除非二者重合,兩個纔會有JS divergence=0。這樣在不重合的狀態下是沒有辦法做優化(train)的。
結果爲什麼會是:log2log2
Intuition: If two distributions do not overlap, binary classifier achieves 100% accuracy.
用一個二分類的分類器對不重疊的兩個分佈做分類,總是可以得到100%的準確率,因此其cost都是一樣的。
Same objective value is obtained. →Same divergence.
因此在GAN中用二分類來訓練是很難收斂的。例如:
用綠色點代表真實數據
藍色點代表生成數據
如果是一個sigmoid函數的話,可以看到藍色點這塊的梯度都是0,所以在GD的時候是不會更新(動)的。所以有人提出說,不要把這個分類函數訓練得太好,使得整個分類函數在藍色點部分有梯度,這樣纔可以進行梯度更新,但是這個訓練得好不好(太用力,沒梯度,不夠用力,discriminator無法工作),很難把握。
在這裏插入圖片描述
因此爲了做GAN的優化,出現了LSGAN

Least Square GAN (LSGAN)

算法思想就是用線性分類器替換sigmoid分類器。也就是把分類問題換成了迴歸問題。
Replace sigmoid with linear (replace classification with regression)
在這裏插入圖片描述
train的目標是使得真實數據越接近1越好,生成數據越接近0越好。

Wasserstein GAN (WGAN): Earth Mover’s Distance

Wasserstein中a發[æ]的音,思想就是不用JS divergence來衡量兩個分佈的差別,而是用另外一個方法:Earth Mover’s Distance。這個玩意的概念很土。。。
把P和Q看作是兩堆土,而你是藍翔畢業的挖掘機(Earth Mover)司機,要把P這堆土(原文是挖到Q那裏,但是應該不是這個意思)挖成Q的形狀,而Earth Mover’s Distance就是挖掘機來回移動的平均距離。
• Considering one distribution P as a pile of earth, and another distribution Q as the target
• The average distance the earth mover has to move the earth.
在這裏插入圖片描述
上面的分佈是簡化了的,土堆應該是這樣:
在這裏插入圖片描述
要把P變成Q,可以有很多種挖法:
在這裏插入圖片描述
上圖中的左邊是是把鄰近的土進行移動,右邊是比較遠的土進行移動。我們一般用左邊的那種。
There many possible “moving plans”.
Using the “moving plan” with the smallest average distance to define the earth mover’s distance.
Best “moving plans” of this example
在這裏插入圖片描述
正式的定義:
A “moving plan” is a matrix. The value of the element is the amount of earth from one position to another.
Average distance of a plan γ\gamma:
B(γ)=xp,xqγ(xp,xq)xpxqB(\gamma)=\sum_{x_p,x_q}\gamma(x_p,x_q)||x_p-x_q||
Earth Mover’s Distance:
W(P,Q)=minγB(γ)W(P,Q)=\underset{\gamma\in\prod}{min}B(\gamma)
這個矩陣中某個點(x,y)代表從行x移動多少土到列y,這個點的顏色越亮,那麼代表移動的土越多。
行x所有點加起來,對應P中對應第x堆土;
列y所有點加起來,對應Q中對應第y堆土。
給定矩陣(確定移動方案後)γ\gamma,計算移動距離B(γ)B(\gamma),如上面公式所示,然後窮舉所有移動方案,找到最小那個。
在這裏插入圖片描述
也就是說這個方法要接一個優化方案後才能得到解。

Why Earth Mover’s Distance?

用Earth Mover’s Distance來替換JS divergence
在這裏插入圖片描述
那麼也就改善了之前兩個分佈不重疊無法更新梯度的缺點:
在這裏插入圖片描述
可以看到d50比d0是有進步的,所以梯度才能不斷更新迭代,最後到達兩個分佈距離爲0的目標。

WGAN

如何用wasserstein distance來衡量兩個分佈,這個過程的證明比較複雜,直接給結論:
Evaluate wasserstein distance between PdataP_{data} and PGP_G
V(G,D)=maxD1Lipschitz{ExPdata[D(x)]ExPG[D(x)]}V(G,D)=\underset{D\in 1-Lipschitz}{max}\{E_{x\sim P_{data}}[D(x)]-E_{x\sim P_{G}}[D(x)]\}
公式的意思是:如果x是從PdataP_{data} 採樣出來的,希望他的Discriminator值越大越好,如果是從PGP_G採樣出來的,希望他的Discriminator值越小越好,另外還有一個約束就是D要是一個1Lipschitz1-Lipschitz的函數(啥意思後面講,就是要D越平滑越好)
爲什麼要平滑呢,如果沒有平滑的這個限制:
在這裏插入圖片描述
D就會在生成數據PdataP_{data}的地方趨向於負無窮大,在真實數據PGP_G的地方趨向於正無窮大。兩個分佈就會差很多。加入平滑限制,會使得D不會無限的上升和下降,會停在某個地方。


Lipschitz FunctionLipschitz\space Function
f(x1)f(x2)Kx1x2||f(x_1)-f(x_2)||\leq K||x_1-x_2||
可以看到公式左邊是輸出的變化,右邊是輸入的變化,也就是說輸出的變化要小於K倍的輸入的變化。
當K=1,我們就把這個滿足這個不等式的函數稱爲1Lipschitz1-Lipschitz
也就是
f(x1)f(x2)x1x2||f(x_1)-f(x_2)||\leq ||x_1-x_2||
也就是不會變化很快。例如下面的綠色函數比較像1Lipschitz Function1-Lipschitz\space Function,藍色函數就肯定不是1Lipschitz Function1-Lipschitz\space Function
在這裏插入圖片描述


如何滿足1Lipschitz Function1-Lipschitz\space Function約束條件呢?原論文使用的方法是:Weight Clipping
Force the parameters ww between cc and c-c. After parameter update,
if w > c, w = c;
if w < -c, w = -c
這個方法很簡單,其實用這個方法弄出來的函數並不滿足1Lipschitz Function1-Lipschitz\space Function約束條件,但是基本能work,能達到使得D平滑的目的。也是沒有辦法的辦法,因爲1Lipschitz Function1-Lipschitz\space Function不好優化。

Improved WGAN (WGAN-GP)

Weight Clipping進行改進,函數還是一樣:
V(G,D)=maxD1Lipschitz{ExPdata[D(x)]ExPG[D(x)]}V(G,D)=\underset{D\in 1-Lipschitz}{max}\{E_{x\sim P_{data}}[D(x)]-E_{x\sim P_{G}}[D(x)]\}
但是Improved WGAN對於約束換了一個角度,就是梯度的norm要小於等於1。
A differentiable function is 1-Lipschitz if and only if it has gradients with norm less than or equal to 1 everywhere.
這個轉換和之前的約束是一樣的。
在這裏插入圖片描述
關於norm的計算是有一個近似計算的方法的:
V(G,D)maxD{ExPdata[D(x)]ExPG[D(x)]λxmax(0,xD(x)1)dx}V(G,D)\approx\underset{D}{max}\{E_{x\sim P_{data}}[D(x)]-E_{x\sim P_{G}}[D(x)]-\lambda\int_xmax(0,||\triangledown _xD(x)||-1)dx\}
後面這個積分項類似於正則項,它的作用是對所有的x做積分,然後取一個max,這個max的意思當Discriminator的梯度的norm大於1,那麼就會存在正則項,如果Discriminator的梯度的norm小於1,那麼這項爲0,沒有正則項(不懲罰)。但是這樣會有問題,我們不可能對所有高維空間中的x都進行求積分這個操作,我們的x是sample出來的。因此再次把正則項進行近似:
λExPpenalty[max(0,xD(x)1)]-\lambda E_{x\sim P_{penalty}}[max(0,||\triangledown _xD(x)||-1)]
這個正則項保證所有采樣出來的x滿足Discriminator的梯度的norm小於1
把這個懲罰項拿出來,做一個可視化,實際上這個penalty項就是從 PdataP_{data} 中隨便取一點,然後從PGP_G中隨便取一點,然後在這兩點的連線上進行採樣,得到xpenaltyx\sim penalty
在這裏插入圖片描述
把這些sample到的xpenaltyx\sim penalty範圍畫出來就是上面的藍色部分,爲什麼不是對整個空間中的x都做penalty呢?原文說實驗結果表明這樣做結果比較好。。。
“Given that enforcing the Lipschitz constraint everywhere is intractable, enforcing it only along these straight lines seems sufficient and experimentally results in good performance.”
從另外一個方面來看,PGP_G要沿着梯度的方向向 PdataP_{data} 靠近,靠近移動的方向就是藍色區域,其他區域也不會去,所以這樣解釋也可以。
Only give gradient constraint to the region between PdataP_{data} and PGP_G because they influence how PGP_G moves to PdataP_{data}.
再來一個trick,之前說近似後的約束是希望梯度大於1就會有懲罰,小於1不會有懲罰(max(0,xD(x)1)max(0,||\triangledown _xD(x)||-1)這裏。)但是在實作的時候,用的正則項爲:(xD(x)1)2(||\triangledown _xD(x)||-1)^2,意思是希望梯度越接近1越好。原文:
“Simply penalizing overly large gradients also works in theory, but experimentally we found that this approach converged faster and to better optima.”
理由就是實作上效果好。。。
當然這個方法也有缺點,它的penalty的點是從兩個分佈中隨機選點然後連接,然後做採樣,如果有下圖的兩個分佈明顯這樣做有問題:
在這裏插入圖片描述
選擇紅色那個點是不好的,(因爲黃色的點移動也是移動到黑色點那個位置,也就是以黑色點爲目標,而不是以紅色點爲目標。)應該選下圖中黑色的點的連線來做採樣比較合適。但是找黑色的點又比較麻煩。。。
在這裏插入圖片描述
後來又研究者對Improved WGAN提出Improved Improved WGAN算法,改進的地方在於把penalty放在了PdataP_{data}的範圍。

Spectrum Norm

上面講的WGAN比較弱,一來都是用近似的方法搞的,解釋不通就說反正實作就是這樣;二來只有在某個區域Discriminator的norm纔會滿足小於1的條件。Spectrum Norm就直接,所有範圍的x經過Discriminator後的norm都會滿足小於1的條件。(不展開)
Spectral Normalization → Keep gradient norm smaller than 1 everywhere [Miyato, et al., ICLR, 2018]
下面是生成狗狗的DEMO,原文是動圖。。。
在這裏插入圖片描述

GAN to WGAN(如何將GAN的算法改爲WGAN的算法)

Algorithm of GAN(Review)

Initialize θd\theta_d for DD and θg\theta_g for GG.
• In each training iteration:


這塊分割線內是訓練Discriminator,重複k次,這裏一定要訓練到收斂爲止,目的是找到maxDV(G,D)\underset{D}{\text{max}}V(G,D)(實作的時候一般沒有辦法真的訓練到收斂或者卡在局部最小點,因此這裏找到的是maxDV(G,D)\underset{D}{\text{max}}V(G,D)的lower bound)。
••Sample m examples {x1,x2,,xm}\{x^1,x^2,\cdots,x^m\} from data distribution Pdata(x)P_{data}(x).(找到真實對象)
••Sample m noise examples {z1,z2,,zm}\{z^1,z^2,\cdots,z^m\} from the prior Pprior(z)P_{prior}(z).(這個先驗分佈種類不是很重要)
•••Obtaining generated data {x~1,x~2,,x~m},x~i=G(zi)\{\tilde x^1,\tilde x^2,\cdots,\tilde x^m\},\tilde x^i=G(z^i).(找到生成對象)
•• Update discriminator parameters θd\theta_d to maximize
V~=1mi=1mlogD(xi)+1mi=1mlog(1D(x~i))(1)\tilde V=\cfrac{1}{m}\sum_{i=1}^mlogD(x^i)+\cfrac{1}{m}\sum_{i=1}^mlog(1-D(\tilde x^i))\tag1
θdθd+ηV~(θd)\theta_d\leftarrow\theta_d+\eta\triangledown\tilde V(\theta_d)



這塊分割線內是訓練Generator,重複1次,目的是減少JSD
••Sample another m noise samples {z1,z2,,zm}\{z^1,z^2,\cdots,z^m\} from the prior Pprior(z)P_{prior}(z)
••Update generator parameters θg\theta_g to minimize
V~=1mi=1mlogD(xi)+1mi=1mlog(1D(G(zi)))θgθgηV~(θg)\tilde V=\cfrac{1}{m}\sum_{i=1}^mlogD(x^i)+\cfrac{1}{m}\sum_{i=1}^mlog(1-D(G(z^i)))\\ \theta_g\leftarrow\theta_g-\eta\triangledown\tilde V(\theta_g)
由於1mi=1mlogD(xi)\cfrac{1}{m}\sum_{i=1}^mlogD(x^i)GG函數無關,所以在求最小值的時候可以忽略:
V~=1mi=1mlog(1D(G(zi)))(2)\tilde V=\cfrac{1}{m}\sum_{i=1}^mlog(1-D(G(z^i)))\tag2
θgθgηV~(θg)\theta_g\leftarrow\theta_g-\eta\triangledown\tilde V(\theta_g)


Algorithm of WGAN

把原始GAN算法中的公式1和公式2進行修改,具體如下:
對於公式1(訓練discriminator),實際上就是把sigmoid函數去掉,變成:
V~=1mi=1mD(xi)1mi=1mD(x~i)(3)\tilde V=\cfrac{1}{m}\sum_{i=1}^mD(x^i)-\cfrac{1}{m}\sum_{i=1}^mD(\tilde x^i)\tag3
當然在訓練discriminator的時候要注意使用Weight clipping /Gradient Penalty …等技巧,否則很難收斂。
對於公式2(訓練generator),改爲:
V~=1mi=1mD(G(zi))\tilde V=-\cfrac{1}{m}\sum_{i=1}^mD(G(z^i))

Energy-based GAN (EBGAN)

Junbo Zhao, et al., arXiv, 2016
這個算法還有一個變形,不展開,大概看看這個算法。
大概思想就是Generator不變,用autoencoder來做Discriminator。
Using an autoencoder as discriminator D.
計算過程如下圖所示:
在這裏插入圖片描述
生成的圖片進入粉色部分(Discriminator),先經過一個Autoencoder,還原後,計算出還原圖像和原圖的reconstruction error(上圖中是0.1),然後乘上一個 -1,得到Discriminator的輸出(上例中是-0.1)
所以從整體上來看Discriminator和之前的GAN的Discriminator一樣,輸入一個對象,得到這個對象和真實對象的差距,只不過是得到這個差距的方法不一樣,之前是JS divergence,這裏是Autoencoder。簡單來說就是根據一個圖片是否能夠被reconstruction,如果能被還原得很好,說明這個圖片是一個high quality的圖片,反之亦然。
這個方法的好處就是Autoencoder是可以pretrain的,不需要negative example來訓練,直接給它positive example來minimize reconstruction error即可。
➢Using the negative reconstruction error of auto-encoder to determine the goodness.
➢Benefit: The auto-encoder can be pre-train by real images without generator.
這樣還有一個好處,原來的GAN剛開始訓練的時候generator和discriminator都很弱,要不斷迭代後discriminator才隨着generator的變強而變強,這個方法discriminator不依賴generator,直接開局就很強。
EBGAN在訓練的時候有一個trick,如果只是希望生成圖片(藍色)的分數,即reconstruction error越大越好(取負號後變小),那麼會讓autoencoder訓練出來直接輸出noise,因爲Hard to reconstruct, easy to destroy,要得到低分很簡單,只要輸出noise,就會得到reconstruction error超級大(取負號後變小),這樣訓練出來的discriminator不是我們想要的。

在這裏插入圖片描述
因此我們會在訓練的過程中爲reconstruction error(取負號後)添加一個margin下限(超參數),讓reconstruction error(取負號後)小到一定程度即可。
在這裏插入圖片描述

Outlook: Loss-sensitive GAN (LSGAN)

這個GAN也用到了margin的概念,之前的WGAN,Discriminator是希望真實數據得分越大越好,生成數據得分越小越好
在這裏插入圖片描述
但是有些生成數據已經比較真實了,沒有必要要搞得很小。例如:下圖中的xx''比較接近真實數據xx,即Δ(x,x)\Delta(x,x'')比較小,xx'沒有那麼接近真實數據xx,即Δ(x,x)\Delta(x,x')比較大,可以看到Δ(x,x)\Delta(x,x')的margin壓得比較小,而Δ(x,x)\Delta(x,x'')的margin比較大。
在這裏插入圖片描述

Reference

• Ian J. Goodfellow, Jean Pouget-Abadie, Mehdi Mirza, Bing Xu, David WardeFarley, Sherjil Ozair, Aaron Courville, Yoshua Bengio, Generative Adversarial Networks, NIPS, 2014
• Sebastian Nowozin, Botond Cseke, Ryota Tomioka, “f-GAN: Training Generative Neural Samplers using Variational Divergence Minimization”, NIPS, 2016
• Martin Arjovsky, Soumith Chintala, Léon Bottou, Wasserstein GAN, arXiv, 2017
• Ishaan Gulrajani, Faruk Ahmed, Martin Arjovsky, Vincent Dumoulin, Aaron Courville, Improved Training of Wasserstein GANs, NIPS, 2017
• Junbo Zhao, Michael Mathieu, Yann LeCun, Energy-based Generative Adversarial Network, arXiv, 2016
• Mario Lucic, Karol Kurach, Marcin Michalski, Sylvain Gelly, Olivier Bousquet, “Are GANs Created Equal? A Large-Scale Study”, arXiv, 2017
• Tim Salimans, Ian Goodfellow, Wojciech Zaremba, Vicki Cheung, Alec Radford, Xi Chen Improved Techniques for Training GANs, NIPS, 2016
• Martin Heusel, Hubert Ramsauer, Thomas Unterthiner, Bernhard Nessler, Sepp Hochreiter, GANs Trained by a Two Time-Scale Update Rule Converge to a Local Nash Equilibrium, NIPS, 2017
• Naveen Kodali, Jacob Abernethy, James Hays, Zsolt Kira, “On Convergence and Stability of GANs”, arXiv, 2017
• Xiang Wei, Boqing Gong, Zixia Liu, Wei Lu, Liqiang Wang, Improving the Improved Training of Wasserstein GANs: A Consistency Term and Its Dual Effect, ICLR, 2018
• Takeru Miyato, Toshiki Kataoka, Masanori Koyama, Yuichi Yoshida, Spectral Normalization for Generative Adversarial Networks, ICLR, 2018

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章