[ML] Softmax function

2022. 5. 12. 19:03·.../Math
728x90
728x90

Softmax 함수란?

ML 에서 multiclass classification을 수행하는 모델의 최종 output을 출력하는 activation함수로 사용되는 함수.

  • Softmax regression (=multinomial logistic regression)에서 사용됨.
  • Logistic Function의 Generalization임.

2022.06.06 - [Computer] - [ML] Logit에서 Logistic Function.

 

[ML] Logit에서 Logistic Function.

다음이 바로 logistic function임. $$\text{logistic}(t)=\sigma(t)=\frac{1}{1+e^{-t}}$$ Sigmoid functions 중에서 가장 유명한 것이다보니 sigmoid라고도 불림. MLP에서 back-propagation이 적용되면서 activation function으로 사용

dsaint31.tistory.com

다른 이름으로는 softargmax 함수, 또는 normalized exponential 함수라고도 불림.

  • $-\infty ~+\infty$ 를 range로 하는 regression score 들을 element로 하는 vector를 입력으로 받음.
  • 출력은 $0~1$의 range로 하는 probability들을 element가지는 vector 임.
  • 입력 vector와 출력 vector는 같은 dimension임 (=element의 수가 같음).


정의식은 다음과 같음.

$$\boldsymbol{\sigma}: \mathbb{R}^n \rightarrow [0.0,1.0]^n$$

$$\displaystyle \sigma(\vec{x})_i=\displaystyle {\dfrac{e^{x_i}}{\sum_{i=1}^n e^{x_i}}}$$

where

  • $[0.0,1.0]$ 은 0.0~1.0 사이의 closed iterval (즉, 0이상 1이하 의 실수들)을 의미.
  • $\mathbb{R}^n$ : Real number를 element로 가지는 $n$-dimensional vector들의 vector space.
  • $[0.0,1.0]^n$ : 0~1 사이의 실수를 element로 가지는 $n$-dimensional vector들의 vector space.
  • $n$ : input vector $\vec{x}$의 element수.
  • $x_i$ : input vector $\vec{x}$의 $i$-th element.
  • $\sigma(\vec{x})_i$ : output vector $\sigma$의 $i$-th element.

Cross Entropy와 함께 Multi-class Classification에서 사용됨.

softmax regression의 경우,

  • $\vec{x}$의 각 element는
  • 해당하는 class에 속할 확률에 대한 logit score (or logits, log-odds, unnormalized log-odds)임.

 

softmax는 모든 클래스에 대한 logit score의 exponential들을 합하고 이로 나누어주는 normalization을 수행하는 형태로써, multi-class classification에 사용가능함.

 

주의할 건, softmax function의 출력 vector는
exclusive한 multi-class classification에서 사용가능하다는 점임.

 

즉, 여러 class 중 하나의 클래스에만 속하는 형태의 task만 가능하지,

multi-output classification(:multi label classification 포함)처럼

하나의 case가 동시에 2개의 클래스가 되는 형태의 task에선 사용할 수 없음.

(이 같은 경우엔, 여러 출력단을 각 class여부를 판정하고 각 출력단의 output을 독립된 logistic function으로 처리해주어야 함.)

 

즉, 최종 class에 대한 estimation은 다음과 같다.

$$\hat{y}= \displaystyle \underset{k}{\operatorname{arg max}} \sigma(\textbf{x})_k$$

  • $\displaystyle \underset{k}{\operatorname{argmax}}$ operator(연산자)는 뒤의 함수(위의 식에서는 softmax $\sigma$)를 최대화하는 변수 $k$의 값을 반환함.
  • 즉 $k$가 각 클래스에 나타내는 값이므로 $\hat{y}$는 softmax로 나오는 출력 벡터에서 가장 큰 element에 해당하는 class를 나타내는 숫자를 값으로 가짐.

softmax를 출력으로 가지는 경우, cost function은 cross entropy임.

 

