# BCELoss vs BCEWithLogitsLoss

**URL:** <https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586>\
**Category:** Uncategorized\
**Created:** [January 2, 2019, 11:18am UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586 "2019-01-02T11:18:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shani\_Gamrian](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/shani_gamrian/32/1299_2.png) [@Shani\_Gamrian](https://discuss.pytorch.org/u/Shani_Gamrian)\
**Post date:** [January 2, 2019, 11:18am UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/1 "2019-01-02T11:18:45Z")

</div>

What is the advantage of using `binary_cross_entropy_with_logits` (aka BCE with sigmoid) over the regular `binary_cross_entropy`? I have a multi-binary classification problem and I’m trying to decide which one to choose.

---

<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:** [January 2, 2019, 11:24am UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/2 "2019-01-02T11:24:50Z")

</div>

As you described the only difference is the included sigmoid activation in `nn.BCEWithLogitsLoss`.  
It’s comparable to `nn.CrossEntropyLoss` and `nn.NLLLoss`. While the former uses a `nn.LogSoftmax` activation function internally, you would have to add it in the latter criterion.

---

<div class="post-metadata">

**Author:** ![Shani\_Gamrian](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/shani_gamrian/32/1299_2.png) [@Shani\_Gamrian](https://discuss.pytorch.org/u/Shani_Gamrian)\
**Post date:** [January 2, 2019, 11:34am UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/3 "2019-01-02T11:34:51Z")

</div>

I understand the differences in the implementation, I don’t understand the theoretical advantages of using BCE with sigmoid vs without sigmoid.

---

<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:** [January 2, 2019, 11:43am UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/4 "2019-01-02T11:43:20Z")

</div>

Sorry for not being not clear enough.  
The sigmoid activation should be applied in both cases.  
While `nn.BCEWithLogitsLoss` will apply it internally for you, you should add it manually if you are using `nn.BCELoss`.

---

<div class="post-metadata">

**Author:** ![vmirly1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vmirly1/32/3309_2.png) [@vmirly1](https://discuss.pytorch.org/u/vmirly1)\
**Post date:** [January 2, 2019, 2:24pm UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/5 "2019-01-02T14:24:44Z")

</div>

@ptrblck Isn’t it the other way around? I thought `BCELoss` needs to receive the outputs of `Sigmoid` activation as its input, but the other-one `BCEWithLogitsLoss` will need the logits as inputs instead of outputs of `Sigmoid`, since it will apply sigmoid internally.

Although, the example in the docs do not apply Sigmoid function prior to BCELoss:

```auto
### Example from pytorch-docs:
>>> m = nn.Sigmoid()
>>> loss = nn.BCELoss()
>>> input = torch.randn(3, requires_grad=True)
>>> target = torch.empty(3).random_(2)
>>> output = loss(m(input), target)
>>> output.backward()

```

So, I suppose the loss should be computed as

```auto
logits = m(input)
output = loss(torch.sigmoid(logits), target)

```

Is that right?

---

<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:** [January 2, 2019, 4:43pm UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/6 "2019-01-02T16:43:31Z")

</div>

Yes, you are completely right and I’ve mixed up both names. I’ll edit my post to get it right.

In the example the `nn.Sigmoid` will be applied by `m(input)`, so it should be right.  
Your code should apply the sigmoid function twice (once in `m()` and the second time using `torch.sigmoid`).

---

<div class="post-metadata">

**Author:** ![vmirly1](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/vmirly1/32/3309_2.png) [@vmirly1](https://discuss.pytorch.org/u/vmirly1)\
**Post date:** [January 2, 2019, 9:06pm UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/7 "2019-01-02T21:06:59Z")

</div>

Yes, that’s right. I somehow over-looked the definition of `m`.

Thanks

---

<div class="post-metadata">

**Author:** ![v1vekkumar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/v1vekkumar/32/10769_2.png) [@v1vekkumar](https://discuss.pytorch.org/u/v1vekkumar)\
**Post date:** [April 6, 2019, 2:02pm UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/8 "2019-04-06T14:02:01Z")

</div>

@Shani_Gamrian Use BCEWithLogitsLoss - it’s stable than using a plain `Sigmoid followed by a`BCELoss` (uses log-sum-exp trick for numerical stability)

see [https://github.com/pytorch/pytorch/issues/751](https://github.com/pytorch/pytorch/issues/751)

---

<div class="post-metadata">

**Author:** ![sid-ls](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sid-ls/32/12123_2.png) [@sid-ls](https://discuss.pytorch.org/u/sid-ls)\
**Post date:** [May 21, 2019, 1:57am UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/9 "2019-05-21T01:57:48Z")

</div>

> [@ptrblck](#):
>
> As you described the only difference is the included sigmoid activation in `nn.BCELoss` .

Just to clarify, if using `nn.BCEWithLogitsLoss(target, output)`, `output` should be passed through a sigmoid and only then to `BCEWithLogitsLoss`? I don’t understand why one would pass it through a sigmoid twice because x is already a probability after passing through one sigmoid.

---

<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 21, 2019, 6:50am UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/10 "2019-05-21T06:50:24Z")

</div>

No, that was a typo which @vmirly1 already corrected.  
You should pass logits to `nn.BCEwithLogitsLoss` and probabilities (using sigmoid) to `nn.BCELoss`.

Also, make sure to pass the model output first and then the target to the criterion.

---

<div class="post-metadata">

**Author:** ![shahriarshayesteh](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/shahriarshayesteh/32/31524_2.png) [@shahriarshayesteh](https://discuss.pytorch.org/u/shahriarshayesteh)\
**Post date:** [June 19, 2019, 7:45pm UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/11 "2019-06-19T19:45:37Z")

</div>

Sorry for asking my question here, I’m doing wod2vec with negative sampling and I had problem using nn.NLLLoss to train my network and I was reading pytorch loss functions, then I found out `binary\_cross\_entropy\_with\_logits, it says that This loss combines a Sigmoid layer and the BCELoss in one single class and This is used for measuring the error of a reconstruction in for example an auto-encoder. Note that the targets yy should be numbers between 0 and 1. So do you think I made a right choice for my loss function?  
Thanks

---

<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:** [June 20, 2019, 10:58pm UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/12 "2019-06-20T22:58:02Z")

</div>

Negative sampling might work with `nn.BCE(WithLogits)Loss`, but might be inefficient, as you would probably calculate the non-reduced loss for all classes and mask them afterwards.  
Some implementations sample the negative classes beforehand and calculate the bce loss manually, e.g. as described [here](https://adoni.github.io/2017/11/08/word2vec-pytorch/).

---

<div class="post-metadata">

**Author:** ![n0obcoder](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/n0obcoder/32/16606_2.png) [@n0obcoder](https://discuss.pytorch.org/u/n0obcoder)\
**Post date:** [July 16, 2019, 11:46am UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/13 "2019-07-16T11:46:10Z")

</div>

but what exactly is the difference between the two? i mean how is BCEWithLogitsLoss more stable? what stability means here. It would be great if you could explain it to me in little more detail. Thanks in advance 😃

---

<div class="post-metadata">

**Author:** ![Santosh-Gupta](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/santosh-gupta/32/30716_2.png) [@Santosh-Gupta](https://discuss.pytorch.org/u/Santosh-Gupta)\
**Post date:** [August 15, 2019, 2:23am UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/14 "2019-08-15T02:23:07Z")

</div>

Just to be super duper clear, do **Not** pass inputs through a sigmoid if using `BCEWithLogitsLoss` ?

And for the outputs, just use ‘1’ for True classes, or ‘0’ for the false classes?

---

<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:** [August 15, 2019, 12:30pm UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/15 "2019-08-15T12:30:25Z")

</div>

> [@Santosh-Gupta](#):
>
> Just to be super duper clear, do **Not** pass inputs through a sigmoid if using `BCEWithLogitsLoss` ?

Yes.

- logits → `nn.BCEWithLogitsLoss`
- logits → sigmoid → `nn.BCELoss`

> [@Santosh-Gupta](#):
>
> And for the outputs, just use ‘1’ for True classes, or ‘0’ for the false classes?

That would be the standard definition, but you can basically define True/False, Positive/Negative as you wish.

---

<div class="post-metadata">

**Author:** ![bigyeet](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/dfb087/32.png) [@bigyeet](https://discuss.pytorch.org/u/bigyeet)\
**Post date:** [February 18, 2020, 7:27am UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/16 "2020-02-18T07:27:41Z")

</div>

Thanks everyone for your input.

If the network outputs logits and is trained with BCEWithLogitsLoss, during inference, should I use the logits directly as a probability or should I apply sigmoid first?

edit:  
Now that I think about it, the logit could totally be negative or larger than 1, making it unsuitable as a probability. So I should apply a sigmoid during inference. Please confirm

---

<div class="post-metadata">

**Author:** ![G.M](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/g.m/32/19266_2.png) [@G.M](https://discuss.pytorch.org/u/G.M)\
**Post date:** [February 18, 2020, 9:25am UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/17 "2020-02-18T09:25:21Z")

</div>

`y*ln(sig(x)) + (1 - y)*ln(1 - sig(x))` = `y*ln(1 / (1 + e^-x))+(1 - y)*ln(1 - 1 / (1 + e^-x))` = `ln(e^yx / (e^x + 1))` If I’m correct, it works like this.

---

<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 18, 2020, 3:49pm UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/18 "2020-02-18T15:49:05Z")

</div>

The values of the logits might be harder to interpret, so you might want to apply a sigmoid to get the probabilities.  
Note that a logit of 0 will map to `p=0.5`, so you could still easily get the prediction for this simple threshold with logits.

---

<div class="post-metadata">

**Author:** ![Dmitry\_Rybakov](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/dmitry_rybakov/32/22266_2.png) [@Dmitry\_Rybakov](https://discuss.pytorch.org/u/Dmitry_Rybakov)\
**Post date:** [April 2, 2020, 1:16pm UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/19 "2020-04-02T13:16:57Z")

</div>

sigmoid( ln(x/(1-x) ) ) = x

> **[sigmoid(ln(x/(1-x))) - Wolfram|Alpha](https://www.wolframalpha.com/input/?i=sigmoid%28ln%28x%2F%281-x%29%29%29)**
>
> Wolfram|Alpha brings expert-level knowledge and capabilities to the broadest possible range of people—spanning all professions and education levels.

---

<div class="post-metadata">

**Author:** ![snip3r77](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@snip3r77](https://discuss.pytorch.org/u/snip3r77)\
**Post date:** [April 2, 2020, 3:44pm UTC](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586/20 "2020-04-02T15:44:00Z")

</div>

for both BCEWithLogitsLoss and CrossEntropyLoss ( 1 step )

we will need to do this when doing inferencing?

```auto
logps = model(img)
ps = torch.exp(logps)

```

Also, even if it’s 2steps(i.e logsoftmax + nlllosss) the above still applies right?

Thanks

[Next page](https://discuss.pytorch.org/t/bceloss-vs-bcewithlogitsloss/33586.md?page=2)
