Soft Dice Loss for Segmentation

medium
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 20. Everything before it counts as cleared.

Segmentation masks are often mostly background, so pixel-wise cross-entropy is dominated by easy pixels. Soft Dice measures overlap directly:

Dice=2∑p g+s∑p+∑g+s,L=1−Dice\text{Dice} = \frac{2\sum p\,g + s}{\sum p + \sum g + s},\qquad L = 1 - \text{Dice}

with predicted probabilities pp, ground truth g∈{0,1}g \in \{0,1\} and smoothing ss. Implement dice_loss(p, g, smooth=1.0) over all elements, and dice_grad(p, g, smooth=1.0) returning ∂L/∂p\partial L/\partial p.

#segmentation#losses#imbalance2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Soft Dice Loss for Segmentation · TechGradify