--- 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())