Soft Dice Loss

~12 mincode completion

Implement soft_dice_loss(probs, target, eps) returning a scalar.

Examples

A perfect hard prediction has zero loss

Input
soft_dice_loss([[1, 1], [0, 0]], [[1, 1], [0, 0]], 0)
Output
0

Half-confident everywhere on a half-foreground target: loss 0.5

Input
soft_dice_loss([[0.5, 0.5]], [[1, 0]], 0)
Output
0.5

Empty prediction on an empty target is zero loss because of eps

Input
soft_dice_loss([[0, 0]], [[0, 0]], 1)
Output
0

Hints

Hint 1

Sum with , and check which axis you are summing over.

Hint 2

Watch for this: thresholded the probabilities before the sum.

Requirements

  • probs: array of foreground probabilities in [0, 1]

  • target: array of 0/1 labels, same shape

  • eps: smoothing constant added to numerator and denominator

  • Return scalar 1 - soft Dice

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

8 employers weight this skill

4 autonomy companies, 1 defense company, 1 health and bio company, 1 enterprise vendor, 1 AI product company. Top match scores 81.

Python
import numpy as np


def soft_dice_loss(probs, target, eps):
    """
    Soft Dice loss on a single-class probability map.

    Args:
        probs:  array of foreground probabilities in [0, 1]
        target: array of 0/1 labels, same shape
        eps:    smoothing constant added to numerator and denominator

    Returns:
        scalar 1 - soft Dice
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.