skills/nlp/pairwise-margin-ranking-loss/SKILL.md
Trains a transformer with MarginRankingLoss on text pairs (more/less toxic), learning to rank rather than classify when only pairwise preference labels are available.
npx skillsauth add wenmin-wu/ds-skills nlp-pairwise-margin-ranking-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.
When labels are pairwise preferences ("text A is more toxic than text B") rather than absolute scores, MarginRankingLoss trains a model to produce scalar scores where the preferred item scores higher by at least a margin. Each text is encoded independently through a shared transformer, producing two scalars per pair. The loss penalizes pairs where the "more toxic" score isn't at least margin above the "less toxic" score. This is the standard approach for learning-to-rank with neural encoders.
import torch
import torch.nn as nn
from transformers import AutoModel, AutoTokenizer
class RankingModel(nn.Module):
def __init__(self, model_name):
super().__init__()
self.encoder = AutoModel.from_pretrained(model_name)
self.drop = nn.Dropout(0.2)
self.fc = nn.Linear(self.encoder.config.hidden_size, 1)
def forward(self, ids, mask):
out = self.encoder(input_ids=ids, attention_mask=mask)
return self.fc(self.drop(out.pooler_output))
# Training
criterion = nn.MarginRankingLoss(margin=0.5)
target = torch.ones(batch_size) # more_toxic should score higher
score_more = model(more_toxic_ids, more_toxic_mask)
score_less = model(less_toxic_ids, less_toxic_mask)
loss = criterion(score_more.squeeze(), score_less.squeeze(), 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