Skip to main content

TensorFlow implementation of focal loss.

Project description

Python Version PyPI Package Version Last Commit Build Status Code Coverage Documentation Status License

TensorFlow implementation of focal loss [1]: a loss function generalizing binary and multiclass cross-entropy loss that penalizes hard-to-classify examples.

The focal_loss package provides functions and classes that can be used as off-the-shelf replacements for tf.keras.losses functions and classes, respectively.

# Typical tf.keras API usage
import tensorflow as tf
from focal_loss import BinaryFocalLoss

model = tf.keras.Model(...)
model.compile(
    optimizer=...,
    loss=BinaryFocalLoss(gamma=2),  # Used here like a tf.keras loss
    metrics=...,
)
history = model.fit(...)

The focal_loss package includes the functions

  • binary_focal_loss

  • sparse_categorical_focal_loss

and wrapper classes

  • BinaryFocalLoss (use like tf.keras.losses.BinaryCrossentropy)

  • SparseCategoricalFocalLoss (use like tf.keras.losses.SparseCategoricalCrossentropy)

Documentation is available at Read the Docs.

Focal loss plot

Installation

The focal_loss package can be installed using the pip utility. For the latest version, install directly from the package’s GitHub page:

pip install git+https://github.com/artemmavrin/focal-loss.git

Alternatively, install a recent release from the Python Package Index (PyPI):

pip install focal-loss

Note. To install the project for development (e.g., to make changes to the source code), clone the project repository from GitHub and run make dev:

git clone https://github.com/artemmavrin/focal-loss.git
cd focal-loss
# Optional but recommended: create and activate a new environment first
make dev

This will additionally install the requirements needed to run tests, check code coverage, and produce documentation.

References

Download files

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

Source Distribution

focal-loss-0.0.6.tar.gz (14.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_loss-0.0.6-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file focal-loss-0.0.6.tar.gz.

File metadata

  • Download URL: focal-loss-0.0.6.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for focal-loss-0.0.6.tar.gz
Algorithm Hash digest
SHA256 23d24860b132f73ce16713424b9591813e07b84ff308fcbafa204562c40bf0e1
MD5 f9f85c34a8efa4cb61d012f6df742be3
BLAKE2b-256 bbe5ac2897fb267b6c979c91dfdd140569be88f6c2d5c5d96e8123bd7acb525f

See more details on using hashes here.

File details

Details for the file focal_loss-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: focal_loss-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for focal_loss-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 c3e49906800c0b9eb46e29e30d43331eed26246d43cf34bffc1a3454ddc9b0ba
MD5 d3420505ca83f208b05934ebea75741b
BLAKE2b-256 4a96babb0f40b2046a45aa2263773d915a34f02d4fb6bae91a505ce2db8ab0b2

See more details on using hashes here.

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