skills/cv/dice-loss/SKILL.md
Dice coefficient loss for pixel-level segmentation that directly optimizes the overlap between predicted and ground-truth masks.
npx skillsauth add wenmin-wu/ds-skills cv-dice-lossInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Dice loss computes 1 - (2*|P intersect G| / (|P| + |G|)), directly optimizing the F1/Dice overlap metric. Unlike BCE which operates per-pixel independently, Dice loss considers the global mask overlap, making it naturally robust to class imbalance — when only 1% of pixels are positive, BCE is dominated by easy negatives, but Dice loss focuses on the positive region overlap. Dice loss is the default choice for binary segmentation and typically improves IoU by 2-5% over pure BCE on imbalanced masks.
import torch
import torch.nn as nn
class DiceLoss(nn.Module):
def __init__(self, smooth=1.0):
super().__init__()
self.smooth = smooth
def forward(self, inputs, targets):
inputs = torch.sigmoid(inputs).view(-1)
targets = targets.view(-1)
intersection = (inputs * targets).sum()
dice = (2.0 * intersection + self.smooth) / (
inputs.sum() + targets.sum() + self.smooth
)
return 1 - dice
# Usage
criterion = DiceLoss()
loss = criterion(logits, masks)
1 - Dice as losscv-bce-dice-combined-loss)data-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF