skills/cv/tversky-loss/SKILL.md
Tversky loss with independent alpha/beta constants to separately penalize false positives and false negatives in imbalanced segmentation.
npx skillsauth add wenmin-wu/ds-skills cv-tversky-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 weights false positives and false negatives equally, but in many segmentation tasks they have different costs — missing a tumor (FN) is worse than a false alarm (FP). Tversky loss generalizes Dice by introducing alpha and beta to independently control FP and FN penalties: Tversky = TP / (TP + alpha*FP + beta*FN). Setting alpha=beta=0.5 recovers Dice; alpha=0.3, beta=0.7 penalizes false negatives more, improving recall on small or rare objects.
import torch
import torch.nn as nn
import torch.nn.functional as F
class TverskyLoss(nn.Module):
def __init__(self, alpha=0.3, beta=0.7, smooth=1.0):
super().__init__()
self.alpha = alpha
self.beta = beta
self.smooth = smooth
def forward(self, inputs, targets):
inputs = torch.sigmoid(inputs).view(-1)
targets = targets.view(-1)
tp = (inputs * targets).sum()
fp = ((1 - targets) * inputs).sum()
fn = (targets * (1 - inputs)).sum()
tversky = (tp + self.smooth) / (tp + self.alpha * fp + self.beta * fn + self.smooth)
return 1 - tversky
# Usage: penalize FN more than FP
criterion = TverskyLoss(alpha=0.3, beta=0.7)
loss = criterion(logits, masks)
1 - Tversky as lossalpha < beta boosts recall (fewer FN); alpha > beta boosts precision (fewer FP)(1-Tversky)^gamma to further focus on hard examplesdata-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