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

Metadata

Release files for torchcriterion 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torchcriterion 0.2.0
File Size Uploaded Provenance Actions
torchcriterion-0.2.0.tar.gz 7.2 kB Without provenance Details for torchcriterion-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for torchcriterion 0.2.0
File Interpreter ABI Platform Actions
torchcriterion-0.2.0-py3-none-any.whl Python 3 none any Details for torchcriterion-0.2.0-py3-none-any.whl

Total release size: 14.4 kB

Release files / torchcriterion-0.2.0.tar.gz

Download URL torchcriterion-0.2.0.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
606058fcb9dd6283ad8ac3ae9dc03813448e89d462e5a065eda1859bc8162998
BLAKE2b-256 checksum
How to use checksums
e4b1c32b4822a80cfb67ada68cb5b1a8cc55cc2dfeff5148b072711b716039c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release files / torchcriterion-0.2.0-py3-none-any.whl

Download URL torchcriterion-0.2.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3bd8f6f76167fadbb48359a5ccf3c6e2fd76a72fb57a228729c17605e82e28e3
BLAKE2b-256 checksum
How to use checksums
05730d3d4515c3e522537ce30a72f3b449aef92ecf934b77bb2912251eba09a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Security for torchcriterion 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

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