keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
62.13k stars 19.49k forks source link

Fixing example code for BinaryFocalCrossentropy in losses.py file #20492

Closed tilakrayal closed 1 week ago

tilakrayal commented 1 week ago

The example mentioned for the Keras API binarycrossentropy was failing with the error AttributeError: 'list' object has no attribute 'shape'.

>>> # 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)

It works if we change the input from list to arrays.

>>> # Example 2: (batch_size = 2, number of samples = 4)
>>> y_true = np.array([[0, 1], [0, 0]])
>>> y_pred = np.array([[-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)
codecov-commenter commented 1 week ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 59.98%. Comparing base (96e07ec) to head (a8641cc).

:exclamation: There is a different number of reports uploaded between BASE (96e07ec) and HEAD (a8641cc). Click for more details.

HEAD has 6 uploads less than BASE | Flag | BASE (96e07ec) | HEAD (a8641cc) | |------|------|------| |keras|4|1| |keras-torch|1|0| |keras-tensorflow|1|0| |keras-jax|1|0|
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #20492 +/- ## =========================================== - Coverage 82.08% 59.98% -22.10% =========================================== Files 515 515 Lines 47543 47543 Branches 7455 7455 =========================================== - Hits 39024 28519 -10505 - Misses 6709 17270 +10561 + Partials 1810 1754 -56 ``` | [Flag](https://app.codecov.io/gh/keras-team/keras/pull/20492/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team) | Coverage Δ | | |---|---|---| | [keras](https://app.codecov.io/gh/keras-team/keras/pull/20492/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team) | `59.98% <ø> (-21.95%)` | :arrow_down: | | [keras-jax](https://app.codecov.io/gh/keras-team/keras/pull/20492/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team) | `?` | | | [keras-numpy](https://app.codecov.io/gh/keras-team/keras/pull/20492/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team) | `59.98% <ø> (-0.01%)` | :arrow_down: | | [keras-tensorflow](https://app.codecov.io/gh/keras-team/keras/pull/20492/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team) | `?` | | | [keras-torch](https://app.codecov.io/gh/keras-team/keras/pull/20492/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team) | `?` | | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.