paddle.nn.functional. dice_loss ( input, label, epsilon=1e-05, name=None ) [source]

Dice loss for comparing the similarity between the input predictions and the label. This implementation is for binary classification, where the input is sigmoid predictions of each pixel, usually used for segmentation task. The dice loss can be defined as the following equation:

\[\begin{split}dice\_loss &= 1 - \\frac{2 * intersection\_area}{total\_area} \\\\ &= \\frac{(total\_area - intersection\_area) - intersection\_area}{total\_area} \\\\ &= \\frac{(union\_area - intersection\_area)}{total\_area}\end{split}\]
  • input (Tensor) – Tensor, rank>=2, shape is \([N_1, N_2, ..., N_D]\), where \(N_1\) is the batch_size, \(N_D\) is 1. It is usually the output predictions of sigmoid activation. The data type can be float32 or float64.

  • label (Tensor) – Tensor, the groud truth with the same rank as input, shape is \([N_1, N_2, ..., N_D]\). where \(N_1\) is the batch_size, \(N_D\) is 1. The data type can be float32 or float64.

  • epsilon (float) – The epsilon will be added to the numerator and denominator. If both input and label are empty, it makes sure dice is 1. Default: 0.00001

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name


Tensor, which shape is [1], data type is the same as input .


import paddle
import paddle.nn.functional as F

x = paddle.randn((3,224,224,2))
label = paddle.randint(high=2, shape=(3,224,224,1))
predictions = F.softmax(x)
loss = F.dice_loss(input=predictions, label=label)