pytorch-metric-learning
The easiest way to use deep metric learning in your application. Modular, flexible, and extensible. Written in PyTorch.
Decision gist · record as of 2026-08-14
Yes, with a minor caveat on maintenance timing. The library is well-established (6336 stars, top 5000 PyPI), permissively licensed, and has low install friction. Its modular design and comprehensive loss/miner suite make it the standard choice for metric learning in PyTorch. However, the last release was 362 days ago; if you need very recent bug fixes or features, verify that the current version addresses your use case or check the development branch.AI-flagged interpretation of the facts on this page — verify before relying
Before you install
- Requires torch >= 1.6 for pytorch-metric-learning >= v0.9.90; earlier versions have no strict torch version requirement but were tested with torch >= 1.2.
- Low friction installation with four core runtime dependencies (numpy, scikit-learn, torch, tqdm).
- Maintenance status is aging—last release was 362 days ago—but the repository remains active with 6336 stars and no archived status.
License · maintenance · safety
permissive license (permissive) — MIT license permits commercial and private use with minimal restrictions, making it suitable for most projects without legal concern.
last release 2025-08-17 (362 days) · last repo commit 2025-08-17 · 6,336 stars
0 known vulnerabilities (OSV.dev, 2026-08-14) · 3,692,753 downloads/mo, #2,526 on PyPI
Alternatives
Verify before relying
pip install pytorch-metric-learning
from pytorch_metric_learning import losses
loss_func = losses.TripletMarginLoss()
# In training loop:
embeddings = model(data)
loss = loss_func(embeddings, labels)
loss.backward()- Whether the library's 9 modules can all be used independently or if some have undocumented interdependencies.
- Performance characteristics or scalability limits when training on very large batches or high-dimensional embeddings.
- Whether optional dependencies (faiss-gpu, faiss-cpu, record-keeper, tensorboard) are truly optional or required for core functionality.
What it is and what it does
pytorch-metric-learning is a PyTorch library for training neural networks to produce embeddings where semantically similar inputs are close together and dissimilar ones are far apart. It provides loss functions (TripletMarginLoss, SmoothAPLoss, ArcFace variants, and others), hard-pair miners that identify difficult training examples, distance metrics, and evaluation tools. The library is modular: each component—losses, miners, distances, reducers, regularizers—can be used independently or composed together. It supports both supervised learning (where labels define similarity) and self-supervised approaches like MoCo-style momentum encoding.
You integrate it into a standard PyTorch training loop by computing embeddings from your model, passing them to a loss function along with labels, and optionally using a miner to focus on hard examples. The library handles the combinatorial complexity of forming triplets or pairs from batches automatically. It includes convenience modules for dataset downloading (CUB200, Cars196, INaturalist, Stanford Online Products), training workflows, and accuracy calculation on standard benchmarks.
Use it for
- Train face recognition or person re-identification models where embeddings must cluster by identity.
- Build product recommendation systems by learning embeddings where similar items are nearby in embedding space.
- Implement image retrieval systems where query and gallery images are ranked by embedding similarity.
- Train self-supervised models using contrastive objectives without labeled data.
- Evaluate embedding quality on standard benchmarks using built-in accuracy calculators and testers.
Worth the install?
AI-flagged interpretation of the facts on this page. Verify before relying on it.
Yes, with a minor caveat on maintenance timing.
The library is well-established (6336 stars, top 5000 PyPI), permissively licensed, and has low install friction. Its modular design and comprehensive loss/miner suite make it the standard choice for metric learning in PyTorch. However, the last release was 362 days ago; if you need very recent bug fixes or features, verify that the current version addresses your use case or check the development branch.
Install
pytorch-metric-learning on PyPI
Before you install
Low friction installation with four core runtime dependencies (numpy, scikit-learn, torch, tqdm). Maintenance status is aging—last release was 362 days ago—but the repository remains active with 6336 stars and no archived status.
Requires torch >= 1.6 for pytorch-metric-learning >= v0.9.90; earlier versions have no strict torch version requirement but were tested with torch >= 1.2.
License in practice
MIT license permits commercial and private use with minimal restrictions, making it suitable for most projects without legal concern.
Quickstart
pip install pytorch-metric-learning
from pytorch_metric_learning import losses
loss_func = losses.TripletMarginLoss()
# In training loop:
embeddings = model(data)
loss = loss_func(embeddings, labels)
loss.backward()
Verify before relying
- Whether the library's 9 modules can all be used independently or if some have undocumented interdependencies.
- Performance characteristics or scalability limits when training on very large batches or high-dimensional embeddings.
- Whether optional dependencies (faiss-gpu, faiss-cpu, record-keeper, tensorboard) are truly optional or required for core functionality.
Package facts
| License | permissive license permissive |
| Python support | Supports the current Python release >=3.0 |
| Install friction | Low. Pure-Python wheel |
| Runtime dependencies | 4 packagesnumpyscikit-learntorchtqdm |
| Maintenance | Aging 362 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 3,692,753 / month, #2,526 on PyPI 30-day window, as of 2026-08-14 |
| Known vulnerabilities | None known OSV.dev, checked 2026-08-14 |
| Classifiers | License :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: Python :: 3 |
Evidence: pytorch_metric_learning-2.9.0-py3-none-any.whl
Tags
Let your AI agent find packages like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 14,416 PyPI packages by what they can do, searchable in plain language.
wish › “metric learning losses pytorch”
- pytorch-metric-learningProvides metric learning loss functions, miners, and evaluation tools…
- raxRax provides ranking losses and metrics for learning-to-rank problems…
- torch-stoiComputes Short Term Objective Intelligibility (STOI) as a PyTorch…
Give your agent the search over MCP, or paste the wish link into any chat.
More Artificial Intelligence packages
LiteLLM provides a unified Python interface to call 100+ LLM providers (OpenAI, Anthropic, Gemini, Bedrock, Azure, and others) using OpenAI-compatible API format, available as both a Python SDK and a self-hosted AI Gateway proxy server.
Install it if you need to work with multiple LLM providers or want to centralize LLM routing in your organization.
Client library and CLI tool for downloading, uploading, and managing models, datasets, and repositories on the Hugging Face Hub platform.
Install it if you work with Hugging Face Hub models or datasets.
LangChain provides a framework for building agents and LLM-powered applications by composing language models, tools, and memory through a unified API that abstracts over multiple model providers.
hf-xet provides chunk-based deduplication and efficient file transfer for the Hugging Face Hub, enabling faster uploads and downloads of large files with local disk caching.
Tokenizers converts raw text into token sequences for NLP models, with support for training custom vocabularies and using pre-built tokenizers (BPE, WordPiece) optimized for speed via Rust.
Transformers provides a unified framework for loading, fine-tuning, and running state-of-the-art pretrained models across text, vision, audio, video, and multimodal tasks using PyTorch, JAX, or TensorFlow.
Install it if you need to run or train any transformer-based model for NLP, vision, audio, or multimodal tasks.
See also cut-cross-entropy · rax · lpips · torch · trainer · torch-stoi · entmax · pytorch_revgrad · torchmetrics · geomloss