Skip to main content

A loss function for

Project description

Focal Cross Entropy Loss

A PyTorch-compatible implementation of the Focal Cross Entropy loss function, designed to address class imbalance by down-weighting easy examples and focusing training on hard negatives.

Install

pip install focal_ce_loss_funcs_pytorch

Quick start

from focal_ce_loss import FocalCELoss

criterion = FocalCELoss()
loss = criterion(inputs, targets)

FocalCELoss

A PyTorch nn.Module implementing the Focal Cross Entropy loss.

from focal_ce_loss import FocalCELoss

criterion = FocalCELoss(alpha=0.25, gamma=2.0, reduction="sum")

# Compute loss from logits and targets
loss = criterion(inputs, targets)
loss.backward()

Parameters

  • gamma: focusing parameter; higher values down-weight easy examples more aggressively (default: 2.0)
  • alpha: optional class weight for additional class balancing (default: 0.25)
  • reduction: specifies the reduction to apply — "mean", "sum", or "none" (default: "sum")
  • ignore_index: target value to ignore during loss computation (default: -100)

Methods

  • forward(input, target) — accepts a torch.Tensor of shape (N, C) and integer targets of shape (N,); returns a scalar loss tensor (or per-sample tensor if reduction="none")

Inputs

  • input: torch.Tensor of shape (N, C)
  • target: torch.Tensor of shape (N,) ` Example
import torch
from focal_ce_loss import FocalCELoss

inputs  = torch.randn(8, 10)          # batch of 8, 10 classes
targets = torch.randint(0, 10, (8,))  # ground-truth labels

criterion = FocalCELoss(gamma=2.0)
loss = criterion(inputs, targets)
loss.backward()

BFCELoss

A PyTorch nn.Module implementing the Binary Focal Cross Entropy loss for binary classification tasks.

from focal_ce_loss import BFCELoss

criterion = BFCELoss(alpha=0.25, gamma=2.0, reduction="sum")

# Compute loss from logits and binary targets
loss = criterion(inputs, targets)
loss.backward()

Parameters

  • gamma: focusing parameter; higher values down-weight easy examples more aggressively (default: 2.0)
  • alpha: optional class weight for the positive class (default: 0.25)
  • reduction: specifies the reduction to apply — "mean", "sum", or "none" (default: "mean")
  • ignore_index: target value to ignore during loss computation (default: -100)

Methods

  • forward(input, target) — accepts a torch.Tensor of shape (N,) or (N, 1) and binary targets of shape (N,); returns a scalar loss tensor (or per-sample tensor if reduction="none")

Inputs

  • input: torch.Tensor of shape (N,) or (N, 1) of predicted probabilities with values in [0, 1].
  • target: torch.Tensor of shape (N,) containing binary labels {0, 1}

Example

import torch
from focal_ce_loss import BFCELoss

inputs  = torch.randn(8)           # batch of 8 logits
targets = torch.randint(0, 2, (8,)).float()  # binary ground-truth labels

criterion = BFCELoss(gamma=2.0, alpha=0.25)
loss = criterion(inputs, targets)
loss.backward()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

focal_ce_loss-0.0.3.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

focal_ce_loss-0.0.3-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file focal_ce_loss-0.0.3.tar.gz.

File metadata

  • Download URL: focal_ce_loss-0.0.3.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for focal_ce_loss-0.0.3.tar.gz
Algorithm Hash digest
SHA256 dc21f26dee8753cfef3520a11dcccc933241b72a7f4f238776d0e5249be611a9
MD5 0494df1f4d7f407424cff199d81e0f82
BLAKE2b-256 495cc23752e7c8679baa89c5886c8a29a97d406c2472d555c48ce7c041bdacae

See more details on using hashes here.

Provenance

The following attestation bundles were made for focal_ce_loss-0.0.3.tar.gz:

Publisher: publish-to-pypi.yml on RPetras3/focal_ce_loss_funcs_pytorch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file focal_ce_loss-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: focal_ce_loss-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for focal_ce_loss-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 30966f15a7fc0877af397bb5796dd1060699f676afe1c2f996ea29f9196a0e36
MD5 1a3f96ae9b381c49a0ccc9344d6e0162
BLAKE2b-256 8d201fbd2ea07e9cfe0b7d44b2d948596076e75aeaf3b53c7ca8658aba3f1786

See more details on using hashes here.

Provenance

The following attestation bundles were made for focal_ce_loss-0.0.3-py3-none-any.whl:

Publisher: publish-to-pypi.yml on RPetras3/focal_ce_loss_funcs_pytorch

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page