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.
Scanned 5/30/2026
Install via CLI
openskills install ECNU-ICALK/AutoSkill---
id: "ee625ebb-bce8-411b-92b4-743078255ce3"
name: "PyTorch Hard Negative Mining and Triplet Loss with Multi-Positive Support"
description: "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."
version: "0.1.0"
tags:
- "pytorch"
- "triplet-loss"
- "hard-negative-mining"
- "metric-learning"
- "deep-learning"
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"
---
# 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
No comments yet. Be the first to comment!