TensorTonicTensorTonic
Problems
Study PlansProjectsInterviewPricingFeedback
Problems
Loading...
1 / 1

Implement Dice Loss

Loss Functions
Medium

Implement Dice Loss for segmentation tasks. This loss measures overlap between prediction and ground truth masks, widely used in image segmentation where pixel imbalance is common.

Dice Coefficient & Loss:

Dice Coefficient (with smoothing epsilon for numerical stability):

Dice(P,Y)=2∑PY+ϵ∑P+∑Y+ϵ\mathrm{Dice}(P, Y) = \frac{2 \sum PY + \epsilon}{\sum P + \sum Y + \epsilon} Dice(P,Y)=∑P+∑Y+ϵ2∑PY+ϵ​

Dice Loss:

Dice Loss=1−Dice\mathrm{Dice\ Loss} = 1 - \mathrm{Dice} Dice Loss=1−Dice

Function Arguments

  • p: array-like - Predicted probabilities, shape (N,) or (H,W)
  • y: array-like - Binary mask {0,1}, same shape as p
  • eps: float = 1e-8 - Smoothing epsilon added to both numerator and denominator
Loading visualization...

Examples

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

Output: 0.135

Good overlap: Dice ≈ 0.865, Loss ≈ 0.135

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

Output: 0.0

Perfect overlap: Dice = 1.0, Loss = 0.0

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

Output: 1.0

No overlap: Dice ≈ 0.0, Loss ≈ 1.0

Hint 1

Flatten arrays with .flatten() to handle both 1D and 2D inputs uniformly.

Hint 2

Intersection is np.sum() of element-wise product, union is sum of both arrays.

Hint 3

Add eps to both numerator and denominator: (2*intersection + eps) / (sum_p + sum_y + eps).

Requirements

  • Convert inputs to float arrays
  • Compute intersection and union using sums
  • Use stability epsilon to prevent division by zero
  • Return scalar Dice loss
  • Handle both 1D and 2D inputs

Constraints

  • Works on 1D or 2D masks
  • p, y have same shape
  • NumPy only; time limit: 200ms
Try Similar Problems
Binary Focal LossFocal LossCross Entropy LossIou Bounding BoxContrastive Loss

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Implement Dice Loss

Loss Functions
Medium

Implement Dice Loss for segmentation tasks. This loss measures overlap between prediction and ground truth masks, widely used in image segmentation where pixel imbalance is common.

Dice Coefficient & Loss:

Dice Coefficient (with smoothing epsilon for numerical stability):

Dice(P,Y)=2∑PY+ϵ∑P+∑Y+ϵ\mathrm{Dice}(P, Y) = \frac{2 \sum PY + \epsilon}{\sum P + \sum Y + \epsilon} Dice(P,Y)=∑P+∑Y+ϵ2∑PY+ϵ​

Dice Loss:

Dice Loss=1−Dice\mathrm{Dice\ Loss} = 1 - \mathrm{Dice} Dice Loss=1−Dice

Function Arguments

  • p: array-like - Predicted probabilities, shape (N,) or (H,W)
  • y: array-like - Binary mask {0,1}, same shape as p
  • eps: float = 1e-8 - Smoothing epsilon added to both numerator and denominator
Loading visualization...

Examples

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

Output: 0.135

Good overlap: Dice ≈ 0.865, Loss ≈ 0.135

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

Output: 0.0

Perfect overlap: Dice = 1.0, Loss = 0.0

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

Output: 1.0

No overlap: Dice ≈ 0.0, Loss ≈ 1.0

Hint 1

Flatten arrays with .flatten() to handle both 1D and 2D inputs uniformly.

Hint 2

Intersection is np.sum() of element-wise product, union is sum of both arrays.

Hint 3

Add eps to both numerator and denominator: (2*intersection + eps) / (sum_p + sum_y + eps).

Requirements

  • Convert inputs to float arrays
  • Compute intersection and union using sums
  • Use stability epsilon to prevent division by zero
  • Return scalar Dice loss
  • Handle both 1D and 2D inputs

Constraints

  • Works on 1D or 2D masks
  • p, y have same shape
  • NumPy only; time limit: 200ms
Try Similar Problems
Binary Focal LossFocal LossCross Entropy LossIou Bounding BoxContrastive Loss

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: array

Accepts: number

You must run your code first.