Skip to content

Commit 6e688ab

Browse files
authored
[Documentation] Updated Binary Focal Crossentropy Loss Docstring (#21036)
* updated binary focal loss docstring * update to docstring comment * fixed typo
1 parent 492fe28 commit 6e688ab

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

keras/src/losses/losses.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,11 +2363,23 @@ def binary_focal_crossentropy(
23632363
23642364
>>> y_true = [[0, 1], [0, 0]]
23652365
>>> y_pred = [[0.6, 0.4], [0.4, 0.6]]
2366-
>>> loss = keras.losses.binary_focal_crossentropy(
2366+
>>> # In this instance, the first sample in the second batch is the
2367+
>>> # 'easier' example.
2368+
>>> focal_loss = keras.losses.binary_focal_crossentropy(
23672369
... y_true, y_pred, gamma=2)
23682370
>>> assert loss.shape == (2,)
2369-
>>> loss
2371+
>>> focal_loss
23702372
array([0.330, 0.206], dtype=float32)
2373+
>>> # Compare with binary_crossentropy
2374+
>>> bce_loss = keras.losses.binary_focal_crossentropy(
2375+
... y_true, y_pred)
2376+
>>> bce_loss
2377+
array([0.916, 0.714], dtype=float32)
2378+
>>> # Binary focal crossentropy loss attributes more importance to the
2379+
>>> # harder example which results in a higher loss for the first batch
2380+
>>> # when normalized by binary cross entropy loss
2381+
>>> focal_loss/bce_loss
2382+
array([0.360, 0.289]
23712383
"""
23722384
y_pred = ops.convert_to_tensor(y_pred)
23732385
y_true = ops.cast(y_true, y_pred.dtype)

0 commit comments

Comments
 (0)