# CrossEntropyLoss() function in PyTorch

**URL:** <https://discuss.pytorch.org/t/crossentropyloss-function-in-pytorch/138947>\
**Category:** Uncategorized\
**Created:** [December 9, 2021, 12:58pm UTC](https://discuss.pytorch.org/t/crossentropyloss-function-in-pytorch/138947 "2021-12-09T12:58:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shar](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/e274bd/32.png) [@Shar](https://discuss.pytorch.org/u/Shar)\
**Post date:** [December 9, 2021, 12:58pm UTC](https://discuss.pytorch.org/t/crossentropyloss-function-in-pytorch/138947/1 "2021-12-09T12:58:50Z")

</div>

Hello,  
I tried to search for this question in the internet, but I didn’t find a strict answer. I’m confused.

How is the cross entropy loss is calculated using torch.nn.CrossEntropyLoss() ?

Is it the sum of log probabilities of the correct class? or is it the sum of log probabilities of the correct class + log of (1 -probabilities) of the wrong classes?

Because in the first case it will a negative log likelihood if I get it correctly.

Thanks

---

<div class="post-metadata">

**Author:** ![mMagmer](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/mmagmer/32/43283_2.png) [@mMagmer](https://discuss.pytorch.org/u/mMagmer)\
**Post date:** [December 9, 2021, 2:13pm UTC](https://discuss.pytorch.org/t/crossentropyloss-function-in-pytorch/138947/2 "2021-12-09T14:13:28Z")

</div>

[https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html](https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html)

> Note that this case is equivalent to the combination of [`LogSoftmax`](https://pytorch.org/docs/stable/generated/torch.nn.LogSoftmax.html#torch.nn.LogSoftmax) and [`NLLLoss`](https://pytorch.org/docs/stable/generated/torch.nn.NLLLoss.html#torch.nn.NLLLoss).

---

<div class="post-metadata">

**Author:** ![Shar](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/e274bd/32.png) [@Shar](https://discuss.pytorch.org/u/Shar)\
**Post date:** [December 9, 2021, 3:38pm UTC](https://discuss.pytorch.org/t/crossentropyloss-function-in-pytorch/138947/3 "2021-12-09T15:38:25Z")

</div>

Thanks for your reply!

I just wonder, what is the weight `w_y_n` that is multiplied with the log probability?

 ![Screenshot 2021-12-09 at 5.39.13 PM](https://discuss.pytorch.org/uploads/default/original/3X/3/f/3fbd2df9a8891d24728a31552f2f8be61ad79a48.png)

---

<div class="post-metadata">

**Author:** ![Eta\_C](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/eta_c/32/17667_2.png) [@Eta\_C](https://discuss.pytorch.org/u/Eta_C)\
**Post date:** [December 10, 2021, 2:06am UTC](https://discuss.pytorch.org/t/crossentropyloss-function-in-pytorch/138947/4 "2021-12-10T02:06:25Z")

</div>

```python
torch.nn.CrossEntropyLoss(weight=None, size_average=None, ignore_index=- 100, reduce=None, reduction='mean', label_smoothing=0.0)

```

Note that `CrossEntropyLoss` has an option `weight=None` and

> - **weight** ([_Tensor_](https://pytorch.org/docs/stable/tensors.html#torch.Tensor)_,_ _optional_) – a manual rescaling weight given to each class. If given, has to be a Tensor of size C
