Skip to content
Back to skills

Pytorch Triplet Loss With Multiple Positives And Hard Negatives

ASecurity

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.

  • 611 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 11, 2026
educationgit

Works with

  • cli

Security analysis

A100/100

Scanned November 11, 2026

npx -y skills add ECNU-ICALK/AutoSkill --skill pytorch-triplet-loss-with-multiple-positives-and-hard-negatives --agent claude-code

Installs 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 with every re-scan.

Security grade badge for Pytorch Triplet Loss With Multiple Positives And Hard Negatives
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/ecnu-icalk-pytorch-triplet-loss-with-multiple-positives-and-h/badge)](https://www.skillsdirectory.com/skills/ecnu-icalk-pytorch-triplet-loss-with-multiple-positives-and-h)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in education →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.