# How to use Cross Entropy loss in pytorch for binary prediction?

In the pytorch docs, it says for cross entropy loss:

input has to be a Tensor of size (minibatch, C)

Does this mean that for binary (0,1) prediction, the input must be converted into an (N,2) tensor where the second dimension is equal to (1-p)?

So for instance if I predict 0.75 for a class with target 1 (true), would I have to stack two values (0.75; 0.25) on top of each other as input?

In Pytorch you can use cross-entropy loss for a binary classification task. You need to make sure to have two neurons in the final layer of the model. Make sure that you do not add a softmax function.

Use the below for resources:

https://discuss.pytorch.org/t/do-i-need-to-use-softmax-before-nn-crossentropyloss/16739

https://discuss.pytorch.org/t/why-does-crossentropyloss-include-the-softmax-function/4420

There is a specific loss in pytorch for your case :

• Just use the binary cross entropy loss

See the documentation here and here.

So for Binary Prediction in Pytorch the ideal loss function would be the Binary Cross Entropy Loss which is available along with all the other error functions in the nn submodule in can be called as follows

nn.BCELoss()


it has parameters reduction(mean and sum) and weights(pre-determined weightages). It's documentation can be found here

Ensure that the predicted and the target variables are off the same shape and both of them are of type float.