temperature_scaling icon indicating copy to clipboard operation
temperature_scaling copied to clipboard

How to use temperature scaling for binary classification using sigmoid?

Open evapachetti opened this issue 3 years ago • 1 comments

My network is trained according to a binary classification approach, so the model outputs as a single logit value which I then convert into probability by applying the sigmoid function. How can I modify the temperature scaling code to apply it to my network?

Thank you in advance.

evapachetti avatar Jun 14 '22 11:06 evapachetti

I think the following approach should work:

  1. Replace the CrossEntropyLoss with BCEWithLogitsLoss
  2. Change the size of self.temperature to number of classes

Sigmoid can be regarded as a special case of softmax where one of the logits is 0: $\frac{e^x}{e^0 + e^x}$. Then we only need to learn the temperature for each class.

chenxia-han avatar Nov 23 '22 13:11 chenxia-han