Skip to content

Importing tf_keras to use Keras 2 in TensorFlow 2.16 fails #756

Open
@MaxiBoether

Description

@MaxiBoether

System information.

  • Have I written custom code (as opposed to using a stock example script provided in Keras): No.
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): macOS 14.2.1, Python 3.12.2 inside a conda environment
  • TensorFlow installed from (source or binary): binary (pip)
  • TensorFlow version (use command below): 2.16
  • Python version: 3.12.2
  • Bazel version (if compiling from source): -
  • GPU model and memory: -
    Describe the problem.

huggingface transformers currently relies on Keras 2. They mention that install the tf-keras package can make Keras 2 APIs available in TF 2.16. The getting started page mentions something similar. In a clean environment, I install the following packages:

pip install tensorflow tf_keras

Importing Keras 3.0 works as expected:

python -c "import tensorflow.keras; print(tensorflow.keras.version())"  
3.0.5

However, importing tf_keras fails with some error:

 python -c "import tf_keras"          
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/mboether/mambaforge/envs/compile2/lib/python3.12/site-packages/tf_keras/__init__.py", line 3, in <module>
    from tf_keras import __internal__
  File "/Users/mboether/mambaforge/envs/compile2/lib/python3.12/site-packages/tf_keras/__internal__/__init__.py", line 3, in <module>
    from tf_keras.__internal__ import backend
  File "/Users/mboether/mambaforge/envs/compile2/lib/python3.12/site-packages/tf_keras/__internal__/backend/__init__.py", line 3, in <module>
    from tf_keras.src.backend import _initialize_variables as initialize_variables
  File "/Users/mboether/mambaforge/envs/compile2/lib/python3.12/site-packages/tf_keras/src/__init__.py", line 21, in <module>
    from tf_keras.src import applications
  File "/Users/mboether/mambaforge/envs/compile2/lib/python3.12/site-packages/tf_keras/src/applications/__init__.py", line 18, in <module>
    from tf_keras.src.applications.convnext import ConvNeXtBase
  File "/Users/mboether/mambaforge/envs/compile2/lib/python3.12/site-packages/tf_keras/src/applications/convnext.py", line 33, in <module>
    from tf_keras.src.engine import sequential
  File "/Users/mboether/mambaforge/envs/compile2/lib/python3.12/site-packages/tf_keras/src/engine/sequential.py", line 24, in <module>
    from tf_keras.src.engine import functional
  File "/Users/mboether/mambaforge/envs/compile2/lib/python3.12/site-packages/tf_keras/src/engine/functional.py", line 33, in <module>
    from tf_keras.src.engine import training as training_lib
  File "/Users/mboether/mambaforge/envs/compile2/lib/python3.12/site-packages/tf_keras/src/engine/training.py", line 48, in <module>
    from tf_keras.src.saving import saving_api
  File "/Users/mboether/mambaforge/envs/compile2/lib/python3.12/site-packages/tf_keras/src/saving/saving_api.py", line 25, in <module>
    from tf_keras.src.saving.legacy import save as legacy_sm_saving_lib
  File "/Users/mboether/mambaforge/envs/compile2/lib/python3.12/site-packages/tf_keras/src/saving/legacy/save.py", line 27, in <module>
    from tf_keras.src.saving.legacy.saved_model import load_context
  File "/Users/mboether/mambaforge/envs/compile2/lib/python3.12/site-packages/tf_keras/src/saving/legacy/saved_model/load_context.py", line 68, in <module>
    tf.__internal__.register_load_context_function(in_load_context)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'. Did you mean: 'register_call_context_function'?

I can trigger the same error by forcing Tensorflow to use Keras 2 instead using TF_USE_LEGACY_KERAS=1 and importing tensorflow.keras. I do not think this is supposed to happen. From my understanding, installing tf_keras should enable us to use keras 2 in tf 2.16.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions