Generative modeling by estimating gradients of the data distribution 에 대해 간단히 리뷰해보겠습니다.
안녕하세요. 이번에는 논문 Generative modeling by estimating gradients of the data distribution에 대해 리뷰해보겠습니다. 이 논문은 데이터 생성과 관련해서 꽤 유명한 논문입니다. 아시는 분은 아시겠지만 이논문의 저자인 Yang Song 은 Score-Based Generative Modeling through Stochastic Differential Equations 논문으로 유명한 분이시지요. 이 논문 Generative modeling by estimating gradients of the data distribution을 읽게 되면 Score-Based Generative Modeling through Stochastic Differential Equations를 읽는데 있어서 도움이 되는 리뷰가 될것입니다. 시작하기 전에 안내를 드리자면 Score-Based Generative Modeling through Stochastic Differential Equations에 대한 리뷰는 제 영상에 있으니 (링크) 참고해주시길 바래요. 그러면 이제 시작해보겠습니다.
Langevin Dynamics 를 이용한 데이터 생성과정
이 논문을 이해하기 위해서는 Langevin Dynamics 를 이용한 데이터 생성과정을 우선 이해하고 있어야 합니다. Langevin Dynamics 는 Stochastic Differential Equation (SDE)를 이용해 표현되는 Dynamics 를 말합니다. 내가 원하는 데이터 분포의 PDF (Probability Density Function) p(\mathbf{x}) 가 있을 때 이 pdf로부터 데이터 샘플을 생성하기 위한 Langevin Dynamics 는 아래와 같이 식 (1)로 표현됩니다.
\begin{equation} d\mathbf{x} = \nabla_{\mathbf{x}} \log p(\mathbf{x})dt + \sqrt{2} d \mathbf{x} \end{equation}위의 식 (1)을 이용해서 p(\mathbf{x}) 의 데이터 샘플을 생성하지요. 데이터 샘플 생성 과정은 아래의 식 (2)와 같습니다.
\begin{equation} \mathbf{x}_{i+1} = \mathbf{x}_i + \epsilon \nabla_{\mathbf{x}_i} \log p (\mathbf{x}_i) + \sqrt{2\epsilon} \mathbf{z}_i, \mathbf{z}_i \sim N \end{equation}
\epsilon > 이 충분히 작다면 (2)의 과정을 반복한다면 p(\mathbf{x}) 로 부터 데이터 생성이 가능합니다. 여기서 주목해야 할점이 식 (2)에서 보듯이 Langevin Dynamics 를 이용하여 데이터를 생성하기 위해서는 score function 인 \nabla_{\mathbf{x}} \log p(\mathbf{x}) 가 필요하단 것을 알 수 있습니다.
Score Matching 방법
위에서 보았듯이 Langevin Dynamics 를 이용하여 데이터 샘플을 생성하기 위해서는 score function \nabla_{\mathbf{x}} \log p(\mathbf{x}) 가 필요합니다. 그런데 \nabla_{\mathbf{x}} \log p(\mathbf{x}) 에 대해서는 알 수 없는 이를 근사하는 score model \mathbf{s}_\mathbf{\theta}( \mathbf{x}) 가 필요합니다. \mathbf{s}_\mathbf{\theta}( \mathbf{x}) 가 score function 을 제대로 근사하다면 식 (2) 대신에 식 (3) 처럼 데이터 샘플을 생성합니다.
\begin{equation} \mathbf{x}_{i+1} = \mathbf{x}_i + \epsilon \nabla_{\mathbf{x}_i}\mathbf{s}_\mathbf{\theta}( \mathbf{x}) + \sqrt{2\epsilon} \mathbf{z}_i, \mathbf{z}_i \sim N \end{equation}그러면 \mathbf{s}_\mathbf{\theta}( \mathbf{x}) 는 어떻게 근사할까요? 아래와 같은 손실함수를 만들어서 최적화를 시킵니다.
\begin{equation} E_{p(\mathbf{x})}\| \nabla_\mathbf{x} \log p (\mathbf{x}) - \mathbf{s}_\mathbf{\theta} (\mathbf{x}) \|^2 \end{equation}
위와 같이 \mathbf{s}_\mathbf{\theta} 를 score function에 근사하는 방식을 score matching 이라고 부릅니다.
Score matching 의 문제점
![image01](https://seongqjini.com/wp-content/uploads/2023/08/image01-optimized.png)
score matching 의 문제점이 있습니다. p(\mathbf{x}) 의 값이 작은 구역에서는 \mathbf{s}_\mathbf{\theta}가 \log p (\mathbf{x})를 근사할 수 있는 영역이 적어집니다. 어떻게 해결했을까요?
논문에서 제안한 방식
score matching 이 잘 안되는 부분이 있는데요. Yang Song은 score matching 을 하기 위해 새로운 방법을 제시하였습니다. 데이터 분포에 perturbation 을 주는 시도를 하였습니다. perturbation 을 준 분포에 대한 score에 대해서는 추정을 잘한다는 것을 발견하였습니다. 오호! 이것을 수식으로 써볼까요? p_{\sigma_i} ( \tilde{\mathbf{x}} | \mathbf{x} ) = N ( \tilde{\mathbf{x}} ; \mathbf{x}, \sigma_i^2 \mathbf{I} ), i=1,2,...,L, \sigma_1 < \sigma_2 < ... \sigma_L 를 이용하여 데이터에 perturbation 을 줍니다. \sigma_i 가 클 수록 데이터에 더 많은 노이즈가 주입됩니다. 그리고 p_{\sigma_i}( \tilde{\mathbf{x}}) = \int p(\mathbf{x}) p_{\sigma_i} ( \tilde{\mathbf{x}} | \mathbf{x}) d \mathbf{x} 의 score \nabla_\mathbf{x} \log p_{\sigma_i} ( \mathbf{x}) 를 근사하는 Noise Conditional Score-Based Model (NCSN) \mathbf{s}_\mathbf{\theta}(\mathbf{x},i) \approx \nabla_{\mathbf{x}} \log p_{\sigma_i}(\mathbf{x}) 를 학습하는 방법을 제안했습니다. 손실함수는 아래와 같습니다.
Annealed Langevin Dynamics
여기서 중요한점은 \sigma_i 가 클 수록 \mathbf{s}_\mathbf{\theta} ( \mathbf{x},i) 이 score \log p_{\sigma_i} ( \mathbf{x} ) 를 잘 추정합니다. 그래서 먼저 추정을 잘할 수 있는 p_{\sigma_i} (\mathbf{x}) 분포로부터 데이터 샘플을 생성하고 \sigma_i 를 줄여가면서 데이터를 생성합니다. 이 과정을 Annealed Langevin Dynamics 라고 부릅니다. 아래에 그림이 잘 나와있네요.
![image03](https://seongqjini.com/wp-content/uploads/2023/08/image03-optimized.png)
출처 – Song, Y., & Ermon, S. (2019). Generative modeling by estimating gradients of the data distribution. Advances in neural information processing systems, 32.