TensorFlow에서

  • binary classification의 경우엔 logistic function과 binary cross entropy조합이 이용되고,
  • multi-class classification의 경우엔 softmax와 categorical cross entropy조합이 이용됨.

PyTorch에서:

  • binary classification의 경우엔 logistic function(torch.sigmoid 함수)과 Binary Cross Entropy 조합이 이용됨 (nn.BCELoss 또는 nn.BCEWithLogitsLoss).
  • multi-class classification의 경우엔 Softmax와 Categorical Cross Entropy 조합인 nn.CrossEntropyLoss (logit 입력) 을 사용함
    • nn.LogSoftmax (logit 입력, log+probability vector 출력) 와
    • nn.NLLLoss (log probability vector 입력) 와 조합하여 사용하기도 함.​​​​​​​​​​​​​​​​

같이보면 좋은 자료들

2022.05.12 - [.../Math] - [Math] Cross Entropy

 

[Math] Cross Entropy

Cross Entropy란 두 probability distribution $p$, $q$ 사이의 dissimilarity(차이)를 정량화하는 지표 로 사용됨. 엄밀하게는 dissimilarity는 KL-Divergence로 구해야 하나, Cross entropy로 해도 큰 문제없기..

dsaint31.tistory.com

https://ds31x.tistory.com/287

 

[DL] Classification 을 위한 Activation Func. 와 Loss Func: PyTorch

PyTorch: Classification에서의 Output Func(~Activation Func.)와 Loss Func. 요약PyTorch는 다양한 종류의 loss function(손실 함수)와 activation function(활성화 함수)를 제공하는데,이 중에서 classification task를 수행하는

ds31x.tistory.com

 

'... > Math' 카테고리의 다른 글

[Math] Orthogonal Projection (정사영)  (0) 2022.05.19
[Math] Distance between Point and Plane : 점과 직선의 거리  (1) 2022.05.19
[Math] Entropy 란 (평균정보량, 정보량의 기댓값)  (0) 2022.05.12
[Math] Cross Entropy  (0) 2022.05.12
[Math] Kullback-Leibler Divergence  (1) 2022.05.12
'.../Math' 카테고리의 다른 글
  • [Math] Orthogonal Projection (정사영)
  • [Math] Distance between Point and Plane : 점과 직선의 거리
  • [Math] Entropy 란 (평균정보량, 정보량의 기댓값)
  • [Math] Cross Entropy
dsaint31x
dsaint31x
    반응형
    250x250
  • dsaint31x
    Dsaint31's blog
    dsaint31x
  • 전체
    오늘
    어제
    • 분류 전체보기 (752)
      • Private Life (13)
      • Programming (56)
        • DIP (112)
        • ML (26)
      • Computer (119)
        • CE (53)
        • ETC (33)
        • CUDA (3)
        • Blog, Markdown, Latex (4)
        • Linux (9)
      • ... (355)
        • Signals and Systems (107)
        • Math (172)
        • Linear Algebra (33)
        • Physics (42)
        • 인성세미나 (1)
      • 정리필요. (54)
        • 의료기기의 이해 (6)
        • PET, MRI and so on. (1)
        • PET Study 2009 (1)
        • 방사선 장해방호 (4)
        • 방사선 생물학 (3)
        • 방사선 계측 (9)
        • 기타 방사능관련 (3)
        • 고시 (9)
        • 정리 (18)
      • RI (0)
      • 원자력,방사능 관련법 (2)
  • 블로그 메뉴

    • Math
    • Programming
    • SS
    • DIP
  • 링크

    • Convex Optimization For All
  • 공지사항

    • Test
    • PET Study 2009
    • 기타 방사능관련.
  • 인기 글

  • 태그

    cv2
    numpy
    fourier transform
    signal_and_system
    Optimization
    DIP
    Python
    signals_and_systems
    Convolution
    opencv
    function
    Probability
    math
    인허가제도
    Term
    SS
    Vector
    linear algebra
    Programming
    SIGNAL
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dsaint31x
[ML] Softmax function
상단으로

티스토리툴바