skills/nlp/identity-weighted-bce-loss/SKILL.md
Weight BCE loss by identity subgroup membership to debias predictions — upweight samples where identity conflicts with label
npx skillsauth add wenmin-wu/ds-skills nlp-identity-weighted-bce-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.
Toxicity classifiers often learn spurious correlations between identity terms ("gay", "muslim") and toxicity. Fix this by upweighting four sample categories: (1) any identity-mentioned sample, (2) background-positive/subgroup-negative (toxic text without identity), (3) background-negative/subgroup-positive (non-toxic text with identity). Each category adds 0.25 to the sample weight, so hard fairness cases get up to 2x weight.
import numpy as np
import torch.nn as nn
def compute_identity_weights(df, identity_cols, target_col, threshold=0.5):
"""Compute per-sample weights for bias-aware training.
Args:
df: DataFrame with identity columns and target
identity_cols: list of identity attribute columns
target_col: toxicity target column
threshold: binarization threshold
"""
weights = np.ones(len(df)) / 4
has_identity = (df[identity_cols].fillna(0).values >= threshold).any(axis=1)
is_toxic = df[target_col].values >= threshold
weights += has_identity.astype(float) / 4 # subgroup
weights += (is_toxic & ~has_identity) / 4 # BPSN
weights += (~is_toxic & has_identity) / 4 # BNSP
return weights
# Custom loss with sample weights
def weighted_bce(pred, target_with_weights):
target = target_with_weights[:, 0:1]
weight = target_with_weights[:, 1:2]
return nn.BCEWithLogitsLoss(weight=weight)(pred, target)
weights = compute_identity_weights(train, IDENTITY_COLS, '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