Loss(Cost)

Learning 이라는 것은 “Loss”를 “Optimization” 하는 것.
model의 output이랑 target(label)이 최대한 비슷할 때 이 값이 작아지는 방향으로 보통 define함.
확률 분포 간 차이로 볼 수도 있다.

  • classification task에서는 마지막 layer가 분류할 target class 개수랑 dim이 같은데, 이를 softmax하면 확률 분포가 model의 최종 output이라고 볼 수 잇다.
  • 따라서 learning은 output probability distribution과 target probability distribution을 최대한 비슷하게 해주는 방향으로 진행되어야 한다.
  • 확률 분포 차이를 나타내는 지표로 KL-divergence라는 지표를 쓸 수 있고, metric 조건을 정확히 만족하진 못하지만, 대략 distance 개념 정도로 이해할 수 있다.
    • d > 0, d(x, x)=0 → x=0, triangular-ineq

How to design a good loss function?


Abstract

  • A loss function can be any differentiable function that we wish to optimize
  • Deriving the cost function from the Maximum Likelihood Estimation (MLE) principle removes the burden of manually designing the cost function for each model.
  • Consider the output of the neural network as parameters of a distribution over



(: log-likelihoood 로 변환)

Regression Loss


Regression - estimation target

regression이란, 결국

즉, output은 real-value 한 개.

  • regression을 수행하는 모델이 예측하는 에러 Gaussian distribution으로 보자!
    • target 값은 gaussian의 mean value.

Classification Loss


Classification - estimation target

classification이란, 결국

즉, output은 real-value vector.

  • classification을 수행하는 모델이 예측하는 것은
    • 2 classes: regression with sigmoid로 확률을 바로 뽑던지, 아니면 softmax를 사용.
      • BCE(Binary Cross-Entropy) Loss
    • more than 2: 일반적으로 softmax
      • CE(Cross-Entropy) Loss

Softmax - Logistic regression


Logistic regression

Logistic regression

logistic regression의 경우, linear regression의 output을 확률값 범위로 고정해주는 activation function(tanh)을 적용한 걸로 볼 수도 있는데, classification task에 수행할 수 있다는 점을 잘 기억하자!
→ ouput 값이 확률 값의 범위에 무조건 떨어지니까.
수식으로는 아래와 같이 model 을 define하고,

model의 output을 posterior 해석.

h(\mathrm{x}) & \text{if}\; y = 1 \\ 1 - h(\mathrm{x}) & \text{if}\; y = 0 \end{cases}$$ 이걸 좀 compact 하게 한 줄로 써보면, $$P(y|\mathrm{x}) = a^y(1-a)^{(1-y)}$$
원본 링크

Expand to BCE

위의 식에서 여러 개의 데이터가 i.i.d. 가정을 통해 뽑혔다고 한다면,

여기서, MLE



where
실제 compute 할 때에는 log 씌우는 게 값이 stable하여 log를 씌운 이후 계산한다고 하는데 정확히 어떠한 말일까..?



또한, 값을 maximizing 하는 것 보다는 minimizing 하는게 편하기 때문에(??) negative log-likelihood를 minimizing 한다.

Expand to Multiple Classes

Categorical distribution을 다음과 같이 정의

따라서 probability distribution은

로 표현될 수 있고,
: One-hot vector

CE(Cross-Entropy) Loss?

Let
then we obtain,

In other words, we minimize the cross-entropy loss(CE loss).
The target is a One-hot vector with its c’th element.

CE-Loss