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 ifreduction="none")
Inputs
input:torch.Tensorof shape(N, C)target:torch.Tensorof 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 ifreduction="none")
Inputs
input:torch.Tensorof shape(N,)or(N, 1)of predicted probabilities with values in [0, 1].target:torch.Tensorof 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f883f2cc5e1e743afdce8db8a05b54864837d36d491e7e996806e95a81869cf
|
|
| MD5 |
9decec8f84f4fc2b05c91a2f225d0e33
|
|
| BLAKE2b-256 |
97350f563879b98c29403d2ade2ea879846de42125dbaba532bbe7baa9642faa
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
focal_ce_loss-0.0.2.tar.gz -
Subject digest:
7f883f2cc5e1e743afdce8db8a05b54864837d36d491e7e996806e95a81869cf - Sigstore transparency entry: 1001364370
- Sigstore integration time:
-
Permalink:
RPetras3/focal_ce_loss_funcs_pytorch@2abcc86f3fa520575ae0067831aaef1edb1b38d7 -
Branch / Tag:
refs/tags/0.0.2 - Owner: https://github.com/RPetras3
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@2abcc86f3fa520575ae0067831aaef1edb1b38d7 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b5d556dd5407bf2afe6d1092be693daa479399f699ee4a9ce286ac459943e79
|
|
| MD5 |
9b13e108af8823d99ddb5f72e09baf2c
|
|
| BLAKE2b-256 |
7edea997d9c4118c5e84fdd73d3266f4a9b1b8035a181cf368610069c80e5927
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
focal_ce_loss-0.0.2-py3-none-any.whl -
Subject digest:
7b5d556dd5407bf2afe6d1092be693daa479399f699ee4a9ce286ac459943e79 - Sigstore transparency entry: 1001364381
- Sigstore integration time:
-
Permalink:
RPetras3/focal_ce_loss_funcs_pytorch@2abcc86f3fa520575ae0067831aaef1edb1b38d7 -
Branch / Tag:
refs/tags/0.0.2 - Owner: https://github.com/RPetras3
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@2abcc86f3fa520575ae0067831aaef1edb1b38d7 -
Trigger Event:
release
-
Statement type: