데이터와 AI / NOTE 30

생성적 적대 신경망

Generative Adversarial Networks

GAN 학습, 변형 모델과 손실함수에 관한 원문 노트입니다.

♫ 이 문서 듣기

개념에서 수식으로

먼저 이해할 내용

GAN은 생성기와 판별기가 서로 다른 목표를 가지고 함께 학습하는 구조이다. 생성기는 데이터를 만들고, 판별기는 실제 데이터와 생성 데이터를 구별한다. 이 글은 두 목표의 관계와 각 확장 모델이 추가하는 조건을 차례로 다룬다.

기호를 먼저 읽기

G(z;θ)
잡음에서 데이터를 만드는 생성기
D(x;φ)
입력이 실제 데이터일 가능성을 판단하는 함수
p_d, p_g
실제 데이터와 생성 데이터의 분포
z, c, y
잡음·구조화된 잠재코드·외부 조건
E[·]
분포에 따른 평균
min_G max_D
서로 다른 방향의 두 최적화 목표

이 글의 흐름

각 모델의 입력과 출력을 구분한 뒤 판별기와 생성기의 목적함수를 읽는다. 이어서 학습 불안정성, 특징 정합, 정보 제약, 조건부 생성, 순환 일관성을 비교한다.

주제와 표기

GAN (Generative Adversarial Network)

Adversarial training

만드는 역할과 구별하는 역할을 분리한다

G는 낮은 차원의 잡음 z를 데이터 공간으로 옮기고, D는 입력이 실제 데이터인지 판단하는 점수를 만든다. 원문에 손실함수를 우회한다는 표현이 있어도, 뒤에는 명시적인 목적함수가 제시된다. 따라서 아무 학습 기준도 없다는 뜻으로 읽지 않는다.

A set of machines learn togeter by pursuing competing goals
A fascinating new training method
Bypasses the need of loss functions in learning
A new way of regularizing learning machines

Generative Adversarial Network

Generator, G[z;θ]:RKRD

Caputres the data distribution
Counterfeiters : Tries to fake discriminator

Discirminator, D[x;φ]:RD→{0,1}

Scoring function : D[x;ϕ] = P[y=1x] (y = 1 : trining data)
Learns features with rich semantics
Police : Tries to detect counterfeit images
원본 도해
Generator (Decoder)
random noise
z ~ Gaussian or uniform
znGϕ[z]x^nDϕ[x]

Training GAN

판별기는 두 종류의 입력을 모두 구별해야 한다

실제 데이터에서는 D(x)가 커지도록, 생성 데이터에서는 1−D(G(z))가 커지도록 로그 항을 더한다. 생성기의 출력에서 평균을 구하는 표현과 잡음 z에서 생성 경로를 따라 평균을 구하는 표현을 나란히 읽는다.

