wikepedia 내용을 정리하고자 한다. 일반적으로 loss 관점에서만 설명되는 글들이 많은데 확률분포 추정에서의 접근이 더 근본적인 방식이다.
1. KL Divergence(Kullback-Leibler Divergence)
KL Divergence는 두 확률분포가 $P,Q$가 있을 때 두 분포가 얼마나 다른지를 측정하는 계산식이다. 정의와 예시는 이 wiki에 있다.
$$D_{KL}(P \ || \ Q) = \sum_{x \in X}P(x) \text{log} (\frac{P(x)}{Q(x)})$$
2. Cross Entropy
(1) 정의
정보이론에서 cross-entropy는 두 확률 분포 p 와 q를 구분하기 위해 필요한 평균 비트 수를 의미한다.
In information theory, the cross-entropy between two probability distributions $p$ and $q$ over the same underlying set of events measures the average number of bits needed to identify an event drawn from the set if a coding scheme used for the set is optimized for an estimated probability distribution $q$, rather than the true distribution $p$
수식 정의는 다음과 같다.
$$H(p,q) = -\mathbb{E}_{p}[\text{log}q]$$
(2) KL Divergence와의 관계
KL Divergence를 풀어쓰면 cross-entropy와의 관계를 알 수 있다. 교집합에서 자기 집합을 빼는 것 처럼 생각해볼 수 있다.
$$\begin{array}{rcl}
D_{KL}(P \ || \ Q) & = & \sum_{x \in X}P(x) \text{log} (\frac{P(x)}{Q(x)}) \\
& = & \sum_{x \in X}p(x)\text{log}\frac{1}{q(x)} -\sum_{x \in X}p(x)\text{log}\frac{1}{p(x)} \\
& = & H(P,Q) - H(P)
\end{array}$$
3. Likelihood
(1) 정의
분류 문제를 풀 때는 참 분포($P$)를 관찰하여 여러 결과들의 확률을 추정($Q$)하고 싶다. 일반적으로 $P$는 true distribution, observation 등으로 표현되고 $Q$는 $\theta$로 parametrized된 모델 $Q_{\theta}$를 의미한다.
파라미터 $\theta$를 기반으로 한 결과 $i$의 추정 확률을 $q_{\theta}(X=i)$, 학습 데이터(observation, empirical probability)에서 결과 $i$의 빈도를 $p(X=i)$라 하자. 그럼 관찰 데이터에 대한 모델 $q_{\theta}(X=x)$의 likelihood는 다음과 같이 정의할 수 있다.
$$\begin{array}{rcl}
L(\theta)& =& \underset{i \in X}{\prod}(\text{estimated probability of} \ i)^{\text{number of occurrences of} \ i} \\
& = & \underset{i \in X}{\prod}q_{\theta}(X=i)^{N_{p}(X=i)} \\
\end{array}$$
이 likelihood에 $log$를 취하고 $N$으로 나눠주면 다음과 같은 결과를 얻을 수 있다.
$$\begin{array}{rcl}
\frac{1}{N}\text{log}(L(\theta)) & = & \frac{1}{N}\text{log} \underset{i \in X}{\prod}q_{\theta}(X=i)^{N_{p}(X=i)} \\
& = &\sum_{i}p(X=i)\text{log}q_{\theta}(X=i) \\
& = & -H(p,q)
\end{array}$$
(2) Negative Loss Likelihood
PyTorch를 사용하다 보면 CrossEntropy(), NLLloss()가 모두 구현되어 있는 것을 볼 수 있다. 근데 NLLloss를 찾아보면 수식만 나와있고 정의가 없다(위키에도 없다).
위의 likelihood의 정의를 통해 이를 유추해볼 수 있다. 먼저 우리가 원하는 것은 관찰된 분포 $P$와 모델의 확률분포 $Q_{\theta}$가 가깝도록 학습하고 싶으며, SGD등을 통해 두 확률분포의 KL Divergence 값을 계산하여 최소화하는 $\theta$를 찾을 수 있다.
$$D_{KL}(P \ || \ Q) = H(P,Q) - H(P)$$
이때 KL Divergence를 최소화 하는 것은 두 확률분포의 Cross Entropy를 최소화하는 것과 동일하다. 그리고 likelihood 정의에 따라 Cross Entropy를 최소화하는 것은 Negative Loss Likelihood를 최소화하는 것과 동일하다.
$$\text{NLL} = - \frac{1}{N}\text{log}(L(\theta)) = H(P,Q) $$
PyTorch 구현 부분을 살펴보면 CrossEntropyLoss를 사용할 때는 출력값 logit을 그대로 입력으로 받아 Cross Entropy값을 계산하고, NLLloss를 사용할 때는 출력값 log에 log softmax를 씌우고 Cross Entropy를 계산한다(참고).
'Pattern Recognition' 카테고리의 다른 글
5. Density Estimation (0) | 2022.09.12 |
---|---|
4. Clustering (2) (2) | 2022.09.12 |
4. Clustering (1) (0) | 2022.09.11 |
3. Maximum Likelihood Estimation(MLE) (0) | 2022.08.06 |
(참고) Gaussian Distribution (0) | 2022.08.06 |
wikepedia 내용을 정리하고자 한다. 일반적으로 loss 관점에서만 설명되는 글들이 많은데 확률분포 추정에서의 접근이 더 근본적인 방식이다.
1. KL Divergence(Kullback-Leibler Divergence)
KL Divergence는 두 확률분포가
2. Cross Entropy
(1) 정의
정보이론에서 cross-entropy는 두 확률 분포 p 와 q를 구분하기 위해 필요한 평균 비트 수를 의미한다.
In information theory, the cross-entropy between two probability distributionsand over the same underlying set of events measures the average number of bits needed to identify an event drawn from the set if a coding scheme used for the set is optimized for an estimated probability distribution , rather than the true distribution
수식 정의는 다음과 같다.
(2) KL Divergence와의 관계
KL Divergence를 풀어쓰면 cross-entropy와의 관계를 알 수 있다. 교집합에서 자기 집합을 빼는 것 처럼 생각해볼 수 있다.
3. Likelihood
(1) 정의
분류 문제를 풀 때는 참 분포(
파라미터
이 likelihood에
(2) Negative Loss Likelihood
PyTorch를 사용하다 보면 CrossEntropy(), NLLloss()가 모두 구현되어 있는 것을 볼 수 있다. 근데 NLLloss를 찾아보면 수식만 나와있고 정의가 없다(위키에도 없다).
위의 likelihood의 정의를 통해 이를 유추해볼 수 있다. 먼저 우리가 원하는 것은 관찰된 분포
이때 KL Divergence를 최소화 하는 것은 두 확률분포의 Cross Entropy를 최소화하는 것과 동일하다. 그리고 likelihood 정의에 따라 Cross Entropy를 최소화하는 것은 Negative Loss Likelihood를 최소화하는 것과 동일하다.
PyTorch 구현 부분을 살펴보면 CrossEntropyLoss를 사용할 때는 출력값 logit을 그대로 입력으로 받아 Cross Entropy값을 계산하고, NLLloss를 사용할 때는 출력값 log에 log softmax를 씌우고 Cross Entropy를 계산한다(참고).
'Pattern Recognition' 카테고리의 다른 글
5. Density Estimation (0) | 2022.09.12 |
---|---|
4. Clustering (2) (2) | 2022.09.12 |
4. Clustering (1) (0) | 2022.09.11 |
3. Maximum Likelihood Estimation(MLE) (0) | 2022.08.06 |
(참고) Gaussian Distribution (0) | 2022.08.06 |