File size: 1,636 Bytes
d191cb2 6b75f89 d191cb2 01a6f4b d191cb2 01a6f4b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
---
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()) |