Different results when running binary_cross_entropy() and the BCEWithLogitsLoss

Hey everyone, in the lectures Jeremy said that the binary_cross_entropy function and the function
returned by nn.BCEWithLogitsLoss have the same implementation, but when invoking them I am getting different results. Has there been a change in the BCEWithLogitsLoss implementation or am I misunderstanding something?

Hi @armanharutyunyan

Nice job on trying to reimplement BCE to understand the inner workings of the loss function. In your definition, in the torch.where clause, you need to either do targets == 0 for your code to work or you can change the conditionals to inputs, 1-inputs instead of vice-versa and then the two outputs should match.

Hope this helps,
Thanks! :slight_smile:

3 Likes

Hey @ElisonSherton, I am very grateful to you for responding to this. I already gave up hope. :slightly_smiling_face: I’ll try out what you suggested!

1 Like

Thanks, that really helps to clear things up for me too :slight_smile:

1 Like