pytorch-metric-learning
Easily implement deep metric learning in applications using PyTorch
GraphCanon updated 3d · GitHub synced 3d
Decision brief
PyTorch Metric Learning is specifically tailored for those leveraging PyTorch and interested in applications that require distance-based learning approaches like computer vision or self-supervised learning tasks.
Good fit when
- When you are working with the PyTorch framework and intend to implement deep metric learning techniques.
- For projects requiring image retrieval or classification where embeddings generated through contrastive learning can improve performance.
Avoid when
- Avoid if you are not working within the PyTorch framework and prefer to use another deep learning library as this tool is tightly integrated with PyTorch.
- If your project requires a less modular setup, where customization might be more cumbersome due to pytorch-metric-learning's design towards flexibility and modularity.
- Hosting:
- library - Provides functions for implementing deep metric learning models within PyTorch.
- Pricing:
- freemium - Free to use under the MIT license, with no direct costs but may require resource investment for implementation and support.
Observed Jul 12, 2026 · Source: enrich:decision_facts
Verify the decision
Maintenance and security
Full trust report- Maintenance
- Dormant (369d since push)
- As of 3d
- Provenance
- Not a fork · Personal account
- As of 3d
- Security (OSV)
- No lockfile
- As of 1mo
Public GitHub metadata and optional OSV scans. Signals, not a guarantee. Trust methodology.
Install
pip install pytorch-metric-learning PyPIHow it fits your stack(1)
Typed graph edges - alternatives, integrations, successors, and dependencies. Ranked by relationship type, not raw GitHub stars.
Relationship graph
Optional deeper exploration of typed edges and category neighbours.
Similar tools
Same-category neighbours not already linked as typed edges.
Evidence and technical details
Sourced facts, taxonomy, compatibility claims, README excerpt, and machine-readable endpoints.
Overview
A flexible and modular framework for deep metric learning that simplifies integration into various machine learning projects, particularly under the PyTorch framework.
Capability facts
- Languages
- python
Source: github.language · Aug 22, 2026
Categories
Compatibility
Sourced claims from the README excerpt - not unsourced marketing copy.
Source: README excerpt (regex_v1, Aug 22, 2026)
```python from pytorch_metric_learning import lossesSource link
Tags
README
News
August 17: v2.9.0
- Added SmoothAPLoss.
- Improved SubCenterArcFaceLoss and GenericPairLoss.
- Thank you ir2718, lucamarini22, and marcpaga.
December 11: v2.8.0
Documentation
- View the documentation here
- View the installation instructions here
- View the available losses, miners etc. here
Google Colab Examples
See the examples folder for notebooks you can download or run on Google Colab.
PyTorch Metric Learning Overview
This library contains 9 modules, each of which can be used independently within your existing codebase, or combined together for a complete train/test workflow.
How loss functions work
Using losses and miners in your training loop
Let’s initialize a plain TripletMarginLoss:
from pytorch_metric_learning import losses
loss_func = losses.TripletMarginLoss()
To compute the loss in your training loop, pass in the embeddings computed by your model, and the corresponding labels. The embeddings should have size (N, embedding_size), and the labels should have size (N), where N is the batch size.
# your training loop
for i, (data, labels) in enumerate(dataloader):
optimizer.zero_grad()
embeddings = model(data)
loss = loss_func(embeddings, labels)
loss.backward()
optimizer.step()
The TripletMarginLoss computes all possible triplets within the batch, based on the labels you pass into it. Anchor-positive pairs are formed by embeddings that share the same label, and anchor-negative pairs are formed by embeddings that have different labels.
Sometimes it can help to add a mining function:
from pytorch_metric_learning import miners, losses
miner = miners.MultiSimilarityMiner()
loss_func = losses.TripletMarginLoss()
# your training loop
for i, (data, labels) in enumerate(dataloader):
optimizer.zero_grad()
embeddings = model(data)
hard_pairs = miner(embeddings, labels)
loss = loss_func(embeddings, labels, hard_pairs)
loss.backward()
optimizer.step()
In the above code, the miner finds positive and negative pairs that it thinks are particularly difficult. Note that even though the TripletMarginLoss operates on triplets, it’s still possible to pass in pairs. This
For agents
This page has a .md twin and JSON over the API.