본문 바로가기
DL

[Pytorch] Multi-label classification Loss Selection

by YGSEO 2021. 2. 22.
728x90

discuss.pytorch.org/t/is-there-an-example-for-multi-class-multilabel-classification-in-pytorch/53579/9

 

Is there an example for multi class multilabel classification in Pytorch?

nn.CrossEntropyLoss uses the target to index the logits in your model’s output. Thus it is suitable for multi-class classification use cases (only one valid class in the target). nn.BCEWithLogitsLoss on the other hand treats each output independently and

discuss.pytorch.org

 

Multilabel 같은 경우에는 과거에 Kaggle에서 Bengali 손글씨 대회에 참여한 경험이 있다.

많이 잊어버렸기 때문에 다시 상기시킬겸 코드 재정리겸 참여 결심


Loss를 어떤걸 써야하나 찾아보다 발견한 discussion thread.

우선은 multilabel 이기 때문에 CrossEntrophy를 사용할 경우 target에 대한 index의 logit값을 사용하기 때문에 적절하지 않고

대신 BCEwithLogitLoss를 추천하고 있다. 그 이유는 BCELogitLoss같은 경우 각각의 output을 독립적으로 다루기 때문에 multilabel에 적합하기 때문.

 

그렇다면, BCEwithLogitLoss가 로 Loss fn을 선택하면 될거 같은데,

 

여기서 MultiLabelSoftMarginLoss와는 어떤 차이가 있는 가

" The main difference is, that the loss will be averaged over the feature dimension "

끝.


결론 BCEwithLogitLoss를 사용하면된다.

 

그렇다면 BCE와 BCEwithLogitLoss는 무슨 차이가 존재하는가.

 

"While nn.BCEWithLogitsLoss will apply it internally for you, you should add it manually if you are using nn.BCELoss."

 

즉, BCELoss를 사용할거라면 output(예측값)에 sigmoid를 적용한 후 LossFn에 넣어서 계산해야하고

BCEwithLogitLoss를 쓴다면, 그냥 LossFn에 넣으면 internally applied.

 

 

discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/2

728x90

댓글