Skip to main content

A modular PyTorch loss function library with popular criteria for classification, regression, segmentation, and metric learning.

Project description

TorchCriterion

TorchCriterion Logo

torchcriterion is a modular, extensible library of PyTorch-compatible loss functions ("criteria") for classification, regression, segmentation, and metric learning tasks. It offers a curated collection of both standard and custom loss functions, built with flexibility and composability in mind.


๐Ÿš€ Features

  • ๐Ÿงฑ Modular architecture for clean API and extension
  • ๐Ÿงช Ready-to-use implementations of popular losses
  • ๐Ÿงฉ Supports multi-loss composition and custom scheduling
  • โšก Fully compatible with PyTorchโ€™s autograd and GPU acceleration

๐Ÿ“ฆ Installation

pip install torchcriterion

๐Ÿงฐ Supported Losses

Classification

  • CrossEntropyLoss
  • FocalLoss

Regression

  • MSELoss
  • HuberLoss

Segmentation

  • DiceLoss
  • TverskyLoss

Metric Learning

  • TripletLoss
  • ContrastiveLoss

NLP Metrics (new)

Under torchcriterion.nlp we provide common NLP evaluation metrics.

Example usage:

from torchcriterion.nlp.bleu import bleu_score
from torchcriterion.nlp.rouge import rouge_l_batch
from torchcriterion.nlp.perplexity import perplexity

preds = ["the cat sat on mat"]
refs  = ["the cat is on the mat"]

print("BLEU:", bleu_score(preds, refs))
print("ROUGE-L:", rouge_l_batch(preds, refs))

# LM perplexity example
logits = model_out  # (batch, seq_len, vocab)
targets = labels    # (batch, seq_len)
print("Perplexity:", perplexity(logits, targets))

๐Ÿงช Example Usage

from torchcriterion import FocalLoss

criterion = FocalLoss(gamma=2.0, alpha=0.25)
loss = criterion(predictions, targets)

๐Ÿ“ Project Structure

torchcriterion/
โ”œโ”€โ”€ classification/
โ”‚   โ”œโ”€โ”€ cross_entropy.py
โ”‚   โ”œโ”€โ”€ focal.py
โ”œโ”€โ”€ regression/
โ”‚   โ”œโ”€โ”€ mse.py
โ”‚   โ”œโ”€โ”€ huber.py
โ”œโ”€โ”€ segmentation/
โ”‚   โ”œโ”€โ”€ dice.py
โ”‚   โ”œโ”€โ”€ tversky.py
โ”œโ”€โ”€ metric_learning/
โ”‚   โ”œโ”€โ”€ triplet.py
โ”‚   โ”œโ”€โ”€ contrastive.py
โ”œโ”€โ”€ nlp/
โ”‚   โ”œโ”€โ”€ bleu.py
โ”‚   โ”œโ”€โ”€ perplexity.py
โ”‚   โ”œโ”€โ”€ rouge.py
โ”œโ”€โ”€ base.py
โ”œโ”€โ”€ __init__.py

๐Ÿ“œ License

This project is licensed under the MIT License. See the LICENSE file for details.


๐Ÿ™Œ Contributing

Pull requests, ideas, and issues are welcome! Feel free to open a PR or start a discussion.


๐Ÿ‘ค Author

Developed by Liodon AI


โญ๏ธ Star the Repo

If you find this library useful, please consider starring it to show your support!


๐Ÿ”— Related Projects

  • torchmetrics โ€” for evaluation metrics
  • timm โ€” for models with built-in loss support

๐Ÿ“š Citation

If you use TorchCriterion in your research or project, please consider citing it:

@misc{torchcriterion2025,
  title        = {TorchCriterion: Advanced Loss Functions for PyTorch},
  year         = {2025},
  publisher    = {GitHub},
  howpublished = {\url{https://github.com/Liodon-AI/TorchCriterion}},
  note         = {GitHub repository},
}

Made with โค๏ธ and PyTorch

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchcriterion-0.2.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchcriterion-0.2.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file torchcriterion-0.2.0.tar.gz.

File metadata

  • Download URL: torchcriterion-0.2.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for torchcriterion-0.2.0.tar.gz
Algorithm Hash digest
SHA256 606058fcb9dd6283ad8ac3ae9dc03813448e89d462e5a065eda1859bc8162998
MD5 7e3cdd728096dbf92a6d024278f34594
BLAKE2b-256 e4b1c32b4822a80cfb67ada68cb5b1a8cc55cc2dfeff5148b072711b716039c1

See more details on using hashes here.

File details

Details for the file torchcriterion-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: torchcriterion-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for torchcriterion-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3bd8f6f76167fadbb48359a5ccf3c6e2fd76a72fb57a228729c17605e82e28e3
MD5 12658bcc5dba70d5ee7a1910447f9773
BLAKE2b-256 05730d3d4515c3e522537ce30a72f3b449aef92ecf934b77bb2912251eba09a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page