AutoSkill 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.
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_GLM4.7/pytorch-hard-negative-mining-and-triplet-loss-with-multi-positiv" ~/.claude/skills/ecnu-icalk-autoskill-pytorch-hard-negative-mining-and-triplet-loss-with-multi-po && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8_GLM4.7/pytorch-hard-negative-mining-and-triplet-loss-with-multi-positiv/SKILL.mdsource content
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 (
) and a binarylogits
.positive_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
is applied correctly.alpha - 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