VAE는 Autoencoder(AE)에 확률적 생성 모형과 Variational Inference를 결합한 생성형 딥러닝 모델이며, Latent Variable을 이용해 확률적으로 데이터를 생성한다.
Autoencoder(AE)는 입력 데이터를 저차원의 Latent Representation으로 압축한 뒤, 다시 원래 데이터와 유사하게 복원하도록 학습하는 비지도학습 기반의 신경망이다.
구조
$X → Encoder → Z → Decoder → \hat{X}$
Encoder: 하나의 latent representation(vector) $z$로 변환
$z = f_{\phi}(X)$
Decoder: latent representation(vector) $z$를 다시 원래 데이터 공간으로 복원
$\hat{X} = g_{\theta}(z)$
$\displaystyle \therefore \hat{X} = g_{\theta}(f_{\phi}(X))$
Variational Autoencoder(VAE)는 입력 데이터를 저차원의 확률분포로 모델링하고, 그 분포에서 잠재 변수를 샘플링하여 다시 새로운 데이터를 생성하는 모델이다.
구조
$X → q_{\phi} (Z|X) → Z → p_{\theta} (X|Z) → \hat{X}$
Encoder
latent variable $Z$의 posterior distribution을 근사하는 분포를 출력한다.
$q_{\phi} (Z|X) = N(Z; \mu_{\phi}(X), \text{diag}(\sigma_{\phi}^2(X)))$
Sampling
위에서 만들어진 approximate posterior distribution에서 latent variable $Z$를 sampling한다.
$Z \sim q_{\phi} (Z|X)$
Neural Network를 Gradient Descent로 학습해야 한다.
$\displaystyle \frac {\partial \mathcal{L}} {\partial \phi}$
그러나 $Z \sim N(\mu, \sigma^2)$에서 random sampling을 직접 수행하면 sampling 과정이 미분 가능한 형태로 표현되기 어렵다. 이를 해결하기 위해 Reparameterization Trick을 사용한다.
Reparameterization Trick
$Z = \mu + \sigma \odot \epsilon$으로 변환하여 $\mu, \sigma$와 $\epsilon \sim N(0, I)$를 분리한다.
Randomness는 $\epsilon$에만 존재하고, $Z$는 $\mu$와 $\sigma$에 대해 미분 가능한 함수가 된다.
$\displaystyle \frac {\partial Z} {\partial \mu}=1$
$\displaystyle \frac {\partial Z} {\partial \sigma}=\epsilon$
$\therefore Z = \mu_{\phi}(X) + \sigma_{\phi}(X) \odot \epsilon$
Decoder
Sampling된 $Z$를 입력으로 받아 조건부 확률분포 $p_{\theta}(X|Z)$를 모델링한다.
VAE의 LossFunction은 $-ELBO$(증거 하한, Evidence Lower Bound)를 최소화하는 형태로 사용되며, 이는 Reconstruction Loss와 KL Divergence로 구성된다.
$\mathcal{L} = -ELBO = \mathcal{L}{recon} + \beta \mathcal{L}{KL}$
Reconstruction Loss(재구성 손실)
$\mathcal{L}{recon} = -\log p{\theta}(x|z)$