When training a pixel segmentation neural network, such as a fully convolutional network, how do you make the decision to use the cross-entropy loss function versus Dice-coefficient loss function?

I realize this is a short question, but not quite sure what other information to provide. I looked at a bunch of documentation about the two loss functions but am not able to get a intuitive sense of when to use one over the other.

  • $\begingroup$ Why not use the hands-on approach to use both and compare the results. Looking at a lot of different fields of application, the discussion of loss function is its own topic of extended research. Since convolutional networks are still a 'hot topic', I'd guess that most papers will still be published in the future. $\endgroup$
    – cherub
    Commented May 3, 2018 at 13:40

3 Answers 3


One compelling reason for using cross-entropy over dice-coefficient or the similar IoU metric is that the gradients are nicer.

The gradients of cross-entropy wrt the logits is something like $p - t$, where $p$ is the softmax outputs and $t$ is the target. Meanwhile, if we try to write the dice coefficient in a differentiable form: $\frac{2pt}{p^2+t^2}$ or $\frac{2pt}{p+t}$, then the resulting gradients wrt $p$ are much uglier: $\frac{2t(t^2-p^2)}{(p^2+t^2)^2}$ and $\frac{2t^2}{(p+t)^2}$. It's easy to imagine a case where both $p$ and $t$ are small, and the gradient blows up to some huge value. In general, it seems likely that training will become more unstable.

The main reason that people try to use dice coefficient or IoU directly is that the actual goal is maximization of those metrics, and cross-entropy is just a proxy which is easier to maximize using backpropagation. In addition, Dice coefficient performs better at class imbalanced problems by design:

However, class imbalance is typically taken care of simply by assigning loss multipliers to each class, such that the network is highly disincentivized to simply ignore a class which appears infrequently, so it's unclear that Dice coefficient is really necessary in these cases.

I would start with cross-entropy loss, which seems to be the standard loss for training segmentation networks, unless there was a really compelling reason to use Dice coefficient.

  • $\begingroup$ The cross entropy of all exponential families is a nice difference $p-t$. $\endgroup$
    – Neil G
    Commented May 4, 2018 at 8:46
  • 3
    $\begingroup$ When is the "main goal" maximization of dice loss? I checked the original paper and all they say is “we obtain results that we experimentally observed are much better than the ones computed through the same network trained optimising a multinomial logistic loss with sample re-weighting.” This is not very convincing. $\endgroup$
    – Neil G
    Commented May 4, 2018 at 8:48
  • $\begingroup$ @shimao By "ugly" you just mean that the gradients can explode, is that correct? $\endgroup$
    – flawr
    Commented Jan 8, 2020 at 8:18
  • $\begingroup$ @NeilG, What's the original paper? $\endgroup$
    – Narfanar
    Commented Feb 5, 2020 at 19:56
  • 2
    $\begingroup$ @Narfanar Probably you already found it, but for anyone else: The quote is from "V-Net: Fully Convolutional Neural Networks for Volumetric Medical Image Segmentation, Milletari 2016" $\endgroup$
    – packoman
    Commented Aug 6, 2020 at 11:07

As summarized by @shimao and @cherub, one cannot say apriori which one will work better on a particular dataset. The correct way is to try both and compare the results. Also, note that when it comes to segmentation, it is not so easy to "compare the results": IoU based measures like dice coefficient cover only some aspects of the quality of the segmentation; in some applications, different measures such as mean surface distance or Hausdorff surface distance need to be used. As you see, not even the choice of the correct quality metric is trivial, let alone the choice of the best cost function.

I personally have very good experience with the dice coefficient; it really does wonders when it comes to class imbalance (some segments occupy less pixels/voxels than others). On the other hand, the training error curve becomes a total mess: it gave me absolutely no information about the convergence, so in this regard cross-entropy wins. Of course, this can/should be bypassed by checking the validation error anyways.

  • $\begingroup$ Interesting reply. Another alternative to cross-entropy that should be better at handling the majority background class is the focal loss: arxiv.org/abs/1708.02002 $\endgroup$
    – ogrisel
    Commented Feb 22, 2021 at 14:30

I would recommend you to use Dice loss when faced with class imbalanced datasets, which is common in the medicine domain, for example. Also, Dice loss was introduced in the paper "V-Net: Fully Convolutional Neural Networks for Volumetric Medical Image Segmentation" and in that work the authors state that Dice loss worked better than mutinomial logistic loss with sample re-weighting


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.