1
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Input In [20], in <cell line: 1>()
----> 1 from tensorflow.keras import layers
      2 import keras.backend as backend
      3 import tensorflow as tf

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\api\_v2\keras\__init__.py:13, in <module>
     10 import sys as _sys
     12 from keras import __version__
---> 13 from keras.api._v2.keras import __internal__
     14 from keras.api._v2.keras import activations
     15 from keras.api._v2.keras import applications

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\api\__init__.py:8, in <module>
      3 """Public API for tf. namespace.
      4 """
      6 import sys as _sys
----> 8 from keras.api import keras
      9 from tensorflow.python.util import module_wrapper as _module_wrapper
     11 if not isinstance(_sys.modules[__name__], _module_wrapper.TFModuleWrapper):

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\api\keras\__init__.py:16, in <module>
     14 from keras.api.keras import activations
     15 from keras.api.keras import applications
---> 16 from keras.api.keras import backend
     17 from keras.api.keras import callbacks
     18 from keras.api.keras import constraints

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\api\keras\backend\__init__.py:24, in <module>
     22 from keras.backend import binary_crossentropy
     23 from keras.backend import binary_focal_crossentropy
---> 24 from keras.backend import binary_weighted_focal_crossentropy
     25 from keras.backend import cast
     26 from keras.backend import cast_to_floatx

ImportError: cannot import name 'binary_weighted_focal_crossentropy' from 'keras.backend' (D:\Users\DGong\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\backend.py)

I can't figure out why I'm getting an import error here. I've uninstalled and reinstalled tensorflow and keras many times, as well as used both pip and pip3. I checked the file and nothing seems to be different between the way binary_focal_crossentropy and binary_weighted_focal_crossentropy is exported. But the former seems to import without error.

2
  • Can you give the full stack? What are you importing that has binary_focal_crossentropy, I've never seen these functions.
    – M Z
    Jul 12, 2022 at 18:53
  • @MZ Just edited with the full stack!
    – Dana
    Jul 12, 2022 at 18:58

1 Answer 1

0

You can import and use tf.keras.metrics.binary_focal_crossentropy by importing the metrics library below.

Also, Instead of pip installing each package(TensorFlow, Keras) separately, the recommended approach is to install Keras as part of the TensorFlow installation. When you install TensorFlow >2.0, Keras will be automatically installed, as well.

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.metrics import binary_focal_crossentropy

You can check more about binary_focal_crossentropy in the given link.

1
  • your line .... from tensorflow.keras import layers ....... gives me the same error binary_focal_crossentropy cant be imported from keras backend. is this stilll correct? im using new versions
    – tgmjack
    Sep 5, 2022 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.