李宏毅學習筆記30.GAN.01.Introduction of Generative Adversarial Network

簡介

GAN系列好長,第一個視頻就90分鐘。。。
Introduction of Generative Adversarial Network (GAN)
這塊貌似也是新內容。開場就很搞笑的用谷歌來讀一下GAN,因爲按拼音讀就是:幹。
然後給出了力坤大佬給出的評價:
問:What are some recent and potentially upcoming breakthroughs in unsupervised learning?
力坤答:Adversarial training is the coolest thing since sliced bread.
I’ ve listed a bunch of relevant papers in a previous answer.
Expect more impressive results with this technique in the coming years.
What’s missing at the moment is a good understanding of it so we can make it work reliably. It’s very finicky. Sort of like ConvNet were in the 1990s, when I had the reputation of being the only person who could make them work(which wasn’t true).


sliced bread:美國人往往會用這麼一句俚語來形容極好極讚的事物:“This is the best thing since sliced bread!(這是自切片面包以來最棒的東西!)”之前是不切片的。。。一般用作對產品、商品、發明等的讚美語。這一誇,也讓人瞬間明白了切片面包的地位。


The most important one, in my opinion, is adversarial training (also called GAN for Generative Adversarial Networks). This is an idea that was originally proposed by Ian Goodfellow when he was a student with Yoshua Bengio at the University of Montreal(he since moved to Google Brain and recently to OpenAI).
This, and the variations that are now being proposed is the most interesting idea in the last 10 years in ML, in my opinion.
常見的GAN變種:
https://github.com/hindupuravinash/the-gan-zoo
在這裏插入圖片描述
變種太多,名字都不夠用,例如:
Mihaela Rosca, Balaji Lakshminarayanan, David Warde-Farley, Shakir Mohamed, “Variational Approaches for Auto-Encoding Generative Adversarial Networks”, arXiv, 2017
本來要叫AEGAN,但是發現有人已經用掉了,所以只能叫α-GAN。
本節GAN課程大綱:
Basic ldea of GAN
GAN as structured learning
Can Generator learn by itself?
Can Discriminator generate?
A little bit theory(未完。。。)
公式輸入請參考:在線Latex公式

Generation

GAN主要用於生成東西:生成圖片,生成句子等。
Image Generation
在這裏插入圖片描述
In a specific range,一般都是從一個分佈(高斯)隨機採樣一個向量,根據這個向量生成圖片。
Sentence Generation
在這裏插入圖片描述
We will control what to generate latter. → Conditional Generation(這塊前面有提到,後面有詳細講解)

Basic Idea of GAN

Generator

GAN裏面有兩個重要的東西,一個是Generator,Generator可以是一個NN。
在這裏插入圖片描述
輸入向量每一個維度都對應生成對象的某些特徵。Each dimension of input vector represents some characteristics.例如:
在這裏插入圖片描述
改第一個維度:
在這裏插入圖片描述
改倒數第二個維度:
在這裏插入圖片描述
改倒數第一個維度:
在這裏插入圖片描述

Discriminator

GAN裏面有兩個重要的東西,另一個是Discriminator,Discriminator也可以是一個NN。注意它的輸入和上面的Generator的不同。
在這裏插入圖片描述
例如:
逼真的二次元:
在這裏插入圖片描述
垃圾二次元:
在這裏插入圖片描述

二者的關係

這頁PPT在RL裏面貌似也有。。。
在這裏插入圖片描述
This is where the term “adversarial” comes from. You can explain the process in different ways…….
用模型來描述上面的關係:
在這裏插入圖片描述
GAN的原文中把Generator和Discriminator的關係描述爲假鈔製作者和警察,老師說這樣太暴力,會教壞小孩子,換個講法:
在這裏插入圖片描述
老師看過很多真實的二次元人物。
在這裏插入圖片描述
另外最後給出兩個問題。
Generator爲什麼不自己學,還需要Discriminator來指導?
Discriminator爲什麼不自己直接做?
二者的關係可以描述爲:寫作敵人,念做朋友:
左邊是棋魂,右邊是火影。他們都是對立又互相幫助的典型,促使對方進步。
在這裏插入圖片描述

Algorithm

語言描述版

先把算法流程描述一遍
• Initialize generator and discriminator.在這裏插入圖片描述
這兩個是NN,這裏的初始化就是初始化NN的參數。
• In each training iteration:
Step 1: Fix generator G, and update discriminator D.這裏的update就是調整參數。
這裏有兩組圖片,一組是數據庫中人工收集的真實的二次元圖像,另外一組是generator生成的圖像
在這裏插入圖片描述
Discriminator learns to assign high scores to real objects and low scores to generated objects.
Discriminator要學會分辨兩組圖像,如上圖,真實圖像得分與1越接近越好,生成圖像得分與0越接近越好。可以用迴歸或者分類的方式來做,都可以。
Step 2: Fix discriminator D, and update generator G
在這裏插入圖片描述
這裏generator 要調整參數,使得輸出分數越大越好。
在這裏插入圖片描述
在實際做的過程中,會把中間部分看做是一個大的網絡。例如generator 有5層,discriminator 有5層,我們會把兩個5層接起來,中間再加一個隱藏層(這個隱藏層的大小實際與圖片的維度大小一樣),變成11層,然後固定後面5層的參數,更新前面5層的參數。用GD來做更新就可以。
在這裏插入圖片描述

數學描述版

Initialize θd\theta_d for D and θg\theta_g for G
• In each training iteration:
• • Learning D
• • • Sample m examples {x1,x2,,xm}\{x^1,x^2,\cdots,x^m\} from database.這裏m是超參數
• • • Sample m noise samples {z1,z2,,zm}\{z^1,z^2,\cdots,z^m\} from a distribution.
• • • 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(xi))\tilde V=\cfrac{1}{m}\sum_{i=1}^mlogD(x^i)+\cfrac{1}{m}\sum_{i=1}^mlog(1-D(x^i))
上面的目標函數是原始論文中的公式,不是最好的方式,其中第一項目的是要把真實圖片得分求和取平均後最大化,第二項目的是要把生成圖像得分求和取平均後最小化。這裏生成圖像的得分會經過一個sigmoid函數,使得分數值域在[0,1]之間,然後1減去這個分數就不會是負數,取log不會出錯,然後整個第二項由於有一個1減去這個操作,就從最小變成求最大。整個式子就是求最大值。
θdθd+ηV~(θd)\theta_d\leftarrow\theta_d+\eta\triangledown\tilde V(\theta_d)
由於是求最大值,這裏更新參數是用+
• • Learning G
• • • Sample m noise samples {z1,z2,,zm}\{z^1,z^2,\cdots,z^m\} from a distribution.
• • • Update generator parameters θg\theta_g to maximize
V~=1mi=1mlogD(G(zi))\tilde V=\cfrac{1}{m}\sum_{i=1}^mlogD(G(z^i))
θgθg+ηV~(θg)\theta_g\leftarrow\theta_g+\eta\triangledown\tilde V(\theta_g)

例子:Anime Face Generation

https://zhuanlan.zhihu.com/p/24767059
沒想到老師也刷知乎。。。拿的例子也是二次元的例子。
100 updates
在這裏插入圖片描述
1000 updates
在這裏插入圖片描述
2000 updates
在這裏插入圖片描述
5000 updates
在這裏插入圖片描述
10,000 updates
在這裏插入圖片描述
20,000 updates
在這裏插入圖片描述
50,000 updates
在這裏插入圖片描述
另外一個結果(學生的作業,使用了自己收集的數據集)
在這裏插入圖片描述
人臉生成上的研究:
在這裏插入圖片描述
調整生成的向量,可以得到:
在這裏插入圖片描述

GAN as structured learning

Structured Learning

這個應該是去年的最後部分的內容,這裏稍微複習一下,普通的機器學習是找到一個函數,使得輸入得到對應的輸出
f:XYf:X\rightarrow Y
Regression: output a scalar
Classification: output a “class” (one-hot vector)
在這裏插入圖片描述
輸出的東西都比較簡單,如果要像輸出比較複雜的東西(序列,圖像等),就是Structured Learning
Structured Learning/Prediction: output a sequence, a matrix, a graph, a tree ……
Output is composed of components with dependency.
下面給幾個Structured Learning的例子:
Output Sequence:
在這裏插入圖片描述
Output Matrix:
在這裏插入圖片描述

Structured Learning面臨的挑戰

One-shot/Zero-shot Learning:(就是訓練數據中可能沒有或者只有很少的某個類別的數據,而要求模型生成對應的東西)
在傳統的分類問題中,每個分類都會有大量的樣本。In classification, each class has some examples.
在Structured Learning:
• If you consider each possible output as a “class” ……
• Since the output space is huge, most “classes” do not have any training data.
• Machine has to create new stuff during testing.
• Need more intelligence
另外,機器需要有大局觀。Machine has to learn to do planning
• Machine generates objects component-by-component, but it should have a big picture in its mind.
• Because the output components have dependency, they should be considered globally.
例如在圖像生成的時候,每次生成一個像素,但是所有像素合起來要像一個人臉。不能人臉有三個眼睛,兩個嘴巴,因此在Structured Learning中生成不是最重要的,重要的是component與component之間的關係。
生成數字,先生成一個點,這個時候並不能說這個點生成得好或者不好:
在這裏插入圖片描述
生成文本,要看整體效果,只看部分是不夠的:
在這裏插入圖片描述
那麼如何把GAN 和 structured learning聯繫起來?

Structured Learning Approach

我們可以把Generator看做Structured Learning中生成很小的component的部分
Discriminator看做整體的評估方法。
在這裏插入圖片描述
二者合起來,就是GAN.
接下來填前面挖到坑:
Generator爲什麼不自己學,還需要Discriminator來指導?
Discriminator爲什麼不自己直接做?

Can Generator learn by itself?

Generator

先來看下,如果不用GAN,怎麼來訓練Generator
在這裏插入圖片描述
之前說過,Generator就是一個NN,輸入一個向量,生成一個圖片,如果現在是做生成手寫數字,那麼就是輸入一個向量,得到一個手寫數字。輸入不同的向量,得到不同的數字。
如果我們用不同的向量來代表不同的手寫數字:
在這裏插入圖片描述
那麼就可以用監督學習的方法來進行訓練:
在這裏插入圖片描述
可以和NN分類器訓練過程一樣:
在這裏插入圖片描述
現在的問題是表示圖片的code從哪裏來?隨機?不行啊,因爲code要表示圖片的特徵,例如:
在這裏插入圖片描述
這裏的兩個code如果差很遠,你叫機器如何學習?可以看到這裏的第一維0.1可能表示數字1,然後第二維表示的是傾斜角度。
之前學過Encoder,可以把圖片壓縮爲一個向量:
在這裏插入圖片描述
回顧Auto-encoder結構:
在這裏插入圖片描述
中間的c就是圖片的向量表示,弄得詳細一點:
在這裏插入圖片描述
分析上面的結構,發現Auto-encoder的Decoder就是根據c(向量)生成圖片,這不就是和Generator乾的事情是一樣的嗎?
在這裏插入圖片描述
我們看能不能把這塊單獨拿出來,Randomly generate a vector as code,然後生成圖片。
在這裏插入圖片描述
通過隨機生成的二維向量,然後生成手寫數字圖片。
在這裏插入圖片描述
假設給定下圖中的兩個二維向量得到的結果分別是手寫數字0和1
在這裏插入圖片描述
那麼,隨着向量在座標軸上的變換,生成圖片如下圖所示:
在這裏插入圖片描述
通過上面的實驗,我們可以下如下結論:
如果有兩個向量a和b,那麼我們可以分別生成兩個手寫數字:
在這裏插入圖片描述
如果我的輸入是兩個向量的計算呢?
在這裏插入圖片描述
可以用之前的內容VAE
來進行解決:
在這裏插入圖片描述
就可以訓練VAE使得模型即使輸入的是它從來沒有看見過的向量,它也能正確的做出相應的輸出。
但是這個做法能和GAN的訓練出來的模型一樣嗎?

