배워서 남 주자

AI/인공지능 및 기계학습 개론

[인공지능 및 기계학습 개론] 4.3. Logistic Regression Parameter Approximation 1

신라면순한맛 2022. 8. 28. 01:00

이번 포스팅에서는 Logistic regression paramter에 대해서 알아보는 시간을 갖도록 하겠습니다.

 

http://www.edwith.org/machinelearning1_17/lecture/10592

 

[LECTURE] 4.3. Logistic Regression Parameter Approximation 1 : edwith

- 신승재

www.edwith.org

 


 

지난 포스팅([인공지능 및 기계학습 개론] 4.2 Introduction to Logistic Regression)에서 logistic regression에 대한 식을 다음과 같이 구했었습니다.: $$ P(Y|X)=\frac{e^{X\theta}}{1+e^{X\theta}} $$ 이 때의 $\theta$를 구하기 위해 MLE를 복기해보면 아래와 같습니다. $$ \hat\theta=\argmax_{\theta} P(D|\theta) $$ 여기서 $D$에 대해 좀 더 구체적으로 상황을 고려해서, input $X_i$에 대한 label $Y_i$가 있다고 해보겠습니다. 통계적으로는 input이 따르는 분포에서 $X_i$라는 변수들을 iid로 추출하고, 마찬가지로 label이 따르는 분포에서 $Y_i$를 iid로써 추출한 것을 보고 있다고 생각하시면 됩니다. 그러면 $\hat\theta$는 아래와 같이 정리가 가능합니다. $$ \begin{aligned} \hat\theta &= \argmax_{\theta} P(D|\theta) \\ &= \argmax_{\theta} \prod_{i=1}^N P(Y_i|X_i;\theta) \\ &= \argmax_{\theta} \log\bigg(\prod_{i=1}^N P(Y_i|X_i;\theta)\bigg) \\ &= \argmax_{\theta}\sum_{i=1}^N \log P(Y_i|X_i;\theta) \end{aligned} $$ 이제 압정을 던지는 상황처럼 label이 true/false로 나올 수 있다 가정하고 이 상황을 모델링 하면 $$ P(y|x)=\mu(x)^y(1-\mu(x))^{1-y} $$ 이라 둘 수 있고, 이 때 $\mu(x)$를 다음과 같이 logistic function으로 modeling 해보겠습니다. $$ \mu(x)=\frac{1}{1+e^{\theta^t x}}=P(y=1|x) $$ 그러면 $\theta$를 logistic regression 하는 관점, 즉 $$ X\theta = \log\bigg(\frac{P(Y|X)}{1-P(Y|X)}\bigg) $$를 적용하면 아래와 같이 정리가 가능합니다. $$ \begin{aligned} \log P(Y_i|X_i;\theta) &= Y_i\log\mu(X_i)+(1-Y_i)\log(1-\mu(X_i)) \\ &= Y_i(\log\mu(X_i) - \log(1-\mu(X_i))) + \log(1-\mu(X_i)) \\ &= Y_i\log\bigg(\frac{\mu(X_i)}{1-\mu(X_i)}\bigg) + \log(1-\mu(X_i)) \\ &= Y_i X_i \theta + \log(1-\mu(X_i)) \\ &= Y_i X_i \theta - \log\bigg(1-\frac{e^{X_i\theta}}{1+e^{X_i\theta}}\bigg) \\ &= Y_i X_i \theta + \log\bigg(\frac{1}{1+e^{X_i\theta}}\bigg) \\ &= Y_i X_i \theta - \log(1+e^{X_i\theta}) \end{aligned} $$ 이로부터 $\hat\theta$를 다시 정리해보면 $$ \hat\theta=\argmax_{\theta}\sum_{i=1}^N \log P(Y_i|X_i;\theta)=\argmax_{\theta}\sum_{i=1}^N (Y_i X_i \theta - \log(1+e^{X_i\theta})) $$ 로 정리할 수 있습니다. $\argmax$ 값을 구하기 위해 미분을 통해 극점을 찾아보도록 하겠습니다. $\theta=(\theta_0,\ldots,\theta_n)\in\mathbb{R}^{n+1}$이므로 각 $\theta_j\:(j=0,\ldots,n)$에 대해 편미분하면 $\frac{\partial}{\partial\theta_j}\theta = \mathbf{e}_j$, $X_i\mathbf{e}_j=X_{ij}$이므로, $$ \begin{aligned} \frac{\partial}{\partial\theta_j}\sum_{i=1}^N (Y_i X_i \theta - \log(1+e^{X_i\theta})) &= \sum_{i=1}^N Y_i X_{ij} + \sum_{i=1}^N \bigg(-\frac{1}{1+e^{X_i\theta}}\cdot e^{X_i\theta}\cdot X_{ij}\bigg) \\ &= \sum_{i=1}^N X_{ij}\bigg(Y_i-\frac{e^{X_i\theta}}{1+e^{X_i\theta}}\bigg) \\ &= \sum_{i=1}^N X_{ij}(Y_i-P(Y_i=1|X_i;\theta))\end{aligned} $$ 인 것을 알 수 있습니다. 여기서 극점을 찾으려면 마지막 항이 $0$이 되게 하는 $\theta$를 찾아야 하는데, MLE처럼 $\theta$에 대해 정리가 깔끔하게 되지는 않습니다. 따라서 이렇게 closed form이 없는 경우 근사를 하여 $\theta$를 구합니다.

 


 

다음 포스팅에서 이어서 해당 내용을 다뤄보도록 하겠습니다.