# Passing the weights to CrossEntropyLoss correctly

**URL:** <https://discuss.pytorch.org/t/passing-the-weights-to-crossentropyloss-correctly/14731>\
**Category:** Uncategorized\
**Created:** [March 10, 2018, 10:05pm UTC](https://discuss.pytorch.org/t/passing-the-weights-to-crossentropyloss-correctly/14731 "2018-03-10T22:05:18Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [May 7, 2019, 10:41am UTC](https://discuss.pytorch.org/t/passing-the-weights-to-crossentropyloss-correctly/14731/10 "2019-05-07T10:41:42Z")

</div>

Small addition to @MariosOreo’s answer:  
if your loss function uses `reduction='mean'`, the loss will be normalized by the sum of the corresponding weights for each element. If you are using `reduction='none'`, you would have to take care of the normalization yourself.  
Here is a small example:

```python
x = torch.randn(10, 5)
target = torch.randint(0, 5, (10,))

weights = torch.tensor([1., 2., 3., 4., 5.])
criterion_weighted = nn.CrossEntropyLoss(weight=weights)
loss_weighted = criterion_weighted(x, target)

criterion_weighted_manual = nn.CrossEntropyLoss(weight=weights, reduction='none')
loss_weighted_manual = criterion_weighted_manual(x, target)
loss_weighted_manual = loss_weighted_manual.sum() / weights[target].sum()

print(loss_weighted == loss_weighted_manual)

```

---

_[View the full topic](https://discuss.pytorch.org/t/passing-the-weights-to-crossentropyloss-correctly/14731)._
