Pairwise margin ranking loss

From wenmin-wu/ds-skills · View interactive version

Pairwise margin ranking loss is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Trains a transformer with MarginRankingLoss on text pairs (more/less toxic), learning to rank rather than classify when only pairwise preference labels are available.

How to use this skill

Skill file

Pairwise Margin Ranking Loss

Overview

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.

Quick Start

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)

Workflow

  1. Tokenize both texts in each pair independently
  2. Forward each through the shared encoder to get scalar scores
  3. Compute MarginRankingLoss(score_more, score_less, target=1)
  4. At inference, rank all texts by their scalar score

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.