{"data":{"slug":"kevinmusgrave-pytorch-metric-learning","name":"pytorch-metric-learning","tagline":"Easily implement deep metric learning in applications using PyTorch","github_url":"https://github.com/KevinMusgrave/pytorch-metric-learning","owner":"KevinMusgrave","repo":"pytorch-metric-learning","owner_avatar_url":"https://avatars.githubusercontent.com/u/2314891?v=4","primary_language":"Python","stars":6339,"forks":659,"topics":["computer-vision","contrastive-learning","deep-learning","deep-metric-learning","embeddings","image-retrieval","machine-learning","metric-learning","pytorch","self-supervised-learning"],"archived":false,"github_pushed_at":"2025-08-17T17:08:48+00:00","maintenance_label":"Dormant","stars_delta_30d":6,"url":"https://www.graphcanon.com/tools/kevinmusgrave-pytorch-metric-learning","markdown_url":"https://www.graphcanon.com/tools/kevinmusgrave-pytorch-metric-learning.md","api_url":"https://www.graphcanon.com/api/graphcanon/tools/kevinmusgrave-pytorch-metric-learning","graph_url":"https://www.graphcanon.com/api/graphcanon/graph?tool=kevinmusgrave-pytorch-metric-learning","description":"The easiest way to use deep metric learning in your application. Modular, flexible, and extensible. Written in PyTorch.","homepage_url":"https://kevinmusgrave.github.io/pytorch-metric-learning/","license":"MIT","open_issues":77,"watchers":59,"ai_summary":"A flexible and modular framework for deep metric learning that simplifies integration into various machine learning projects, particularly under the PyTorch framework.","readme_excerpt":"<h1>\n<a href=\"https://github.com/KevinMusgrave/pytorch-metric-learning\">\n<img alt=\"PyTorch Metric Learning\" src=\"https://github.com/KevinMusgrave/pytorch-metric-learning/blob/master/docs/imgs/Logo2.png\">\n</a>\n</h1>\n\n<p align=\"center\">\n <a href=\"https://pypi.org/project/pytorch-metric-learning\">\n     <img alt=\"PyPi version\" src=\"https://img.shields.io/pypi/v/pytorch-metric-learning?color=bright-green\">\n </a>\n\t\n\t\n \n <a href=\"https://anaconda.org/conda-forge/pytorch-metric-learning\">\n     <img alt=\"Anaconda version\" src=\"https://img.shields.io/conda/v/conda-forge/pytorch-metric-learning?color=bright-green\">\n </a>\n</p>\n\n## News\n\n**August 17**: v2.9.0\n- Added [SmoothAPLoss](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#smoothaploss).\n- Improved SubCenterArcFaceLoss and GenericPairLoss.\n- Thank you [ir2718](https://github.com/ir2718), [lucamarini22](https://github.com/lucamarini22), and [marcpaga](https://github.com/marcpaga).\n\n**December 11**: v2.8.0\n- Added the [Datasets](https://kevinmusgrave.github.io/pytorch-metric-learning/datasets) module for easy downloading of common datasets:\n  - [CUB200](https://kevinmusgrave.github.io/pytorch-metric-learning/datasets/#cub-200-2011)\n  - [Cars196](https://kevinmusgrave.github.io/pytorch-metric-learning/datasets/#cars196)\n  - [INaturalist 2018](https://kevinmusgrave.github.io/pytorch-metric-learning/datasets/#inaturalist2018)\n  - [Stanford Online Products](https://kevinmusgrave.github.io/pytorch-metric-learning/datasets/#stanfordonlineproducts)\n- Thank you [ir2718](https://github.com/ir2718).\n\n## Documentation\n- [**View the documentation here**](https://kevinmusgrave.github.io/pytorch-metric-learning/)\n- [**View the installation instructions here**](https://github.com/KevinMusgrave/pytorch-metric-learning#installation)\n- [**View the available losses, miners etc. here**](https://github.com/KevinMusgrave/pytorch-metric-learning/blob/master/CONTENTS.md) \n\n\n## Google Colab Examples\nSee the [examples folder](https://github.com/KevinMusgrave/pytorch-metric-learning/blob/master/examples/README.md) for notebooks you can download or run on Google Colab.\n\n\n## PyTorch Metric Learning Overview\nThis library contains 9 modules, each of which can be used independently within your existing codebase, or combined together for a complete train/test workflow.\n\n\n\n\n\n## How loss functions work\n\n### Using losses and miners in your training loop\nLet’s initialize a plain [TripletMarginLoss](https://kevinmusgrave.github.io/pytorch-metric-learning/losses/#tripletmarginloss):\n```python\nfrom pytorch_metric_learning import losses\nloss_func = losses.TripletMarginLoss()\n```\n\nTo 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.\n\n```python\n# your training loop\nfor i, (data, labels) in enumerate(dataloader):\n\toptimizer.zero_grad()\n\tembeddings = model(data)\n\tloss = loss_func(embeddings, labels)\n\tloss.backward()\n\toptimizer.step()\n```\n\nThe 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. \n\nSometimes it can help to add a mining function:\n```python\nfrom pytorch_metric_learning import miners, losses\nminer = miners.MultiSimilarityMiner()\nloss_func = losses.TripletMarginLoss()\n\n# your training loop\nfor i, (data, labels) in enumerate(dataloader):\n\toptimizer.zero_grad()\n\tembeddings = model(data)\n\thard_pairs = miner(embeddings, labels)\n\tloss = loss_func(embeddings, labels, hard_pairs)\n\tloss.backward()\n\toptimizer.step()\n```\nIn 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","github_created_at":"2019-10-23T17:20:35+00:00","created_at":"2026-07-11T11:28:33.802352+00:00","updated_at":"2026-08-22T00:01:30.092712+00:00","categories":[{"slug":"data-retrieval","name":"Data & Retrieval","url":"https://www.graphcanon.com/categories/data-retrieval","markdown_url":"https://www.graphcanon.com/categories/data-retrieval.md","api_url":"https://www.graphcanon.com/api/graphcanon/categories/data-retrieval"},{"slug":"model-training","name":"Model Training","url":"https://www.graphcanon.com/categories/model-training","markdown_url":"https://www.graphcanon.com/categories/model-training.md","api_url":"https://www.graphcanon.com/api/graphcanon/categories/model-training"}],"tags":[{"slug":"computer-vision","name":"computer-vision"},{"slug":"contrastive-learning","name":"contrastive-learning"},{"slug":"deep-learning","name":"deep-learning"},{"slug":"embeddings","name":"embeddings"},{"slug":"image-retrieval","name":"image-retrieval"},{"slug":"metric-learning","name":"metric-learning"},{"slug":"pytorch","name":"pytorch"},{"slug":"self-supervised-learning","name":"self-supervised-learning"}],"trust":{"provenance":{"is_fork":false,"github_id":217113590,"owner_type":"User","methodology":"github_public_v1","parent_repo":null,"near_duplicate_slugs":[]},"computed_at":"2026-08-22T00:01:29.283Z","maintenance":{"label":"Dormant","score":18,"methodology":"github_public_v1","releases_90d":0,"days_since_push":369,"last_release_at":"2025-08-17T17:07:40Z","stars_delta_30d":6,"open_issues_delta_30d":0},"security_summary":{"status":"no_lockfile","scanner":null,"low_count":0,"high_count":0,"last_scan_at":"2026-07-11T11:28:34.901Z","medium_count":0,"scan_profile":"none","critical_count":0}},"capability_facts":{"scan":{"source":"repo_scan","observed_at":"2026-08-22T00:01:29.764Z"},"languages":{"value":["python"],"source":"github.language","observed_at":"2026-08-22T00:01:29.764Z"},"license_spdx":{"value":"MIT","source":"github.license","observed_at":"2026-08-22T00:01:29.764Z"}},"decision_facts":{"hosting":{"model":"library","summary":"Provides functions for implementing deep metric learning models within PyTorch."},"pricing":{"model":"freemium","summary":"Free to use under the MIT license, with no direct costs but may require resource investment for implementation and support."},"requirements":null,"constraints":{"hosting_model":"library","pricing_model":"freemium"},"when_to_use":["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.","If your project involves a large dataset that benefits from self-supervised learning, reducing the need for labeled data."],"when_not_to_use":["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."],"source":"enrich:decision_facts","observed_at":"2026-07-12T11:00:00.203Z"},"constraint_facets":{"hosting_model":"library","pricing_model":"freemium"},"decision_summary":[{"label":"Hosting","value":"library - Provides functions for implementing deep metric learning models within PyTorch."},{"label":"Pricing","value":"freemium - Free to use under the MIT license, with no direct costs but may require resource investment for implementation and support."},{"label":"Adopt for","value":"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."}]}}