Answers.Body: <p>I believe that there is no &quot;best formula&quot; here, as there are many <a href="https://torchmetrics.readthedocs.io/en/v0.8.0/classification/calibration_error.html" rel="nofollow noreferrer">Calibration metrics</a> out there, depending on what you want to calibrate. <a href="https://arxiv.org/abs/1706.04599" rel="nofollow noreferrer">This paper</a> introduces three metrics for different purposes:</p>
  <ul>
  <li>Expected Calibration Error (ECE): provides a single scalar summary of calibrations.</li>
  <li>Maximum Calibration Error (MCE): use when we wish to minimize the worst-case deviation between confidence and accuracy</li>
  <li>Negative log likelihood (NLL): this is the same as Cross-entropy loss.</li>
  </ul>
  <p>There is also a <a href="https://arxiv.org/abs/1904.01685" rel="nofollow noreferrer">related paper</a> about more metrics.</p>
  <p>Just like Accuracy, F1, and ROC-AUC, Calibration metric should depend on the use case.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-19T06:47:15.600Z
Answers.LastActivityDate: 2023-01-19T06:47:15.600Z
Answers.UserDisplayName: Minh-Long Luu
Answers.Body: <p>I assume you want a model that uses the <a href="https://en.wikipedia.org/wiki/Softmax_function" rel="nofollow noreferrer">Softmax</a> as the output layer.</p>
  <p>Basically, the Softmax will produce a set of probabilities that all sum up to 1. So if you have three classes in your data the Softmax will produce these confidence values by default, even though this is not exactly its main functionality.</p>
  <p>The Softmax is commonly used on multiclass data.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-28T19:39:24.307Z
Answers.LastActivityDate: 2021-04-28T19:39:24.307Z
Answers.UserDisplayName: Marcus
Answers.Body: <p>Obvious answer for a binary (2 classes)classification is .5. Beyond that the earlier comment is correct. One of the things I have seen done is to run your model on the test set and save the prediction probability results.  Then create a threshold variable call it thresh. Then increment thresh from 0 to 1 in a loop. On each iteration compare thresh with the highest predicted probability prediction call it P. If  P>thresh declare that as the selected prediction, then compare that with the true prediction. Keep track of the errors for each value of thresh. At the end select the value of thresh that has the least errors. 
  There are also some  more sophisticated methods for example "top 2 accuracy" where thresh is selected based on  having the true class within either the prediction with the highest probability or the second highest probability . You can construct a weighted error function and select the value of thresh that has the net lowest error over the test set.
  For example an error function might be as follows. If neither P(highest) or P(second highest) = True class, error=1. If P(second highest) = true class, error=.5. If p(highest)=true class error=0. I have never tried this myself so I am not sure how well this works. When I get some time will try it on a model with 100 classes and see how well it does. I know in the Imagenet competition they evaluate not just the top accuracy but also the "Top 3" and "Top 5' accuracy. In that competition there are 1000 classes.
  I never thought of this before but I assume you could train your model specifically to optimize say the Top 2 accuracy by constructing a loss function used during training that forces the network to minimize this loss.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-03T19:18:01.613Z
Answers.LastActivityDate: 2020-03-03T19:18:01.613Z
Answers.UserDisplayName: Gerry P
Answers.Body: <p>Maybe its possible to use Entropy to evaluate confidence?</p>
  <pre><code>import torch
  
  def entropy(cls_prob:torch.tensor)-&gt;torch.tensor:
      &quot;&quot;&quot;
      Calculate entropy
      The larger the number, the lower the confidence level.
  
      Args:
          cls_prob (torch.tensor): The output score of the AI ​​model, 
          the expected input shape is (batch size, number of categories)
  
      Returns:
          torch.tensor: entropy of each sample
      &quot;&quot;&quot;
      score = -torch.sum(torch.log(cls_prob)*cls_prob, dim=1)
      return score
  </code></pre>
  <pre><code>prob_score = torch.tensor([[0.9,0.05,0.05], [0.4,0.45,0.15]])
  confidence = entropy(prob_score)
  </code></pre>
  <p>and the confidence be like <strong>tensor([0.3944, 1.0104])</strong></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-03T05:57:12.337Z
Answers.LastActivityDate: 2024-01-03T05:57:12.337Z
Answers.UserDisplayName: 陳敏涵
Body: <p>I'm training a classifier and I want to collect incorrect outputs for human to double check.</p>
  
  <p>the output of the classifier is a vector of probabilities for corresponding classes. for example, [0.9,0.05,0.05]</p>
  
  <p>This means the probability for the current object being class A is 0.9, whereas for it being the class B is only 0.05 and 0.05 for C too.</p>
  
  <p>In this situation, I think the result has a high confidence. As A's probability dominants B's and C's.</p>
  
  <p>In another case, [0.4,0.45,0.15], the confidence should be low, as A and B are close. </p>
  
  <p>What's the best formula to use to calculate this confidence?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-03T05:46:22.527Z
