Binary Cross Entropy vs Cross Entropy: When to Use Which Loss

Which cross entropy loss to use: sigmoid with binary cross entropy for binary and multi-label tasks, softmax with categorical for exclusive classes.

Binary Cross Entropy vs Cross Entropy: When to Use Which Loss
Written by TechnoLynx Published on 11 Jul 2026

Choose the cross entropy loss by how your labels behave. If each output is an independent yes/no decision (one binary target, or several labels that can all be true at once), use a sigmoid per output with binary cross entropy. If exactly one class is correct per example, use softmax with categorical cross entropy. In either case, feed the loss raw logits through the fused version (PyTorch’s BCEWithLogitsLoss or CrossEntropyLoss, TensorFlow’s *_with_logits functions), because combining the activation and the log inside the loss is the numerically stable way to compute it.

The rest of this page covers the details that decide whether training actually works: the binary edge case, multi-label problems, the formula, why the pairings exist, and the input conventions that cause most silent bugs.

Cross Entropy Loss Choices at a Glance

Task Output layer Activation (inside the loss) PyTorch Keras / TensorFlow Target format
Binary, one output 1 logit sigmoid BCEWithLogitsLoss BinaryCrossentropy(from_logits=True) 0 or 1 per example
Binary, two outputs 2 logits softmax CrossEntropyLoss SparseCategoricalCrossentropy class index 0 or 1
Multi-class, exclusive C logits softmax CrossEntropyLoss CategoricalCrossentropy (one-hot) or SparseCategoricalCrossentropy (integers) class index, or one-hot / probability vector
Multi-label C logits one sigmoid per label BCEWithLogitsLoss tf.nn.sigmoid_cross_entropy_with_logits a 0/1 (or soft) value per label

Can I Use nn.CrossEntropyLoss for a Binary Classification Problem?

Yes. PyTorch’s CrossEntropyLoss is documented for a classification problem with C classes, and two is a valid C: give the model two output logits and pass class indices 0 or 1 as targets. Sebastian Raschka’s walkthrough of PyTorch losses makes the same point, noting that the regular CrossEntropyLoss can be used for binary classification alongside the BCE options.

The alternative is one logit with BCEWithLogitsLoss. What you should not do is mix them, for example a single sigmoid output fed to CrossEntropyLoss, which then sees a one-class problem.

Which Loss for Multi-Label Classification, Where One Input Can Belong to Several Classes?

Use independent sigmoids with binary cross entropy, one term per label, summed or averaged across labels. PyTorch’s BCEWithLogitsLoss documentation treats this directly: its class dimension is c = 1 for single-label binary classification and c > 1 for multi-label binary classification. TensorFlow describes sigmoid_cross_entropy_with_logits as measuring error where each outcome is independent.

Softmax is the wrong tool here for a structural reason. Softmax forces the probabilities to sum to one, so raising one label necessarily lowers the others; an image tagged cat, outdoor and night cannot score high on all three. TensorFlow’s softmax cross entropy documentation says the same from the other side: it is for classes that are mutually exclusive, where a CIFAR-10 image is a dog or a truck, not both. In image tagging for computer vision R&D, the label structure decides which loss fits.

For class imbalance, BCEWithLogitsLoss offers pos_weight, one element per class, which adjusts the loss for the imbalance between negative and positive samples in that class. For exclusive multi-class problems the equivalent knob is the per-class weight argument, which PyTorch’s NLLLoss documentation describes as useful for an unbalanced training set.

The Rule Is a Default, and the Evidence Has Exceptions

Two published results complicate the clean story. Mahajan et al. (ECCV 2018), pretraining on Instagram images with several hashtags each, tried per-hashtag sigmoids with binary logistic loss and obtained significantly worse results than a softmax over all hashtags trained against a target spread evenly over the image’s hashtags. The authors call this counter-intuitive and suggest that making sigmoids work may need label completion techniques and more hyperparameter search.

The opposite surprise shows up on single-label data. In “ResNet strikes back” (Wightman, Touvron and Jégou), training ImageNet with Mixup and CutMix, the authors treat each mixed image as containing all of its source concepts and switch to BCE, with each selected class’s target set to 1 independently of the others. Over their best settings, BCE slightly outperformed cross entropy. They are careful about the scope: with the same target, they do not conclude that BCE is necessarily better than CE.

The Cross Entropy Loss Function, and Why Log Loss and NLL Are the Same Thing

For a single binary example with label y in {0, 1} and logit z, binary cross entropy is:

L_BCE = -[ y * log(sigmoid(z)) + (1 - y) * log(1 - sigmoid(z)) ]

For a single example with a one-hot label y over K exclusive classes, categorical cross entropy is:

