tf.keras.losses.BinaryFocalCrossentropy
Stay organized with collections
Save and categorize content based on your preferences.
Computes focal cross-entropy loss between true labels and predictions.
Inherits From: Loss
tf.keras.losses.BinaryFocalCrossentropy(
apply_class_balancing=False,
alpha=0.25,
gamma=2.0,
from_logits=False,
label_smoothing=0.0,
axis=-1,
reduction='sum_over_batch_size',
name='binary_focal_crossentropy'
)
Binary cross-entropy loss is often used for binary (0 or 1) classification
tasks. The loss function requires the following inputs:
y_true
(true label): This is either 0 or 1.
y_pred
(predicted value): This is the model's prediction, i.e, a single
floating-point value which either represents a
logit, (i.e, value in [-inf, inf]
when from_logits=True
) or a probability (i.e, value in [0., 1.]
when
from_logits=False
).
According to Lin et al., 2018, it
helps to apply a "focal factor" to down-weight easy examples and focus more
on hard examples. By default, the focal tensor is computed as follows:
focal_factor = (1 - output) ** gamma
for class 1
focal_factor = output ** gamma
for class 0
where gamma
is a focusing parameter. When gamma=0
, this function is
equivalent to the binary crossentropy loss.
Args |
apply_class_balancing
|
A bool, whether to apply weight balancing on the
binary classes 0 and 1.
|
alpha
|
A weight balancing factor for class 1, default is 0.25 as
mentioned in reference Lin et al., 2018. The weight for class 0 is
1.0 - alpha .
|
gamma
|
A focusing parameter used to compute the focal factor, default is
2.0 as mentioned in the reference
Lin et al., 2018.
|
from_logits
|
Whether to interpret y_pred as a tensor of
logit values. By default, we
assume that y_pred are probabilities (i.e., values in [0, 1] ).
|
label_smoothing
|
Float in [0, 1] . When 0 , no smoothing occurs.
When > 0 , we compute the loss between the predicted labels
and a smoothed version of the true labels, where the smoothing
squeezes the labels towards 0.5 .
Larger values of label_smoothing correspond to heavier smoothing.
|
axis
|
The axis along which to compute crossentropy (the features axis).
Defaults to -1 .
|
reduction
|
Type of reduction to apply to the loss. In almost all cases
this should be "sum_over_batch_size" .
Supported options are "sum" , "sum_over_batch_size" or None .
|
name
|
Optional name for the loss instance.
|
Examples:
With the compile()
API:
model.compile(
loss=keras.losses.BinaryFocalCrossentropy(
gamma=2.0, from_logits=True),
...
)
As a standalone function:
# Example 1: (batch_size = 1, number of samples = 4)
y_true = [0, 1, 0, 0]
y_pred = [-18.6, 0.51, 2.94, -12.8]
loss = keras.losses.BinaryFocalCrossentropy(
gamma=2, from_logits=True)
loss(y_true, y_pred)
0.691
# Apply class weight
loss = keras.losses.BinaryFocalCrossentropy(
apply_class_balancing=True, gamma=2, from_logits=True)
loss(y_true, y_pred)
0.51
# Example 2: (batch_size = 2, number of samples = 4)
y_true = [[0, 1], [0, 0]]
y_pred = [[-18.6, 0.51], [2.94, -12.8]]
# Using default 'auto'/'sum_over_batch_size' reduction type.
loss = keras.losses.BinaryFocalCrossentropy(
gamma=3, from_logits=True)
loss(y_true, y_pred)
0.647
# Apply class weight
loss = keras.losses.BinaryFocalCrossentropy(
apply_class_balancing=True, gamma=3, from_logits=True)
loss(y_true, y_pred)
0.482
# Using 'sample_weight' attribute with focal effect
loss = keras.losses.BinaryFocalCrossentropy(
gamma=3, from_logits=True)
loss(y_true, y_pred, sample_weight=[0.8, 0.2])
0.133
# Apply class weight
loss = keras.losses.BinaryFocalCrossentropy(
apply_class_balancing=True, gamma=3, from_logits=True)
loss(y_true, y_pred, sample_weight=[0.8, 0.2])
0.097
# Using 'sum' reduction` type.
loss = keras.losses.BinaryFocalCrossentropy(
gamma=4, from_logits=True,
reduction="sum")
loss(y_true, y_pred)
1.222
# Apply class weight
loss = keras.losses.BinaryFocalCrossentropy(
apply_class_balancing=True, gamma=4, from_logits=True,
reduction="sum")
loss(y_true, y_pred)
0.914
# Using 'none' reduction type.
loss = keras.losses.BinaryFocalCrossentropy(
gamma=5, from_logits=True,
reduction=None)
loss(y_true, y_pred)
array([0.0017 1.1561], dtype=float32)
# Apply class weight
loss = keras.losses.BinaryFocalCrossentropy(
apply_class_balancing=True, gamma=5, from_logits=True,
reduction=None)
loss(y_true, y_pred)
array([0.0004 0.8670], dtype=float32)
Methods
call
View source
call(
y_true, y_pred
)
from_config
View source
@classmethod
from_config(
config
)
get_config
View source
get_config()
__call__
View source
__call__(
y_true, y_pred, sample_weight=None
)
Call self as a function.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-06-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-06-07 UTC."],[],[],null,["# tf.keras.losses.BinaryFocalCrossentropy\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/losses/losses.py#L499-L670) |\n\nComputes focal cross-entropy loss between true labels and predictions.\n\nInherits From: [`Loss`](../../../tf/keras/Loss) \n\n tf.keras.losses.BinaryFocalCrossentropy(\n apply_class_balancing=False,\n alpha=0.25,\n gamma=2.0,\n from_logits=False,\n label_smoothing=0.0,\n axis=-1,\n reduction='sum_over_batch_size',\n name='binary_focal_crossentropy'\n )\n\nBinary cross-entropy loss is often used for binary (0 or 1) classification\ntasks. The loss function requires the following inputs:\n\n- `y_true` (true label): This is either 0 or 1.\n- `y_pred` (predicted value): This is the model's prediction, i.e, a single floating-point value which either represents a [logit](https://en.wikipedia.org/wiki/Logit), (i.e, value in \\[-inf, inf\\] when `from_logits=True`) or a probability (i.e, value in `[0., 1.]` when `from_logits=False`).\n\nAccording to [Lin et al., 2018](https://arxiv.org/pdf/1708.02002.pdf), it\nhelps to apply a \"focal factor\" to down-weight easy examples and focus more\non hard examples. By default, the focal tensor is computed as follows:\n\n`focal_factor = (1 - output) ** gamma` for class 1\n`focal_factor = output ** gamma` for class 0\nwhere `gamma` is a focusing parameter. When `gamma=0`, this function is\nequivalent to the binary crossentropy loss.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `apply_class_balancing` | A bool, whether to apply weight balancing on the binary classes 0 and 1. |\n| `alpha` | A weight balancing factor for class 1, default is `0.25` as mentioned in reference [Lin et al., 2018](https://arxiv.org/pdf/1708.02002.pdf). The weight for class 0 is `1.0 - alpha`. |\n| `gamma` | A focusing parameter used to compute the focal factor, default is `2.0` as mentioned in the reference [Lin et al., 2018](https://arxiv.org/pdf/1708.02002.pdf). |\n| `from_logits` | Whether to interpret `y_pred` as a tensor of [logit](https://en.wikipedia.org/wiki/Logit) values. By default, we assume that `y_pred` are probabilities (i.e., values in `[0, 1]`). |\n| `label_smoothing` | Float in `[0, 1]`. When `0`, no smoothing occurs. When \\\u003e `0`, we compute the loss between the predicted labels and a smoothed version of the true labels, where the smoothing squeezes the labels towards `0.5`. Larger values of `label_smoothing` correspond to heavier smoothing. |\n| `axis` | The axis along which to compute crossentropy (the features axis). Defaults to `-1`. |\n| `reduction` | Type of reduction to apply to the loss. In almost all cases this should be `\"sum_over_batch_size\"`. Supported options are `\"sum\"`, `\"sum_over_batch_size\"` or `None`. |\n| `name` | Optional name for the loss instance. |\n\n\u003cbr /\u003e\n\n#### Examples:\n\nWith the `compile()` API: \n\n model.compile(\n loss=keras.losses.BinaryFocalCrossentropy(\n gamma=2.0, from_logits=True),\n ...\n )\n\nAs a standalone function: \n\n # Example 1: (batch_size = 1, number of samples = 4)\n y_true = [0, 1, 0, 0]\n y_pred = [-18.6, 0.51, 2.94, -12.8]\n loss = keras.losses.BinaryFocalCrossentropy(\n gamma=2, from_logits=True)\n loss(y_true, y_pred)\n 0.691\n\n # Apply class weight\n loss = keras.losses.BinaryFocalCrossentropy(\n apply_class_balancing=True, gamma=2, from_logits=True)\n loss(y_true, y_pred)\n 0.51\n\n # Example 2: (batch_size = 2, number of samples = 4)\n y_true = [[0, 1], [0, 0]]\n y_pred = [[-18.6, 0.51], [2.94, -12.8]]\n # Using default 'auto'/'sum_over_batch_size' reduction type.\n loss = keras.losses.BinaryFocalCrossentropy(\n gamma=3, from_logits=True)\n loss(y_true, y_pred)\n 0.647\n\n # Apply class weight\n loss = keras.losses.BinaryFocalCrossentropy(\n apply_class_balancing=True, gamma=3, from_logits=True)\n loss(y_true, y_pred)\n 0.482\n\n # Using 'sample_weight' attribute with focal effect\n loss = keras.losses.BinaryFocalCrossentropy(\n gamma=3, from_logits=True)\n loss(y_true, y_pred, sample_weight=[0.8, 0.2])\n 0.133\n\n # Apply class weight\n loss = keras.losses.BinaryFocalCrossentropy(\n apply_class_balancing=True, gamma=3, from_logits=True)\n loss(y_true, y_pred, sample_weight=[0.8, 0.2])\n 0.097\n\n # Using 'sum' reduction` type.\n loss = keras.losses.BinaryFocalCrossentropy(\n gamma=4, from_logits=True,\n reduction=\"sum\")\n loss(y_true, y_pred)\n 1.222\n\n # Apply class weight\n loss = keras.losses.BinaryFocalCrossentropy(\n apply_class_balancing=True, gamma=4, from_logits=True,\n reduction=\"sum\")\n loss(y_true, y_pred)\n 0.914\n\n # Using 'none' reduction type.\n loss = keras.losses.BinaryFocalCrossentropy(\n gamma=5, from_logits=True,\n reduction=None)\n loss(y_true, y_pred)\n array([0.0017 1.1561], dtype=float32)\n\n # Apply class weight\n loss = keras.losses.BinaryFocalCrossentropy(\n apply_class_balancing=True, gamma=5, from_logits=True,\n reduction=None)\n loss(y_true, y_pred)\n array([0.0004 0.8670], dtype=float32)\n\nMethods\n-------\n\n### `call`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/losses/losses.py#L20-L22) \n\n call(\n y_true, y_pred\n )\n\n### `from_config`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/losses/losses.py#L30-L34) \n\n @classmethod\n from_config(\n config\n )\n\n### `get_config`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/losses/losses.py#L660-L670) \n\n get_config()\n\n### `__call__`\n\n[View source](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/losses/loss.py#L32-L61) \n\n __call__(\n y_true, y_pred, sample_weight=None\n )\n\nCall self as a function."]]