skills/cv/batch-all-contrastive-loss/SKILL.md
All-vs-all contrastive loss comparing every pair in a batch (N^2 pairs) with margin and compactification regularizer
npx skillsauth add wenmin-wu/ds-skills cv-batch-all-contrastive-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.
Instead of sampling specific positive/negative pairs, compute distances for all N^2 pairs in a batch. Positive pairs (same class) minimize distance; negative pairs push apart beyond margin m. A compactification term prevents embedding space from expanding unboundedly. Averaging only over non-zero loss terms focuses learning on informative pairs.
import torch
import torch.nn as nn
import torch.nn.functional as F
class BatchAllContrastiveLoss(nn.Module):
def __init__(self, margin=10.0, wd=1e-4):
super().__init__()
self.margin = margin
self.wd = wd
def forward(self, embeddings, labels):
n = embeddings.size(0)
dist = torch.cdist(embeddings, embeddings).pow(2).view(-1)
labels_eq = (labels.unsqueeze(0) == labels.unsqueeze(1))
eye = torch.eye(n, device=labels.device).bool()
pos_mask = (labels_eq | eye).view(-1)
loss_pos = dist[pos_mask]
loss_neg = F.relu(self.margin - dist[~pos_mask].sqrt()).pow(2)
all_loss = torch.cat([loss_pos, loss_neg])
nonzero = all_loss[all_loss > 0]
loss = nonzero.mean() if nonzero.numel() > 0 else all_loss.sum()
loss += self.wd * dist.mean()
return loss
wd * mean(dist^2) prevents embeddings from drifting to infinitydata-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