MIT License

项目介绍

KevinMusgrave/pytorch-metric-learning 是一个托管在 GitHub 上的开源项目,主要使用 Python 编写,采用 MIT License 许可证,目前获得 6.3k 个 Star、663 次 Fork。

仓库全名
KevinMusgrave/pytorch-metric-learning
开源许可证
MIT License
Star 数
6.3k
Fork 数
663
未关闭 Issue
3

README 摘要

Train embedding models with composable PyTorch losses, hard-example miners, distances, reducers, and regularizers. Use them in your own training loop for retrieval, clustering, recognition, or learning from pairs and triplets. Docs for humans · Documentation source Install Install from PyPI with uv or pip: The core package needs only PyTorch and NumPy. Optional extras add the Faiss retrieval backend,…

完整内容见 GitHub 原始 README 或本页 。

常见问题

pytorch-metric-learning 用什么语言开发?

主要使用 Python 编写,采用 MIT License 许可证,目前有 6.3k 个 Star。

pytorch-metric-learning 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 pytorch-metric-learning 的源码?

可以直接克隆仓库:git clone https://github.com/KevinMusgrave/pytorch-metric-learning.git,默认分支为 master;安装与运行步骤见仓库 README。

pytorch-metric-learning 最近还在维护吗?

仓库最近一次更新为 6 天前,当前有 3 个未关闭 Issue;Star 数为 6.3k,可作为社区活跃度的参考。

README

PyTorch Metric Learning

Train embedding models with composable PyTorch losses, hard-example miners, distances, reducers, and regularizers. Use them in your own training loop for retrieval, clustering, recognition, or learning from pairs and triplets.

Docs for humans · Documentation source

How embeddings flow through miners, losses, reducers, and retrieval metrics.

Install

Install from PyPI with uv or pip:

uv pip install pytorch-metric-learning
pip install pytorch-metric-learning

The core package needs only PyTorch and NumPy. Optional extras add the Faiss retrieval backend, pytorch-metric-learning[faiss], and scikit-learn for the NMI/AMI clustering metrics, pytorch-metric-learning[clustering].

Version 3 requires Python 3.12 or newer, PyTorch 2.14.0 or newer, and NumPy 2.5.3 or newer. Upgrading from version 2? Read the release notes and look up old names in the migration guide. The version 2 documentation remains available.

A minimal training step

Your model produces one embedding per input. Equal labels identify positive pairs; different labels identify negatives. Here a contrastive loss pulls positive pairs together and pushes negatives apart:

import torch
from pytorch_metric_learning import losses

torch.manual_seed(17)
model = torch.nn.Linear(8, 4)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
data = torch.randn(12, 8)
labels = torch.arange(4).repeat_interleave(3)
loss_func = losses.ContrastiveLoss(neg_margin=1.4)

optimizer.zero_grad(set_to_none=True)
loss = loss_func(model(data), labels)
loss.backward()
optimizer.step()

The model returns embeddings shaped (12, 4) and labels have shape (12,). This loss L2-normalizes embeddings by default. It averages active positive and negative pair penalties separately, then adds them. See the contrastive-loss contract for distances, margins, and reduction options.

Learn more

The documentation starts with a task index and a concepts overview of embeddings, tuples, distances, loss terms, and reducers. Every component page states its signature, defaults, tensor contract, and paper, and the recipes show complete training and evaluation code.

Contributing

See CONTRIBUTING.md. Thanks to all contributors, upstream projects, and supporters.

Citing this library

If you use this library in your research, please cite the PyTorch Metric Learning paper:

@article{Musgrave2020PyTorchML,
  title={PyTorch Metric Learning},
  author={Kevin Musgrave and Serge J. Belongie and Ser-Nam Lim},
  journal={ArXiv},
  year={2020},
  volume={abs/2008.09164}
}

评论

暂无评论