VAE vs GAN

在VAE中,我們的目標是模型G生成的圖片與真實圖片差距越小越好:
在這裏插入圖片描述
這個差距往往是通過兩個圖片逐像素進行比較得到的,例如上圖中的箭頭就是兩個圖片對應的像素,我們比較這些所有的像素,計算差距,換句話說就是用兩個圖片的向量,計算每個維度之間的距離來衡量差距。
當然如果生成的圖片如果和真實圖片一模一樣當然沒有問題,現在問題是生成的圖片總是會和真實圖片有一些差異,在計算差異的時候就會出現問題:
例如我們的真實圖片是:
在這裏插入圖片描述
生成圖片:
在這裏插入圖片描述
可以看到,雖然上面兩個按差異計算來說比下面的圖片的計算結果要好,但是從圖片整體來看,反而下面的圖片符合手寫圖片的規律。
也就是說模型的優化目標不是單純的讓你的生成結果與真實結果越接近越好。
而是要使得component與component之間的關係符合現實規律。例如:
在這裏插入圖片描述
左邊多了一個像素是不行的,如果在多出來的像素附近填滿像素當然是可以的。
但是這個事情在NN中很難做到:
在這裏插入圖片描述
例如上面的模型中Each neural in output layer corresponds to a pixel.
第二個神經元輸出有顏色的時候,那麼它會希望旁邊的神經元也生成顏色,這樣符合手寫數字的規律,但是NN中,每個神經元都是獨立的:
在這裏插入圖片描述
The relation between the components are critical.
Although highly correlated, they cannot influence each other.
Need deep structure to catch the relation between components.
我們需要structure 來約束這個事情。就是在後面在加幾個隱藏層,就可以調整第L層的神經元輸出。
也就是說理論上,VAE要想獲得GAN的效果,它的網絡要比GAN要深才行。
而且由於VAE算法採用的分佈採樣,因此做一些離得比較散的目標效果不好:
下圖中綠色是目標,藍色是VAE學習的結果
在這裏插入圖片描述

Can Discriminator generate?

Discriminator 在不同文獻中有不同的名字:Evaluation function, Potential Function, Energy Function …
Discriminator is a function D (network, can deep)
D:XRD:X\rightarrow R
• Input x: an object x (e.g. an image)
• Output D(x): scalar which represents how “good” an object x is
在這裏插入圖片描述
Can we use the discriminator to generate objects?
回答:當然可以。
但是用Discriminator來生成對象和用Generator來生成對象不太一樣,後者是一個一個component來生成,每個component之間的關係不好控制,而Discriminator由於是top-down evaluation,所以很容易控制component之間的關係。
• It is easier to catch the relation between the components by top-down evaluation.
也就是說Discriminator很容易分辨下面兩張圖片左邊不行,右邊ok。
在這裏插入圖片描述
因此檢查component之間有沒有isolate的component很容易,只需要用CNN的filter掃描一遍就知道了。
在這裏插入圖片描述
下面來看生成過程。假設:
• Suppose we already have a good discriminator D(x) …
discriminator 可以判斷對象是否足夠好,生成對象x~\tilde x的時候只需要窮舉所有可能的x~\tilde x,再挑一個使得discriminator 得分最好的那個即可:
x~=argmaxxXD(x)(1)\tilde x=\text{arg}\underset{x\in X}{\text{max}}D(x)\tag1
Enumerate all possible x!這個事情可以嗎?

Discriminator - Training

現在我們手上只有真實的對象,因此用這個東西來訓練,得到的Discriminator會比較笨,看什麼都是真實的。
在這裏插入圖片描述
Discriminator only learns to output “1” (real).
Discriminator training needs some negative examples. Negative examples are critical.
How to generate realistic negative examples?
普通的可以用加噪音來實現:
在這裏插入圖片描述
但是一些逼真的生成對象不好弄,例如:
在這裏插入圖片描述
下面是正確的訓練方法:
Given a set of positive examples, randomly generate a set of negative examples.
在這裏插入圖片描述
在這裏插入圖片描述
• In each iteration
• • Learn a discriminator D that can discriminate positive and negative examples.
在這裏插入圖片描述
• • Generate negative examples by discriminator D(假設公式(1)已知)
在這裏插入圖片描述
• • Learn a discriminator D that can discriminate positive and negative examples.
在這裏插入圖片描述
• • Generate negative examples by discriminator D(假設公式(1)已知)
在這裏插入圖片描述
不斷反覆。
從可視化的角度來看,
在這裏插入圖片描述
輸入一個對象x(例如圖像),輸出一個向量,用於評判x到底好還是不好,好就是真實對象,不好就是生成對象。
爲了簡單,我們把輸出D(x)看成一維的。那麼紅色的分佈曲線就是D(x)。這裏我們希望對於真實對象(綠色)的得分集中在凸起部分,生成對象在其他部分。

在這裏插入圖片描述
但是由於D(x)一般是高維的,那麼很難做到某個地方就是這麼簡單按取值來判定,高維空間的分佈非常複雜,不能把沒有真實對象出現的地方就弄成低分。
實作的時候是這樣:
綠色代表真實對象;藍色代表生成對象。
剛開始二者的分佈如下圖:
在這裏插入圖片描述
然後學習出來的discriminator分佈:
在這裏插入圖片描述
可以看到,剛開始discriminator在沒有分佈的地方(右邊)不知道要打什麼分數,所以出現那個地方打分比實際對象分數要高。
然後用discriminator重新產生negative example:
在這裏插入圖片描述
然後再次學習出來的discriminator分佈:
在這裏插入圖片描述
看到它修復了它自己的漏洞。右邊是低分了。
再次迭代後:
在這裏插入圖片描述
最後希望訓練到這樣停止迭代:
在這裏插入圖片描述
其實用discriminator來生成對象就是GAN中的一種生成方法,記得在RL中裏面有提到過,critical policy的生成方法。
最後貼一張圖(Structured Learning的分支),圖中的Graphical Model實際上就是相當於discriminator,整個訓練也是迭代方式進行的(EM是這個?)
在這裏插入圖片描述

小結:Generator v.s. Discriminator

先把Generator v.s. Discriminator的優缺點對比一下

Generator Discriminator
Pros:
• Easy to generate even with deep model
Pros:
•Considering the big picture
Cons:
• Imitate the appearance
• Hard to learn the correlation between components
Cons:
• Generation is not always feasible
••Especially when your model is deep(就是那個argmax函數不好解)
• How to do negative sampling?

現在在回過頭來看Generator + Discriminator的GAN的算法(在前面有描述):
實際上是Generator在模擬生成對象,使得Discriminator給出高分。

Benefit of GAN

• From Discriminator’s point of view
•• Using generator to generate negative samples
在這裏插入圖片描述
• From Generator’s point of view
•• Still generate the object component-bycomponent
•• But it is learned from the discriminator with global view. (跟discriminator 學習大局觀)
之前VAE做下面的結果不好,現在用GAN來做效果不錯。紅色是generator生成的結果,藍色是真實對象。
在這裏插入圖片描述
VAE生成的人臉比較模糊,GAN的比較清晰。
在這裏插入圖片描述
某篇論文中不同GAN和VAE的對比。從圖中我們看到,實驗數據集分別是MNIST和CIFAR10,然後評價指標是FID,這個指標具體後面還會講,這裏只要知道越小越好,結果顯示所有的GAN其實都差不多,都有很大的範圍,因爲GAN的算法對於參數非常敏感,很容易產生很大的偏差,但是總體而言,GAN對比VAE,VAE的variance比較小,GAN比VAE的效果(應該是看星號位置)要好。

在這裏插入圖片描述
後面還有部分內容,時間不夠了。貼點相關視頻:
https://www.youtube.com/watch?v=KSN4QYgAtao&feature=youtu.be
https://www.youtube.com/watch?v=0CKeqXl5IY0&feature=youtu.be

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