Comments.Text: There's no best formula, this is heuristic based. It depends on what accuracy you're looking for, but if you want a place to start, consider anything `> 0.85` in the correct class a confident prediction, anything between `0.3` and `0.85` low confidence, and anything beneath `0.3` wrong
Comments.UserDisplayName: Recessive
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-03T19:39:31.107Z
Comments.Text: If you have a well-calibrated method that does indeed output probabilities, your problem is already solved. In the first instance, the classifier says there's a 90% probability that the object belongs to class A, but in the second instance, it's only 45% sure that it belongs to class B. What more do you want?
Comments.UserDisplayName: Nuclear Hoagie
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-03-29T16:00:32.160Z
Comments.Text: After your human curators have marked a sample of the output, you can use https://scikit-learn.org/stable/modules/calibration.html
Comments.UserDisplayName: chrishmorris
Tags: <classification>
Title: How to calculate the confidence of a classifier's output?
UserDisplayName: Bill Yan
ThreadText: # How to calculate the confidence of a classifier's output?
  
  (Asked by: username_0 on 2020-03-02)
  
  I'm training a classifier and I want to collect incorrect outputs for human to double check.
  
  the output of the classifier is a vector of probabilities for corresponding classes. for example, [0.9,0.05,0.05]
  
  This means the probability for the current object being class A is 0.9, whereas for it being the class B is only 0.05 and 0.05 for C too.
  
  In this situation, I think the result has a high confidence. As A's probability dominants B's and C's.
  
  In another case, [0.4,0.45,0.15], the confidence should be low, as A and B are close.
  
  What's the best formula to use to calculate this confidence?
  
  **Tags:** classification
  
  > **Comments:**
  > **username_1**: There's no best formula, this is heuristic based. It depends on what accuracy you're looking for, but if you want a place to start, consider anything `> 0.85` in the correct class a confident prediction, anything between `0.3` and `0.85` low confidence, and anything beneath `0.3` wrong  
  > **username_2**: If you have a well-calibrated method that does indeed output probabilities, your problem is already solved. In the first instance, the classifier says there's a 90% probability that the object belongs to class A, but in the second instance, it's only 45% sure that it belongs to class B. What more do you want?  
  > **username_3**: After your human curators have marked a sample of the output, you can use https://scikit-learn.org/stable/modules/calibration.html  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_4 on 2023-01-19. Score: 1
  
  I believe that there is no "best formula" here, as there are many [Calibration metrics](https://torchmetrics.readthedocs.io/en/v0.8.0/classification/calibration_error.html) out there, depending on what you want to calibrate. [This paper](https://arxiv.org/abs/1706.04599) introduces three metrics for different purposes:
  
  * Expected Calibration Error (ECE): provides a single scalar summary of calibrations.
  * Maximum Calibration Error (MCE): use when we wish to minimize the worst-case deviation between confidence and accuracy
  * Negative log likelihood (NLL): this is the same as Cross-entropy loss.
  
  There is also a [related paper](https://arxiv.org/abs/1904.01685) about more metrics.
  
  Just like Accuracy, F1, and ROC-AUC, Calibration metric should depend on the use case.
  
  
  ---
  
  ### Answer by username_5 on 2021-04-28. Score: 0
  
  I assume you want a model that uses the [Softmax](https://en.wikipedia.org/wiki/Softmax_function) as the output layer.
  
  Basically, the Softmax will produce a set of probabilities that all sum up to 1. So if you have three classes in your data the Softmax will produce these confidence values by default, even though this is not exactly its main functionality.
  
  The Softmax is commonly used on multiclass data.
  
  
  ---
  
  ### Answer by username_6 on 2020-03-03. Score: 0
  
  Obvious answer for a binary (2 classes)classification is .5. Beyond that the earlier comment is correct. One of the things I have seen done is to run your model on the test set and save the prediction probability results. Then create a threshold variable call it thresh. Then increment thresh from 0 to 1 in a loop. On each iteration compare thresh with the highest predicted probability prediction call it P. If P>thresh declare that as the selected prediction, then compare that with the true prediction. Keep track of the errors for each value of thresh. At the end select the value of thresh that has the least errors.
  There are also some more sophisticated methods for example "top 2 accuracy" where thresh is selected based on having the true class within either the prediction with the highest probability or the second highest probability . You can construct a weighted error function and select the value of thresh that has the net lowest error over the test set.
  For example an error function might be as follows. If neither P(highest) or P(second highest) = True class, error=1. If P(second highest) = true class, error=.5. If p(highest)=true class error=0. I have never tried this myself so I am not sure how well this works. When I get some time will try it on a model with 100 classes and see how well it does. I know in the Imagenet competition they evaluate not just the top accuracy but also the "Top 3" and "Top 5' accuracy. In that competition there are 1000 classes.
  I never thought of this before but I assume you could train your model specifically to optimize say the Top 2 accuracy by constructing a loss function used during training that forces the network to minimize this loss.
  
  
  ---
  
  ### Answer by username_7 on 2024-01-03. Score: 0
  
  Maybe its possible to use Entropy to evaluate confidence?
  
  ```
  import torch
  
  def entropy(cls_prob:torch.tensor)->torch.tensor:
      """
      Calculate entropy
      The larger the number, the lower the confidence level.
  
      Args:
          cls_prob (torch.tensor): The output score of the AI ​​model, 
          the expected input shape is (batch size, number of categories)
  
      Returns:
          torch.tensor: entropy of each sample
      """
      score = -torch.sum(torch.log(cls_prob)*cls_prob, dim=1)
      return score
  
  ```
  
  ```
  prob_score = torch.tensor([[0.9,0.05,0.05], [0.4,0.45,0.15]])
  confidence = entropy(prob_score)
  
  ```
  
  and the confidence be like **tensor([0.3944, 1.0104])**
  
  
  ---
  
  