300x250

 

 

Flow-based Models (1)에서 모델을 전반적으로 이해해보았다. 본 포스팅에서는 flow-based model의 예시를 알아보자.

Flow-based model은 invertible transformation에 어떤 함수를 사용하는지에 따라 나뉜다.

 

 

 

 

Planar Flows

 

Planar flow에서의 invertible transformation은 다음과 같다. (편의 상 bold체를 따로 사용하지 않고 벡터를 표현한다.)

x=fθ(z)=z+uh(wz+b)

 

위 식과 같이 θ=(w,u,b)로 parameterize하고, non-linearity h()이 non-linearity라 할 때, Jacobian의 determinant의 절댓값은 다음과 같이 구할 수 있다.

|dete(fθ(z)z)|=|det(I+h(wz+b)uw)|=|1+h(wz+b)uw|

 

그런데, invertible 성질을 만족하기 위해서는 다음과 같은 두 가지 제약조건이 필요하다.

h=tanh()

h(wz+b)uw1

 

Base distribution π가 Gaussian distribution인 경우와 Uniform distribution인 경우, M개 (이전 글에서의 K) transformation 후 distribution을 시각화해보면 다음과 같은 결과를 보인다.

 

Fig 1. Planar Flow result on Gaussian distribution
Fig 2. Planar Flows result on uniform distribution

 

 

 

 

NICE and Real-NVP

 

NICE와 Real-NVP라는 flow-based model을 알아보자. 특히 Real-NVP는 GLOW 등 좋은 성능을 보이는 다른 flow-based model들의 기본이 되는 중요한 모델이다.

 

 

Nonlinear Independent Components Estimation (NICE)

 

NICE는 additive coupling layer와 rescaling layer 이렇게 두 가지 invertible transformation으로 구성된다.

 

Additive coupling layers

Additive coupling layer에서는 random variable z두 subset z1:dzd+1:D으로 나눈다. 이때 1d<D는 랜덤으로 고른다.

 

그리고 layer에서는 두 가지 연산 forward mapping zx와 inverse mapping xz을 수행한다.

먼저 forward mapping은 아래와 같이 진행된다.

x1:d=z1:didentity transformation
xd+1:D=zd+1:DMθ(z1:d)
  • Mθ : d개 input units, Dd개 output units를 갖는 neural network

첫 번째 subset에 대해서는 identity transformation(그대로 transform), 두 번째 subset에 대해서는 첫 번째 subset을 입력한 neural network의 결과를 뺀 결과를 x로 맵핑하는 transformation이다.

이때 Jacobian은 다음과 같이 계산한다.

J=xz=[Id×d0d×(Dd)xd+1:Dz1:dI(Dd)×(Dd)]

Jacobian은 lower triangular matrix 형태로 나오며, determinant가 1이므로 volume이 보존된다. (volume preserving transformation)

 

또한 inverse mapping은 아래와 같이 진행한다.

z1:d=x1:didentity transformation
zd+1:D=xd+1:D+Mθ(x1:d)

Forward mapping에서 사용했던 neural network를 그대로 활용하여 xz로 맵핑한다.

 

NICE에서는 여러 개의 서로 다른 d값을 갖는, 즉 partition이 서로 다른 additive coupling layer들을 거친다.

 

Rescaling layeres

Rescaling layer는 여러 additive coupling layer를 거친 후 마지막 layer에서 동작하는 transformation이다.

 

마찬가지로 forward mapping zx와 inverse mapping xz을 수행한다.

Forward mapping부터 살펴보자.

xi=sizi
  • si : i번째 dimension에 대한 scaling factor

즉, 단순히 z의 요소 각각에 scaling factor를 곱해주는 과정이다.

Jacobian도 간단하다.

J=diag(s)
det(J)=i=1Dsi

 

Inverse mapping은 자연스럽게 다음과 같이 계산한다.

zi=xisi

 

 

 

Real-valued Non-volume Preserving Flows (Real-NVP)

 

Real-NVP도 NICE와 비슷하게 coupling layer와 permutation layer 두 가지 layer로 구성된다. 단, 이름에서 알 수 있듯이, Jacobian의 determinant가 1이 아닌, non-volume preserving transformation을 활용한다.

 

Coupling layers

NICE에서의 additive coupling layer와 마찬가지로, 우선 variable z를 두 subset z1:dzd+1:D으로 나눈다. (마찬가지로 1d<D)

 

Forward mapping zx는 다음과 같다.

