gpleiss / temperature_scaling

A simple way to calibrate your neural network.
MIT License
1.09k stars 159 forks source link

How to use temperature scaling for binary classification using sigmoid? #32

Open evapachetti opened 2 years ago

evapachetti commented 2 years ago

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.

xchani commented 1 year ago

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.