@@ -2363,11 +2363,23 @@ def binary_focal_crossentropy(
2363
2363
2364
2364
>>> y_true = [[0, 1], [0, 0]]
2365
2365
>>> 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(
2367
2369
... y_true, y_pred, gamma=2)
2368
2370
>>> assert loss.shape == (2,)
2369
- >>> loss
2371
+ >>> focal_loss
2370
2372
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]
2371
2383
"""
2372
2384
y_pred = ops .convert_to_tensor (y_pred )
2373
2385
y_true = ops .cast (y_true , y_pred .dtype )
0 commit comments