AI/PyTorch

Softmax 결과의 총합이 1 이하로 나오는 경우

검정비니 2022. 7. 18. 19:02
728x90
반응형

만약 softmax를 썼는데 결과의 총합이 1이 아닐 경우, 이는 softmax를 사용한 위치와 loss 함수의 특성을 다시 한번 살펴봐야 한다.

 

대표적인 예로, 만약 loss 함수로 CrossEntropyLoss를 쓰는데 모델이 output을 logit이 아니라 softmax의 결과 값을 쓴다면 이와 같은 문제가 발생할 수 있다. PyTorch에서 CrossEntropyLoss는 내부적으로 log_softmax와 NLLLoss를 사용하며, input 값이 probability가 아닌 logit 값이 들어오기를 기대한다. 따라서, softmax와 log_softmax의 중복으로 인해 위와 같은 문제가 발생할 수 있다. 당연히 이는 학습을 불안정하게 하는 원인이 된다.

 

참고: https://discuss.pytorch.org/t/softmax-not-summing-to-1/58526/6

 

Softmax not summing to 1

Unrelated to your question, but note that nn.CrossEntropyLoss expects logits as the model output not probabilities coming from softmax. Internally F.log_softmax and nn.NLLLOSS will be used so you can just remove the softmax as the output activation. Also n

discuss.pytorch.org

 

반응형