Batch all contrastive loss

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

Batch all contrastive loss is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. All-vs-all contrastive loss comparing every pair in a batch (N^2 pairs) with margin and compactification regularizer

How to use this skill

Skill file

Batch-All Contrastive Loss

Overview

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.

Quick Start

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

Workflow

  1. Forward batch through embedding model
  2. Compute all N^2 pairwise squared distances
  3. Split into positive pairs (same label) and negative pairs (different label)
  4. Positive loss = squared distance; negative loss = relu(margin - distance)^2
  5. Average only non-zero terms + compactification regularizer

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.