A modular PyTorch loss function library with popular criteria for classification, regression, segmentation, and metric learning.
Project description
TorchCriterion
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
CrossEntropyLossFocalLoss
Regression
MSELossHuberLoss
Segmentation
DiceLossTverskyLoss
Metric Learning
TripletLossContrastiveLoss
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
606058fcb9dd6283ad8ac3ae9dc03813448e89d462e5a065eda1859bc8162998
|
|
| MD5 |
7e3cdd728096dbf92a6d024278f34594
|
|
| BLAKE2b-256 |
e4b1c32b4822a80cfb67ada68cb5b1a8cc55cc2dfeff5148b072711b716039c1
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3bd8f6f76167fadbb48359a5ccf3c6e2fd76a72fb57a228729c17605e82e28e3
|
|
| MD5 |
12658bcc5dba70d5ee7a1910447f9773
|
|
| BLAKE2b-256 |
05730d3d4515c3e522537ce30a72f3b449aef92ecf934b77bb2912251eba09a8
|