BCELosswithLogits no binary assertion for gt

username: JuanFMontesinos
cooked: <p>Hi,<br>
If I’m not wrong BCE with logits used to have some assertion for the ground truth to be binary right?<br>
Any reason why it’s been removed? (pytorch 1.2.0) It doesn’t even check if it’s bounded between 0-1</p>
<pre><code class="lang-auto">import torch

crit = torch.nn.BCEWithLogitsLoss()

pred = (torch.rand(10) - 0.5) * 10
gt = torch.randint_like(pred,0,2).float()
assert (pred &gt; 1.).any()
assert (pred &lt; 1.).any()

dif_predgt = crit(pred,gt)
dif_gtpred = crit(gt,pred)
print(dif_gtpred)
print(dif_predgt)
</code></pre>

username: KFrank
cooked: <p>Hi Juan!</p>
<aside class="quote no-group" data-username="JuanFMontesinos" data-post="1" data-topic="60092" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/juanfmontesinos/48/76115_2.png" class="avatar"> JuanFMontesinos:</div>
<blockquote>
<p>If I’m not wrong BCE with logits used to have some assertion for the ground truth to be binary right?<br>
Any reason why it’s been removed? (pytorch 1.2.0) It doesn’t even check if it’s bounded between 0-1</p>
</blockquote>
</aside>
<p>First, I wouldn’t want such an assertion.*</p>
<p>Second, I happen to be running pytorch 0.3.0, and I don’t see such<br>
an assertion.</p>
<p>Here is your script, tweaked to run on 0.3.0:</p>
<pre data-code-wrap="python"><code class="lang-python">import torch
print (torch.__version__)

crit = torch.nn.BCEWithLogitsLoss()

pred = (torch.rand (10) - 0.5) * 10
gt = torch.round (torch.rand (10))
assert (pred &gt; 1.).any()
assert (pred &lt; 1.).any()

dif_predgt = crit (pred, gt)
dif_gtpred = crit (gt, pred)
print (dif_gtpred)
print (dif_predgt)
</code></pre>
<p>And here is the output:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; print (torch.__version__)
0.3.0b0+591e73e
&gt;&gt;&gt;
&gt;&gt;&gt; crit = torch.nn.BCEWithLogitsLoss()
&gt;&gt;&gt;
&gt;&gt;&gt; pred = (torch.rand (10) - 0.5) * 10
&gt;&gt;&gt; gt = torch.round (torch.rand (10))
&gt;&gt;&gt; assert (pred &gt; 1.).any()
&gt;&gt;&gt; assert (pred &lt; 1.).any()
&gt;&gt;&gt;
&gt;&gt;&gt; dif_predgt = crit (pred, gt)
&gt;&gt;&gt; dif_gtpred = crit (gt, pred)
&gt;&gt;&gt; print (dif_gtpred)
1.4237342596054077
&gt;&gt;&gt; print (dif_predgt)
1.8770477950572968
</code></pre>
<p>*)  It is perfectly reasonable and meaningful for the targets (“ground<br>
truth”) of binary cross-entropy to non-binary.  Your annotated training<br>
data, could, for example, be scored with how likely the sample is to<br>
be in class “0” or class “1”.  I wouldn’t want pytorch to gratuitously<br>
prevent me from using binary cross-entropy with such a use case.</p>
<p>You could argue that pytorch should check that the targets are bounded<br>
between 0 and 1.  This could make sense to me, but I would probably<br>
argue that such a constraint is the responsibility of the upstream code<br>
and that <code>BCELossWithLogits</code> (and related) shouldn’t have to pay<br>
the minor performance penalty of imposing it.</p>
<p>Good luck.</p>
<p>K. Frank</p>

username: JuanFMontesinos
cooked: <p>You are right. I was just mentioning it because I think several losses assert inputs. Just to check the assertion wants  missing in some version <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>