TensorTonic
Problems
Implement Dice Loss
Loss Functions
Medium

Compute Dice loss for two equally shaped prediction and target masks. First calculate the smoothed Dice coefficient:

Dice⁡(P,Y)=2∑iPiYi+ε∑iPi+∑iYi+ε\operatorname{Dice}(P,Y) = \frac{2\sum_i P_iY_i + \varepsilon}{\sum_i P_i + \sum_i Y_i + \varepsilon}Dice(P,Y)=∑i​Pi​+∑i​Yi​+ε2∑i​Pi​Yi​+ε​

Then calculate the loss:

LDice=1−Dice⁡(P,Y)L_{\mathrm{Dice}} = 1 - \operatorname{Dice}(P,Y)LDice​=1−Dice(P,Y)

Here, PiP_iPi​ and YiY_iYi​ are corresponding mask values and ε\varepsilonε is eps. Sum across every dimension and return the loss as a Python float.

Loading visualization...

Examples

Input: p = [0.9, 0.7, 0.1, 0.0], y = [1, 1, 0, 0], eps = 1e-8

Output: 0.135135

Explanation: The soft intersection is 1.6, producing a Dice coefficient of approximately 0.864865.

Input: p = [1.0, 1.0, 0.0, 0.0], y = [1, 1, 0, 0], eps = 1e-8

Output: 0.0

Input: p = [1.0, 1.0], y = [0, 0], eps = 1e-8

Output: 1.0

Hint 1

Use np.sum(p * y) for the soft intersection.

Hint 2

Compute the coefficient before returning 1.0 - coefficient.

Requirements

  • Sum the intersection and both masks across every dimension
  • Add eps to the numerator and denominator
  • Return Dice loss as a Python float

Constraints

  • p and y are nonempty numeric lists with the same shape
  • Prediction values are between 0 and 1
  • Target values are 0 or 1
  • eps is positive
  • Use NumPy only
Try Similar Problems
Implement Contrastive Loss (Siamese)MediumImplement Cross-Entropy Loss in PythonMediumBinary Focal LossMediumImplement Focal LossMediumIntersection over Union (IoU)EasyLogistic Regression Training LoopMediumImplement Triplet LossMediumImplement Symmetric CLIP Contrastive LossMediumHigher-Order Derivatives & Loss CurvatureMediumLoss FunctionsMediumBradley-Terry Reward Model LossMediumREINFORCE with BaselineMedium
Scratchpad

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
Implement Dice Loss

Implement Dice Loss

Loss Functions
Medium

Compute Dice loss for two equally shaped prediction and target masks. First calculate the smoothed Dice coefficient:

Dice⁡(P,Y)=2∑iPiYi+ε∑iPi+∑iYi+ε\operatorname{Dice}(P,Y) = \frac{2\sum_i P_iY_i + \varepsilon}{\sum_i P_i + \sum_i Y_i + \varepsilon}Dice(P,Y)=∑i​Pi​+∑i​Yi​+ε2∑i​Pi​Yi​+ε​

Then calculate the loss:

LDice=1−Dice⁡(P,Y)L_{\mathrm{Dice}} = 1 - \operatorname{Dice}(P,Y)LDice​=1−Dice(P,Y)

Here, PiP_iPi​ and YiY_iYi​ are corresponding mask values and ε\varepsilonε is eps. Sum across every dimension and return the loss as a Python float.

Loading visualization...

Examples

Input: p = [0.9, 0.7, 0.1, 0.0], y = [1, 1, 0, 0], eps = 1e-8

Output: 0.135135

Explanation: The soft intersection is 1.6, producing a Dice coefficient of approximately 0.864865.

Input: p = [1.0, 1.0, 0.0, 0.0], y = [1, 1, 0, 0], eps = 1e-8

Output: 0.0

Input: p = [1.0, 1.0], y = [0, 0], eps = 1e-8

Output: 1.0

Hint 1

Use np.sum(p * y) for the soft intersection.

Hint 2

Compute the coefficient before returning 1.0 - coefficient.

Requirements

  • Sum the intersection and both masks across every dimension
  • Add eps to the numerator and denominator
  • Return Dice loss as a Python float

Constraints

  • p and y are nonempty numeric lists with the same shape
  • Prediction values are between 0 and 1
  • Target values are 0 or 1
  • eps is positive
  • Use NumPy only
Try Similar Problems
Implement Contrastive Loss (Siamese)MediumImplement Cross-Entropy Loss in PythonMediumBinary Focal LossMediumImplement Focal LossMediumIntersection over Union (IoU)EasyLogistic Regression Training LoopMediumImplement Triplet LossMediumImplement Symmetric CLIP Contrastive LossMediumHigher-Order Derivatives & Loss CurvatureMediumLoss FunctionsMediumBradley-Terry Reward Model LossMediumREINFORCE with BaselineMedium
Scratchpad

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
Problems
Loading...
1 / 1