skills/cv/lap-hard-negative-mining/SKILL.md
Use linear assignment problem (LAP/lapjv) on a score matrix to select globally optimal hard-negative pairs for metric learning
npx skillsauth add wenmin-wu/ds-skills cv-lap-hard-negative-miningInstall 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.
In metric learning, hard negatives (close but different-class examples) drive the most learning. Random negatives are too easy; per-anchor hardest negatives cause collapse. LAP (linear assignment problem) via the Jonker-Volgenant algorithm finds a globally optimal one-to-one assignment that maximizes overall difficulty across the entire batch, avoiding degenerate pairings.
import numpy as np
from lap import lapjv
def mine_hard_negatives(score_matrix, labels, t2i):
cost = -score_matrix.copy()
# Block same-class pairs with high cost
for cls_indices in labels.values():
idxs = [t2i[t] for t in cls_indices]
for i in idxs:
for j in idxs:
cost[i, j] = 10000.0
_, _, col_assignment = lapjv(cost)
hard_pairs = []
for j, i in enumerate(col_assignment):
hard_pairs.append((i, j))
cost[i, j] = 10000.0
cost[j, i] = 10000.0
return hard_pairs
lapjv to find optimal one-to-one hard negative assignmentlap package: pip install lap for fast C++ Jonker-Volgenant solverdata-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