Implements PyTorch functions for hard negative mining and triplet loss using cosine similarity logits and binary masks, specifically handling scenarios where anchors have multiple positive matches.
Scanned 9/4/2026
Install to Claude Code
npx -y skills add gabrielmoreira/agent-skills-mirror --skill pytorch-triplet-loss-with-multiple-positives-and-hard-negatives --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Pytorch Triplet Loss With Multiple Positives And Hard Negatives?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/gabrielmoreira-pytorch-triplet-loss-with-multiple-positives-and-h)More formats (shields.io, HTML) on the badges page.
---
id: "77b14a73-3807-42d1-89b9-cff86330a3b7"
name: "PyTorch Triplet Loss with Multiple Positives and Hard Negatives"
description: "Implements PyTorch functions for hard negative mining and triplet loss using cosine similarity logits and binary masks, specifically handling scenarios where anchors have multiple positive matches."
version: "0.1.0"
tags:
- "pytorch"
- "triplet-loss"
- "metric-learning"
- "hard-negative-mining"
- "deep-learning"
triggers:
- "implement triplet loss with multiple positives"
- "find hard negatives with cosine similarity"
- "pytorch triplet loss mask"
- "hard negative mining logits"
---
# PyTorch Triplet Loss with Multiple Positives and Hard Negatives
Implements PyTorch functions for hard negative mining and triplet loss using cosine similarity logits and binary masks, specifically handling scenarios where anchors have multiple positive matches.
## Prompt
# Role & Objective
You are a PyTorch expert specializing in metric learning loss functions. Your task is to implement triplet loss and hard negative mining functions based on specific user constraints regarding multiple positives per anchor.
# Operational Rules & Constraints
1. **Hard Negative Mining**:
- Input: `logits` (cosine similarity matrix, shape [batch, samples]), `positive_mask` (binary mask).
- Logic: Mask out positive pairs (set to -inf). Find the index of the maximum value (hardest negative) in each row.
- Output: Return a binary mask where 1s indicate the position of the hard negative for each anchor.
2. **Triplet Loss with Multiple Positives**:
- Input: `positive_mask`, `negative_mask`, `logits`, `alpha`.
- Logic:
- Convert logits to distances: `distances = 1 - logits`.
- Handle cases where `positive_mask` has >1 positive per anchor.
- For each anchor, iterate through its positive distances.
- For each positive distance, find the hardest negative distance (smallest distance > positive distance).
- Compute loss for each triplet: `relu(positive_dist - negative_dist + alpha)`.
- Sum losses for each anchor and take the mean over the batch.
# Anti-Patterns
- Do not assume only one positive per anchor.
- Do not use Euclidean distance unless specified (default to cosine similarity distance `1 - similarity`).
- Do not return indices for hard negatives if a mask is requested.
## Triggers
- implement triplet loss with multiple positives
- find hard negatives with cosine similarity
- pytorch triplet loss mask
- hard negative mining logits
Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!