<p>Cross entropy can also be weighted, standard practice when training on imbalanced datasets. Instead of the classic formulation</p>
<p><span class="math-container">$$C = -\sum_{i=1}^{M}y_i\text{log}(\hat{y}_i)$$</span></p>
<p>you can rewrite it as</p>
<p><span class="math-container">$$C = -\sum_{i=1}^{M}\sum_{c=1}^{N}w_cy_i\text{log}(\hat{y}_i)$$</span></p>
<p>were <span class="math-container">$w_c$</span> represent the weight of each training class. So if you care mostly about the accuracy of a specific class you could boost that specific class by using a large weight for it, let's say 0.8, and 0.2/(n-1) for all the remaining classes.</p>

CC BY-SA 4.0
2022-07-19T18:20:00.490Z
*were* → *[where](http://www.wikihow.com/Use-Where,-Were-and-We're)*
Peter Mortensen
CC BY-SA 4.0
2022-07-19T18:20:14.403Z
The formula here seems to show sum(w_c) as a separate constant factor that can be factorized out
Criticize SE actions means ban
CC BY-SA 4.0
2022-07-19T09:15:24.790Z
2022-07-19T09:15:24.790Z
Edoardo Guerriero
<p>CrossEntropyLoss optimizes the overall classification accuracy as
<span class="math-container">$$ {n_{\text{correct}} \over N} $$</span></p>
<p>What loss function should I use if I only care about increasing the <strong>true positive rate</strong> of one class?</p>
<p><span class="math-container">$$ {n_{\text{true A in predicted A}} \over N_{\text{predicted A}} } $$</span></p>
<p>For example, I predict 100 images to be in class A, and 90 out of this 100 are truly A. So the accuracy is 90%.</p>
<p>In the meantime, I predict another 900 images to be in class B, but 500 of them are actually A, and only 400 are B. So the overall accuracy is (90+400)/(100+900) = 49%.</p>
<p>In the meantime, I don't want <span class="math-container">$ N_{\text{predicted A}} $</span> to be too small, since one can see from above that a smaller <span class="math-container">$ N_{\text{predicted A}} $</span> can likely lead to larger true positive rate.</p>

<objective-functions>
What loss function should I use if I only care about the accuracy of one class?
em1971
# What loss function should I use if I only care about the accuracy of one class?

(Asked by: username_0 on 2022-07-19)

CrossEntropyLoss optimizes the overall classification accuracy as
$$ {n\_{\text{correct}} \over N} $$

What loss function should I use if I only care about increasing the **true positive rate** of one class?

$$ {n\_{\text{true A in predicted A}} \over N\_{\text{predicted A}} } $$

For example, I predict 100 images to be in class A, and 90 out of this 100 are truly A. So the accuracy is 90%.

In the meantime, I predict another 900 images to be in class B, but 500 of them are actually A, and only 400 are B. So the overall accuracy is (90+400)/(100+900) = 49%.

In the meantime, I don't want $ N\_{\text{predicted A}} $ to be too small, since one can see from above that a smaller $ N\_{\text{predicted A}} $ can likely lead to larger true positive rate.

**Tags:** objective-functions


---

## 1 answers

### Answer by username_1 on 2022-07-19. Score: 3 | Accepted answer

Cross entropy can also be weighted, standard practice when training on imbalanced datasets. Instead of the classic formulation

$$C = -\sum\_{i=1}^{M}y\_i\text{log}(\hat{y}\_i)$$

you can rewrite it as

$$C = -\sum\_{i=1}^{M}\sum\_{c=1}^{N}w\_cy\_i\text{log}(\hat{y}\_i)$$

were $w\_c$ represent the weight of each training class. So if you care mostly about the accuracy of a specific class you could boost that specific class by using a large weight for it, let's say 0.8, and 0.2/(n-1) for all the remaining classes.

> **Comments:**
> **username_2**: *were* → *[where](http://www.wikihow.com/Use-Where,-Were-and-We're)*  
> **username_3**: The formula here seems to show sum(w_c) as a separate constant factor that can be factorized out  

---

