skills/cv/bce-dice-combined-loss/SKILL.md
Combines BCE-with-logits and soft Dice loss with configurable weights for binary and multilabel segmentation training.
npx skillsauth add wenmin-wu/ds-skills cv-bce-dice-combined-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.
BCE loss optimizes per-pixel accuracy but struggles with class imbalance in segmentation (background dominates). Dice loss directly optimizes the Dice coefficient (F1 score) and handles imbalance well, but has noisy gradients. Combining both gives stable training from BCE while pushing toward high Dice overlap. This is the standard loss for medical image segmentation tasks.
import torch
import torch.nn as nn
def dice_loss(logits, targets, smooth=1.0):
probs = torch.sigmoid(logits)
iflat = probs.view(-1)
tflat = targets.view(-1)
intersection = (iflat * tflat).sum()
return 1 - (2. * intersection + smooth) / (iflat.sum() + tflat.sum() + smooth)
def bce_dice_loss(logits, targets, bce_weight=1.0, dice_weight=1.0):
bce = nn.functional.binary_cross_entropy_with_logits(logits, targets)
dice = dice_loss(logits, targets)
return (bce_weight * bce + dice_weight * dice) / (bce_weight + dice_weight)
# Usage
loss = bce_dice_loss(model_output, mask_target)
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