Pytorch hard negative mining and triplet loss with multi positiv
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.From its SKILL.md
npx -y skills add ECNU-ICALK/AutoSkill --skill pytorch-hard-negative-mining-and-triplet-loss-with-multi-positivAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
SKILL.md
2.5 KB, 359 tokens by cl100k_base, as published. Nobody here has run it
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
- Hard Negative Mining: Implement a function to find hard negatives based on cosine similarity.
- Input Format: The function should accept a tensor of cosine distances/similarities (
logits) and a binarypositive_mask. - Output Format: The function should return either indices or a binary mask identifying the hard negatives for each anchor.
- 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.
- Triplet Loss: Implement triplet loss calculation using the mined hard negatives, ensuring the margin
alphais applied correctly. - 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
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.