Denote by pd[x] the true data distribution and by pg[x] our model distribution (generator ' s distribution over x)

Training D

Train the discriminator D[x;ϕ] to maximize the probability of assigning the correct label to training data as well as fake data generated by G
maxD[Expd[x][log[D[x;ϕ]]]+Expg[x][log[1D[x;ϕ]]]]
=maxD[Expd[x][log[D[x;ϕ]]]+Ezp[z][log[1D[G[z;θ]]]]]

Training G

생성기와 판별기는 목표의 방향이 다르다

생성기는 판별기가 생성 데이터를 가짜로 분류하기 어렵게 만드는 쪽으로 움직인다. min_G max_D는 한 식을 두 매개변수 집합이 반대 방향으로 다룬다는 뜻이다. 원문은 두 모델을 번갈아 갱신하는 절차와 다른 생성기 목표식도 구분해 제시한다.

Train the generator G to minimize the probability of the negative (generated - data) class, Log[1D[G[z;θ]]]
minG[Ezp[z][log[1D[G[z;θ]]]]]

Two-player minimax game (for Nash equilibrium)

minθmaxϕJ[θ,ϕ]
where
J[θ,ϕ]=Expd[x][log[D[x;ϕ]]]+Ezp[z][log[1D[G[z;θ]]]]
Bath G and D are deep neural networks
Does not require any sophisticated inference methods (varaitional or sampling)
Alternate between k steps of optimizing D (cross - entropy loss minimization) and one step of optimizing G
maxDExq[x][log[D[x;ϕ]]]+Ezp[z][log[1D[G[z;θ]]]]
minGEzp[z][log[1D[G[z;θ]]]]
In practice, train G :
maxGEzp[z][log[D[G[z;θ]]]]
(stronger gradients early in learning)

Unrolled GANs

학습 안정성과 출력 크기는 별도의 문제이다

Unrolled GAN은 학습 경로를, DCGAN과 Progressive Growing은 생성 구조와 크기에 관련된 사례이다. 특정 모델 이름만으로 다양한 샘플 생성이나 안정 수렴이 무조건 보장되는 것은 아니다. 입력 잡음의 차원과 출력 영상의 크기를 따로 확인한다.

Stabilizes training of GANs and solves mode collapsing problem
Increases the diversity and coverage of the data distribution by the generator
Unrolled optimization for updating generator parameters

Generating Images by GANs

DCGAN

Generating an image of size 64×64 using 100 - dimensional random noise vector .
need to make sure how those sizes are obtained !
수식

Progressive Growing of GANS

Generating images of size 1024×1024 is a challenging task

Interesting Applications of GANs

GAN for single Image Super-Resolution

Goal : Estimate a high - resolution, superresolved image from a low - resolution input image .

eCommerce GAN

Improved Techniques for Training GANs

Convergent Issue in GAN

Training GANs

한쪽의 개선이 다른 쪽의 기준을 바꾼다

판별기가 바뀌면 생성기가 최적화할 대상도 달라진다. 따라서 일반적인 하나의 고정 손실을 줄이는 문제와 다른 상호작용이 있다. 원문은 진동, 다양성 감소, 약한 기울기를 서로 다른 학습 문제로 나누어 설명한다.

= Finding a Nash equilibrium of a non - convex game with continuous and high - dimensional parameters
Note that the modification of parameters in D increase
LD=Expd[x][log[D[x;ϕ]]]+Ezp[z][log[1D[G[z;θ]]]]
but G is modified to decrease
JG=Ezp[z][log[1D[G[z;θ]]]]
Thus, gradient methods may fail to converge for many games

Problems in GAN Training

Non-Convergence

Model parameters oscillate, destabilize and never converge

Mode collapsing

The generator may collapse, producing limited varieties of samples

Diminished gradient

The discriminator gets too successful that the generator gradient vanishes and learns nothing

Feature Matching to Train G

최종 판별 점수 대신 중간 특징을 맞춘다

Feature Matching은 실제 데이터와 생성 데이터의 특징 평균 차이를 줄인다. 이는 샘플의 각 픽셀을 일대일로 맞추는 식이 아니라 특징 공간의 통계를 맞추는 식이다. 어느 층의 φ를 사용하는지가 비교 대상의 의미를 정한다.

Rather than directly optimizing the out of the discriminator
minGEzp[z][log[1D[G[z;θ]]]]
train the generator to match the expected value of the features ϕ[]
on a intermediate layer of the discriminator
minG|Expdata[x][ϕ[x]]Ezp[z][ϕ[G[z;θ]]]|22

Denoising Auto-Encoder

특징 복원 기준과 적대적 기준을 함께 사용한다

DAE는 손상된 특징을 복원하는 역할이고, GAN 항은 생성 데이터가 판별기를 통과하도록 하는 역할이다. λ_dae와 λ_gan은 두 기준의 비중을 나타낸다. DAE를 학습하는 단계와 생성기를 갱신하며 DAE를 참조하는 단계를 구별한다.

원본 도해
minE[|xx^|22]

GAN Trained with Denoising Feature Matching

Training G

= Denoising autoencoder (in the space of discriminator features) + adversarial discriminator
The discriminator D = d◦ϕ[d[]:RD{0,1}isaclassifierandϕ[]:RDKisafeatureextracotr] is trained as in the standard GAN
maxDExPdata[x][log[D[x;ϕ]]]+Ezp[z][log[1D[G[z;θ]]]]
The generator G is trained
minGEzp[z][λdae|ϕ[G[z;θ]]DAE[ϕ[G[z;θ]]]|2]λgan[log[D[G[z;θ]]]]
where DAE[] is treated as constant w . r . t . gradient computations, which is trained by
minDAEExpdata[x][|ϕ[x]DAE[η[ϕ[x]]]|2]
η[] is the corruption function

An Information-Theoretic Extension of GAN

잠재코드의 일부에 의미를 남기려 한다

일반 잡음 z와 구조화된 코드 c를 나누면 생성 결과에서 c의 정보를 읽어낼 수 있도록 제약할 수 있다. InfoGAN의 상호정보량 항은 이 정보가 결과에 남도록 하는 목적이다. 잠재축마다 특정 실제 속성이 자동으로 확정된다고 보지는 않는다.

Toward Disentangled Representation

Disentangled Representation

Problems with GANs : No restrictions on how the generator G[z] uses z
z can be used in highly entangled way
Each dimension of z does not represent the salient attributes of a data instance

Disentangled = Interpretable and Factorized

Information Maximization

InfoGAN

Decompose the input noise vector into two parts (structured noise vector)
z : treated as source of incompressible noise
c : latent code which will target the salient features of the data distribution

정보량을 키우는 항의 부호를 확인한다

I(c;G(z,c))를 키우려면 최소화 목적에서는 음의 부호로 들어간다. 판별기의 진위 판단 목표와 생성기의 정보 보존 목표를 함께 읽는다. p(c₁,…,c_k)의 곱 표현은 코드 성분을 독립으로 두는 원문의 가정을 나타낸다.

p[c1,c2,,ck]=i=1kp[ci]

InfoGAN

Generator is of the form G[z,c] and involves information - regularized minimax game,
minGmaxDνGANλI[c;G[z,c]]
Mutural information
I[x;y]0

Training InfoGAN

Train the discriminator D[x]

maxDEzpd[x][log[D[x]]]+Ezpz[z],cpc[c][log[1D[G[z,c]]]]

Train the generator G[z,c]

minGEzpz[z][log[1D[G[z,c]]]]λI[c;G[z,c]]
Mutual information : I[c;G[z,c]]

Variational Infomax

직접 계산하기 어려운 정보량에 하한을 둔다

I=H(c)−H(c|생성결과)는 결과를 보았을 때 코드의 불확실성이 얼마나 줄어드는지 나타낸다. q(c|x)를 이용한 하한은 이를 학습 가능한 추정 문제로 바꾸려는 흐름이다. 원문 KL 중간식의 두 분포 표기는 따로 확인해야 하므로, 같은 분포끼리의 KL이 일반적인 차이 항인 것처럼 설명하지 않는다.

The mutual information I[c;G[z,c]] is hard to maximize directly as it requires access to the posterior p[cx]
Consider a variational lower - bound on the mutual information term
I[c;G[z,c]]=H[c]H[cG[z,c]]
=ExG[z,c][Ecp[cx][log[p[cx]]]]+H[c]
=ExG[z,c][KL[p[cx]p[cx]]0]+ExG[z,c][Ecp[cx][log[q[cx]]]]+H[c]
ExG[z,c][Ecp[cx][log[q[cx]]]]+H[c]
=ECp[c],xG[z,c][log[q[cx]]]+H[c]
=Linfomax

Conditional GAN

조건부 생성에서는 진위뿐 아니라 조건도 함께 본다

생성기에 잡음 z와 조건 y를 같이 주고, 판별기에도 x와 y를 함께 준다. 따라서 결과가 실제처럼 보이는지만 아니라 주어진 조건과 맞는지도 구조에 반영된다. 조건의 종류는 원문에 제시된 라벨 또는 다른 자료 형태의 범위에서 읽는다.

Generator is trained to generate a fake sample x with a condition y
(e . g ., class label or data from other modalities) provided as another input, in addition to noise z .

Generator

Input noise z and y are combined in joint hidden representation

Discriminator

x and y are presented as inputs to the discriminator

Optimization

minGmaxDExpdata[x],yp[y][log[D[x,y]]]+Ezp[z],yp[y][log[1D[G[z,y],y]]]

Image-to-image translation

Map Edges to Photo via cGAN

Unpaired Image to Image Translation

왕복 변환이 입력을 유지하도록 제한한다

G는 X에서 Y로, F는 Y에서 X로 변환한다. F(G(x))가 x에 가깝고 G(F(y))가 y에 가까워야 한다는 것이 순환 일관성이다. 단순히 다른 영역처럼 보이는 결과를 만드는 목표에 입력 정보를 보존하려는 제약을 더한다.

Given any two unordered image collections, it learns to automatically translate an image from one to other and vice versa

Cycle-Consistency

Translation should be cycle consistent
G:XY
F:YX
F[G[x]]xandG[F[y]]y
원본 도해
원본 도해

Adversarial Loss + Cycle Consistency Loss

Adversarial loss for G:XY and F:YX

적대적 손실과 왕복 오차를 나누어 읽는다

두 적대적 손실은 각 방향의 결과가 해당 데이터 영역처럼 보이는지를 다룬다. 순환 손실은 왕복한 결과와 원래 입력 사이의 L₁ 차이를 모은다. 목적이 서로 다르므로 한 항의 개선이 다른 항의 개선과 같은 의미라고 보지 않는다.

Lgan[G,DY,X,Y]=Eypdata[y][log[DY[y]]]+Expdata[x][log[1DY[G[x]]]]
Lgan[F,DX,Y,X]=Expdata[x][log[DX[x]]]+Eypdata[y][log[1DX[F[y]]]]

Cycle consistency loss

Lcyc[G,F]=Expdata[x][|F[G[x]]x|1]+Eypdata[y][|G[F[y]]y|1]

Summary

AE VAE GAN
likelihood-based likelihood-free
Deterministic Encoder & Decoder Probabilistic Encoder & Decoder Deterministic Decoder
GAN+Encoder=ALI (Adevarsarially learned inference)Bi-GAN
Probabilistic  Decoder Generator network
Blurry images Sharp-looking image
x Mode-collapsing problem

GANs with Encoder Networks

Adversarially Learned Inference

입력과 잠재변수의 쌍을 비교한다

ALI에서는 실제 x와 인코더가 만든 z의 쌍, 잡음 z와 생성기가 만든 x의 쌍을 비교한다. q(x,z)와 p(x,z)는 각각 이러한 두 경로의 결합분포이다. 데이터를 생성하는 방향과 잠재표현을 추정하는 방향을 함께 맞추려는 구조이다.

원본 도해

Encoder joint distribution

q[x,z]=q[x]q[zx]

Decoder joint distribution

p[x,z]=p[z]p[xz]

Match these two joint distributions

The miniax game

minGmaxDEq[x][log[D[x,E[x]]]]+Ep[z][log[1D[G[z;θ],z]]]

Semi-Supervised Learning with GANs

Small amount of labeled data

일부 정답과 정답 없는 데이터를 함께 다룬다

K개의 실제 범주에 생성 데이터라는 추가 범주를 두는 방식이다. 지도 손실은 알려진 라벨을 맞추고, 비지도 손실은 실제·생성 데이터의 구별에 관련된다. 마지막 손실 변형의 로그 부호는 앞의 최소화 정의와 일치하는지 확인해야 하며, 표기 차이를 임의로 하나로 합치지 않는다.

small labeled data + large unlabeled data = semi - supervised learning
multi - modal learning
meta - learning

Semi-Supervised Learning with GAN

Classifier for K classes

pmodel[y=kx]=exp[k]j=1Kexp[j]where k are logits

GAN

Label generated samples with y = K + 1, i . e ., pmodel[y=K+1x] yields the probability that x is fake

Loss

L=Lsupervised+Lunsupervised
Lsupervised=E(x,y)pdata(x,y)[log[pmodel[yx,yK]]]
Lunsupervised=Expdata[x][log[1pmodel[y=K+1x]]]ExG[log[pmodel[y=K+1x]]]
This ℒ_unsupervised is the case where in the standard GAN game value, we use D[x] = 1 - pmodel[y=K+1x], yielding
Lunsupervised=Expdata[x][log[D[x]]]Ezp[z][log[1D[G[z]]]]

정리하면

GAN의 여러 변형은 단순히 이미지를 그리는 방식의 차이가 아니라 생성기가 무엇을 만족해야 하는지를 바꾸는 방법이다. 각 손실 항의 대상과 최적화 방향을 따로 읽어야 한다.