x1:d=z1:didentity transformation
xd+1:D=zd+1:Dexp(s(z1:d))+t(z1:d)
  • s(), t() : 각각 scaling, transition neural network로, d개 units을 입력받아 nd개 units를 출력한다.
  • : Element-wise 곱(production)을 나타낸다.

 

Inverse mapping xz는 다음과 같다.

z1:d=x1:didentity transformation
zd+1:D=(xd+1:Dt(x1:d))exp(s(x1:d))

 

Jacobian과 그 determinant는 다음과 같이 계산한다.

J=xz=[Id×d0d×(Dd)xd+1:Dx1:ddiag(exp(s(x1:d)))]
det(J)=j=1Ddexp(s(x1:d))j=exp(j=1Dds(xa)j)

이 경우, determinant 값이 1이 아닐수도 있다. 즉 non-preserving volume transformation이므로 더 general한 모델이다.

 

 

 

 

 

MAF, IAF, Parallel Wavenet

 

Real-NVP까지는 flow-based model에서 어떤 transformation을 사용하는가로 구분지었다. 그런데, autoregressive model을 flow model처럼 사용할 수도 있다. 이러한 방법을 autoregressive flow라 한다.

아래와 같은 Gaussian autoregressive model을 복기해보자.

p(x)=i=1np(xi|x<i)

여기서 conditional distribution p(xi|x<i)는 Gaussian distirbution N(μi(x1,,xi1),exp(αi(x1,,xi1))2)을 따르며, μi()αi()i=1일 때 constant, i>1일 때 neural network이다.

 

즉, autoregressive flow는 결국 standard Gaussian에서 샘플링한 z를 모델이 생성한 샘플 x로 맵핑하는 것이고, 이때 μi(),αi()로 parameterize한 invertible transformation을 활용한다.

 

 

 

Masked Autoregressive Flow (MAF)

 

Fig 3. MAF

 

MAF의 forward mapping zx 과정은 다음과 같다. (sequential)

  • i=1,,n에 대해 ziN(0,1) 샘플링
  • x1=exp(α1)z1+μ1으로 두고, μ2(x1)α2(x1) 계산
  • x2=exp(α2)z2+μ2으로 두고, μ3(x1,x2)α3(x1,x2) 계산

따라서 샘플링을 할 때 autoregressive하게 이전 value들을 사용하여 계산함으로써 샘플링 시간이 오래걸린다는 특징이 있다.

 

MAF의 inverse mapping xz 과정은 다음과 같다. (parallel)

  • 모든 μiαi를 계산한다. (모든 x를 알고 있으므로 parallel computation 가능) → MADE 모델의 과정과 같다.
  • zi=(xiμi)/exp(αi)z를 구한다.

따라서, MAF는 샘플링을 할 때에는 autoregressisve하게(이전 value들을 사용하여) 계산해야 하기 때문에 느리지만, likelihood를 구할 때(training할 때)에는 빠르게 할 수 있다.

 

 

 

Inverse Autoregressive Flow (IAF)

 

IAF는 말그대로 MAF와 forward, inverse mapping 방법이 반대이다. 즉, MAF 수식에서 xz를 바꾸면 IAF이다.

 

Fig 4. IAF

 

 

IAF의 forward mapping zx 과정은 다음과 같다. (parallel)

  • 모든 i=1,,n에 대해 ziN(0,1)를 샘플링한다.
  • 모든 μi,αi를 계산한다.
  • xi=exp(αi)zi+μix를 계산한다.

 

Inverse mapping xz 과정은 다음과 같다. (sequential)

  • z1=(x1μ1)/exp(α1)μ2(x1),α2(x1)을 계산한다.
  • z2=(x2μ2)/exp(α2)μ3(x1,x2),α3(x1,x2)를 계산한다.

 

즉, sampling은 빠르지만 training이 느리다.

 

 

 

Parallel Wavenet

 

MAF와 IAF처럼, 샘플링 시간과 학습 시간은 trade-off 관계이다. 그런데, Parallel Wavenet 모델에서는 knowledge distillation을 활용하여 training 시에는 MAF의 이점을, sampling 시에는 IAF의 이점을 활용한다. 

Training은 두 단계로 나뉜다.

  1. Teacher model (MAF)를 학습한다.
  2. Student model (IAF)를 학습한다. 이때 loss는 아래와 같은 KL divergence를 활용한다.

DKL(s,t)=Exs[logs(x)logt(x)]

 

Objective는 다음과 같이 나타낼 수 있다.

argminθDKL[t(xi)s(xi)]

 

Test(evaluation) 시에는 IAF 기반의 student model을 사용한다.

 

728x90
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기