# BCELoss and CrossEntropyloss behaviour

**URL:** <https://discuss.pytorch.org/t/bceloss-and-crossentropyloss-behaviour/196727>\
**Category:** Uncategorized\
**Created:** [February 6, 2024, 7:30pm UTC](https://discuss.pytorch.org/t/bceloss-and-crossentropyloss-behaviour/196727 "2024-02-06T19:30:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sai1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sai1/32/62072_2.png) [@Sai1](https://discuss.pytorch.org/u/Sai1)\
**Post date:** [February 6, 2024, 7:30pm UTC](https://discuss.pytorch.org/t/bceloss-and-crossentropyloss-behaviour/196727/1 "2024-02-06T19:30:21Z")

</div>

Hi all, my problem is a binary class classification. when I’m training my model with BCELoss and sigmoid. I’m getting nan in second epoch right away. However, when I changed my loss to Crossentropy, the model is not outputting any nan’s. Can anyone explain what’s going on. if I should only be using BCELoss, can you suggest If I can find out what’s causing nan’s.  
Thank you

---

<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:** [February 6, 2024, 7:49pm UTC](https://discuss.pytorch.org/t/bceloss-and-crossentropyloss-behaviour/196727/2 "2024-02-06T19:49:26Z")

</div>

> [@Sai1](#):
>
> if I should only be using BCELoss, can you suggest If I can find out what’s causing nan’s.

Remove the `sigmoid` and use `nn.BCEWithLogitsLoss` for better numerical stability and check if you would still see the NaN outputs.
