본문 바로가기

Deep Learning

Neural Networks and Deep Learning - Logistic Regression

본 글은 Andrew Ng의 강의를 듣고 필요한 부분을 정리했습니다. Notation을 다르게 사용했습니다.

01. Logistic Regression 

  • - 이진분류 (0, 1)에 주로 사용되는 방법론
  • Single variable case : Given $\boldsymbol{x}$, want $\hat{y_i} = P(y = 1|X = x_i)$ where $\boldsymbol{x} = (x_1, x_2, \cdots, x_n) \in \mathbb{R}^{n}$ and $x_i \in \mathbb{R}$
    - parameters : $w \in \mathbb{R}, b \in \mathbb{R}$,
    - output :  $\hat{\boldsymbol{y}} = w \boldsymbol{x} + b$
    단지 w^t x + b로 둔다면 음수, 양수, 1보다 큰 값 모두 발생 가능하기 때문에, 로지스틱 리그레션은 여기에 시그모이드 함수를 적용한다.
    => output : $\hat{\boldsymbol{y}} =  \sigma(w \boldsymbol{x} + b)$ where $\sigma(z) = \frac{1}{1 + e^{-z}}$.
  • Multivariate case logistic regression: $$ 
    \begin{aligned}
    \hat{y}^{(i)} &= \sigma(X \boldsymbol{w} + b), \textrm{  where  }  \sigma(z) = \frac{1}{1 + e^{-z}} \\ &\textrm{  and  } X = [X_1, \cdots, X_p]  \textrm{  for  } X_j \in \mathbb{R}^m, \boldsymbol{w} = (w_1, w_2, \cdots, w_p)^T \in \mathbb{R}^p
    \end{aligned}
    $$

sigmoid(z) - between 0 and 1

  • Cost function of logistic regression
    - 문제 정의 : Given $\{(x^{(1)}, y^{(1)}), \cdots, (x^{(m)}, y^{(m)})\}$, want $\hat{y}^{(i)} \approx y^{(i)}$.
    - Loss(error) function : $L(\hat{y}, y) = -(y\log\hat{y} + (1-y) \log (1-\hat{y}))$
       if $y=1 : L(\hat{y}, y) = -\log\hat{y}$ <- want $\log\hat{y}$ large, want $\hat{y}$ large
       if $y=0 : L(\hat{y}, y) = -\log(1-\hat{y})$ <- want $\log(1-\hat{y})$ large, want $\hat{y}$ small
    => gradient descent가 용이하게 하기 위해 convex한 손실함수를 정의한다. 

    - Cost function : $$J(w, b) = \frac{1}{m} \sum_{i=1}^m L(\hat{y}^{(i)}, y^{(i)}) = -\frac{1}{m} \sum_{i=1}^m [y^{(i)}\log\hat{y}^{(i)} + (1-y^{(i)}) \log (1-\hat{y}^{(i)})]$$