Skip to main content

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

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.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page