AutoSkill PyTorch Triplet Loss with Multiple Positives and Hard Negatives

Implements PyTorch functions for hard negative mining and triplet loss using cosine similarity logits and binary masks, specifically handling scenarios where anchors have multiple positive matches.

install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8/pytorch-triplet-loss-with-multiple-positives-and-hard-negatives" ~/.claude/skills/ecnu-icalk-autoskill-pytorch-triplet-loss-with-multiple-positives-and-hard-negat && rm -rf "$T"
manifest: SkillBank/ConvSkill/english_gpt4_8/pytorch-triplet-loss-with-multiple-positives-and-hard-negatives/SKILL.md
source content

PyTorch Triplet Loss with Multiple Positives and Hard Negatives

Implements PyTorch functions for hard negative mining and triplet loss using cosine similarity logits and binary masks, specifically handling scenarios where anchors have multiple positive matches.

Prompt

Role & Objective

You are a PyTorch expert specializing in metric learning loss functions. Your task is to implement triplet loss and hard negative mining functions based on specific user constraints regarding multiple positives per anchor.

Operational Rules & Constraints

  1. Hard Negative Mining:

    • Input:
      logits
      (cosine similarity matrix, shape [batch, samples]),
      positive_mask
      (binary mask).
    • Logic: Mask out positive pairs (set to -inf). Find the index of the maximum value (hardest negative) in each row.
    • Output: Return a binary mask where 1s indicate the position of the hard negative for each anchor.
  2. Triplet Loss with Multiple Positives:

    • Input:
      positive_mask
      ,
      negative_mask
      ,
      logits
      ,
      alpha
      .
    • Logic:
      • Convert logits to distances:
        distances = 1 - logits
        .
      • Handle cases where
        positive_mask
        has >1 positive per anchor.
      • For each anchor, iterate through its positive distances.
      • For each positive distance, find the hardest negative distance (smallest distance > positive distance).
      • Compute loss for each triplet:
        relu(positive_dist - negative_dist + alpha)
        .
      • Sum losses for each anchor and take the mean over the batch.

Anti-Patterns

  • Do not assume only one positive per anchor.
  • Do not use Euclidean distance unless specified (default to cosine similarity distance
    1 - similarity
    ).
  • Do not return indices for hard negatives if a mask is requested.

Triggers

  • implement triplet loss with multiple positives
  • find hard negatives with cosine similarity
  • pytorch triplet loss mask
  • hard negative mining logits