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 = FocalCrossEntropyLoss()
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 tensor of shape (C,) 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 = FocalCrossEntropyLoss(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 scalar 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.2.tar.gz (15.3 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.2-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: focal_ce_loss-0.0.2.tar.gz
  • Upload date:
  • Size: 15.3 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.2.tar.gz
Algorithm Hash digest
SHA256 7f883f2cc5e1e743afdce8db8a05b54864837d36d491e7e996806e95a81869cf
MD5 9decec8f84f4fc2b05c91a2f225d0e33
BLAKE2b-256 97350f563879b98c29403d2ade2ea879846de42125dbaba532bbe7baa9642faa

See more details on using hashes here.

Provenance

The following attestation bundles were made for focal_ce_loss-0.0.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: focal_ce_loss-0.0.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7b5d556dd5407bf2afe6d1092be693daa479399f699ee4a9ce286ac459943e79
MD5 9b13e108af8823d99ddb5f72e09baf2c
BLAKE2b-256 7edea997d9c4118c5e84fdd73d3266f4a9b1b8035a181cf368610069c80e5927

See more details on using hashes here.

Provenance

The following attestation bundles were made for focal_ce_loss-0.0.2-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