# Cross Entropy and Binary Cross Entropy do not give the same result

**URL:** <https://discuss.pytorch.org/t/cross-entropy-and-binary-cross-entropy-do-not-give-the-same-result/52654>\
**Category:** Uncategorized\
**Created:** [August 6, 2019, 9:38am UTC](https://discuss.pytorch.org/t/cross-entropy-and-binary-cross-entropy-do-not-give-the-same-result/52654 "2019-08-06T09:38:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haydnspass](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/haydnspass/32/3780_2.png) [@Haydnspass](https://discuss.pytorch.org/u/Haydnspass)\
**Post date:** [August 6, 2019, 9:38am UTC](https://discuss.pytorch.org/t/cross-entropy-and-binary-cross-entropy-do-not-give-the-same-result/52654/1 "2019-08-06T09:38:28Z")

</div>

Hey,  
Until now I used Binary Cross entropy loss but since I need to use some other loss function I need to change my output so that it conforms to the Cross Entropy format. The result should be exactly the same, right?

When I tried a fake / handcrafted example I do not get the same results for both of the loss functions, probably I am just overseeing something …  
Suppose in binary format my ground truth is [1., 0.] and my prediction [0.5, 0], then in normal CE format my ground truth should be the same (but with integers since those are now class indices, so [1 0]). My prediction however should be for the first element: [0.5, 0.5] and for the second element [1.0, 0.0] right?

Below you can find it in a jupyter notebook.

Cheers,  
Lucas

 ![image](https://discuss.pytorch.org/uploads/default/original/2X/f/f1d037b95e41eace989fea2ed1956f53cdccdb29.png)

---

<div class="post-metadata">

**Author:** ![InnovArul](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/innovarul/32/5282_2.png) [@InnovArul](https://discuss.pytorch.org/u/InnovArul)\
**Post date:** [August 6, 2019, 11:46am UTC](https://discuss.pytorch.org/t/cross-entropy-and-binary-cross-entropy-do-not-give-the-same-result/52654/2 "2019-08-06T11:46:05Z")

</div>

Remember that cross entropy loss = ‘log softmax’ + ‘negative log likelihood’.

Hence, you may need to set  
`prediction[1,1]=float("-inf")`
