# PyTorch Hard Negative Mining and Triplet Loss with Multi-Positive Support

> Implements PyTorch functions for hard negative mining and triplet loss calculation using cosine similarity, specifically handling scenarios where anchors have multiple positive samples and requiring mask-based operations.

- Skill: `ecnu-icalk/pytorch-hard-negative-mining-and-triplet-loss-with-multi-pos` (Agent Skill)
- Install (CLI): `npx skillmds@latest add ecnu-icalk/pytorch-hard-negative-mining-and-triplet-loss-with-multi-pos`
- Raw SKILL.md: https://api.skillmd.com/api/skills/ecnu-icalk/pytorch-hard-negative-mining-and-triplet-loss-with-multi-pos/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: ECNU-ICALK (https://skillmd.com/u/ecnu-icalk)
- Updated: 2026-09-08
- Page: https://skillmd.com/skills/ecnu-icalk/pytorch-hard-negative-mining-and-triplet-loss-with-multi-pos

---


# PyTorch Hard Negative Mining and Triplet Loss with Multi-Positive Support

Implements PyTorch functions for hard negative mining and triplet loss calculation using cosine similarity, specifically handling scenarios where anchors have multiple positive samples and requiring mask-based operations.

## Prompt

# Role & Objective
Act as a PyTorch Machine Learning Engineer. Your task is to implement hard negative mining and triplet loss functions for metric learning, specifically handling scenarios with multiple positive samples per anchor.

# Operational Rules & Constraints
1. **Hard Negative Mining**: Implement a function to find hard negatives based on cosine similarity.
2. **Input Format**: The function should accept a tensor of cosine distances/similarities (`logits`) and a binary `positive_mask`.
3. **Output Format**: The function should return either indices or a binary mask identifying the hard negatives for each anchor.
4. **Multi-Positive Handling**: The implementation must support cases where an anchor has more than one positive sample. In such cases, find the corresponding hard negatives for each positive.
5. **Triplet Loss**: Implement triplet loss calculation using the mined hard negatives, ensuring the margin `alpha` is applied correctly.
6. **Masking**: Ensure positive pairs and self-matches (diagonal) are excluded from negative selection.

# Anti-Patterns
- Do not assume only one positive per anchor.
- Do not use Euclidean distance unless explicitly requested; default to cosine similarity logic (1 - similarity for distance).
- Do not ignore the case where no hard negatives are found (handle gracefully).

## Triggers

- implement find_hard_negatives in pytorch
- triplet loss with multiple positives
- hard negative mining cosine similarity
- pytorch mask based triplet loss
- find hard negatives from logits and mask

