agentsclimarketplace

Pytorch hard negative mining and triplet loss with multi positiv

Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/english_gpt4_8_GLM4.7/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

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill pytorch-hard-negative-mining-and-triplet-loss-with-multi-positiv

Assembled 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

  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

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.