|
|
--- |
|
|
library_name: torch |
|
|
license: apache-2.0 |
|
|
tags: |
|
|
- pytorch |
|
|
- loss-functions |
|
|
- deep-learning |
|
|
- machine-learning |
|
|
- library |
|
|
--- |
|
|
|
|
|
# torchlosses |
|
|
|
|
|
A lightweight library of advanced **PyTorch loss functions** β ready to plug into your training loop. |
|
|
Designed for deep learning practitioners who want more than just MSE and CrossEntropy. |
|
|
|
|
|
Available on **PyPI**: [torchlosses](https://pypi.org/project/torchlosses/) |
|
|
|
|
|
--- |
|
|
|
|
|
## Features |
|
|
- **Focal Loss** β handle class imbalance in classification. |
|
|
- **Dice Loss** β segmentation-friendly overlap metric. |
|
|
- **Contrastive Loss** β learn pairwise similarity (Siamese nets). |
|
|
- **Triplet Loss** β enforce anchor-positive vs negative separation. |
|
|
- **Cosine Embedding Loss** β similarity learning with cosine distance. |
|
|
- **Huber Loss** β robust regression, less sensitive to outliers. |
|
|
- **KL Divergence Loss** β probability distribution alignment. |
|
|
|
|
|
--- |
|
|
|
|
|
## Created By |
|
|
|
|
|
Naga Adithya Kaushik (GenAIDevTOProd) |
|
|
https://medium.com/@GenAIDevTOProd/from-loss-functions-to-training-utilities-building-pytorch-packages-from-scratch-91e884d14001 |
|
|
|
|
|
## Installation |
|
|
|
|
|
```bash |
|
|
pip install torchlosses |
|
|
|
|
|
## Usage |
|
|
|
|
|
import torch |
|
|
from torchlosses import FocalLoss, DiceLoss |
|
|
|
|
|
# Focal Loss for classification |
|
|
inputs = torch.randn(4, 5, requires_grad=True) # logits for 5 classes |
|
|
targets = torch.randint(0, 5, (4,)) |
|
|
criterion = FocalLoss() |
|
|
loss = criterion(inputs, targets) |
|
|
print("Focal Loss:", loss.item()) |
|
|
|
|
|
# Dice Loss for segmentation |
|
|
inputs = torch.randn(4, 1, 8, 8) |
|
|
targets = torch.randint(0, 2, (4, 1, 8, 8)) |
|
|
criterion = DiceLoss() |
|
|
loss = criterion(inputs, targets) |
|
|
print("Dice Loss:", loss.item()) |