L_CCE = - sum_k  y_k * log(softmax(z)_k)

Only the true class’s term survives the sum when y is one-hot, so the loss is the negative log of the probability the model gave the correct class. Over a dataset, Google’s Machine Learning Crash Course writes log loss as the mean of the binary expression across N examples.

The names overlap because they describe one quantity. scikit-learn documents log loss as “aka logistic loss or cross-entropy loss”, defined as the negative log-likelihood of a logistic model. Raschka summarises it as: logistic loss is the negative log-likelihood of a logistic regression model, and minimising the negative log-likelihood is the same as minimising the cross entropy. More generally, maximising likelihood amounts to minimising cross entropy, and for neural networks with a softmax output, cross entropy coincides with the logistic loss.

In PyTorch, the split between NLLLoss and CrossEntropyLoss is about input format, not maths. NLLLoss expects log-probabilities, typically from a LogSoftmax layer, and its documentation suggests CrossEntropyLoss if you prefer not to add that layer. CrossEntropyLoss on logits is equivalent to LogSoftmax followed by NLLLoss.

Why Sigmoid Goes with Binary Cross Entropy and Softmax with Categorical

The pairings are canonical because the gradient of each loss with respect to the logits collapses to the predicted probability minus the label, p - y. Squared error on a sigmoid output instead runs into the sigmoid-saturation problem the pairing avoids. Google’s crash course gives a related reason, precision: with squared loss, outputs near 0 and 1 would need ever more precision to track, while log loss measures the logarithm of the change. Goodfellow, Bengio and Courville describe mean squared error, popular in the 1980s and 1990s, being gradually replaced by cross-entropy losses and the principle of maximum likelihood.

Why BCEWithLogitsLoss and the Other Logits Versions Are More Stable

PyTorch’s documentation states it directly: BCEWithLogitsLoss combines the sigmoid and BCELoss in one class and is more numerically stable than a plain sigmoid followed by BCELoss, because the combined form can use the log-sum-exp trick. Raschka gives the same reason for CrossEntropyLoss merging log_softmax into the loss: numerical stability, helped by the fact that the derivatives simplify cleanly. TensorFlow exposes sigmoid_cross_entropy_with_logits and softmax_cross_entropy_with_logits for the same purpose, avoiding forming the probabilities explicitly.

The underlying problem is floating point. Blanchard, Higham and Higham note that log-sum-exp and softmax contain exponentials and are prone to overflow and underflow, especially in low precision, so implementations use shifted formulas. Their rounding error analysis concludes the shifted formulas are about as accurate as the unshifted ones and safe to use.

Raschka reports one example of the practical effect: a VGG16 trained with logits and BCEWithLogitsLoss reached about 92% accuracy after 4 epochs, while the sigmoid plus BCELoss version did not converge with the same hyperparameters.

Input Conventions Behind the Most Common Loss Bugs

Check these in order:

  1. Does the PyTorch loss receive raw logits? CrossEntropyLoss expects unnormalised logits that need not be positive or sum to 1. Adding nn.Softmax before it, one of the combinations Raschka lists, applies softmax twice, since the loss already applies LogSoftmax.
  2. Does the Keras loss know it is getting logits? BinaryCrossentropy and CategoricalCrossentropy both assume probabilities by default, so a model emitting logits needs from_logits=True.
  3. Are the targets in the format the loss expects? CrossEntropyLoss takes class indices in [0, C), or class probabilities. Keras CategoricalCrossentropy expects one-hot labels, with SparseCategoricalCrossentropy for integers. BCELoss and BCEWithLogitsLoss want targets between 0 and 1 with the same shape as the predictions.
  4. Are soft targets valid distributions? With probability targets, CrossEntropyLoss expects each row to be a distribution summing to 1 (useful for blended labels and label smoothing). PyTorch notes that class indices are generally faster, so use probabilities only when a single label per item is too restrictive. Sigmoid cross entropy accepts soft per-label values between 0 and 1 as well as hard labels.

The loss choice also shapes what the evaluation numbers mean, which is covered separately in what the BCE-versus-CE difference means for your evaluation metrics.

Settle the Label Question First

The sigmoid-or-softmax rule only looks like a choice between two loss functions. Underneath, you are deciding whether your labels are mutually exclusive, independent, or something messier in between. The Instagram hashtag and Mixup results are two cases where the label structure the targets encoded departed from the textbook mapping, in opposite directions. If you cannot say confidently which of those your data is, treat the loss as a hypothesis to test on held-out data, using the fused logits version for whichever you try. Tracking and reproducing those loss experiments is the kind of work covered by MLOps engineering.