Error while importing tf_keras
Closed this issue · 11 comments
System information.
- TensorFlow installed from (source or binary): https://pypi.org/project/tf-nightly/
- TensorFlow version (use command below): 2.16.0-dev20231101
- Python version: 3.10.13
- Bazel version (if compiling from source): 6.4.0
- Exact command to reproduce:
import tf_keras
Describe the problem.
Installing tf-keras
using pip install tf_keras
results into the following error:
AttributeError: module 'tensorflow._api.v2.compat.v2.__internal__' has no attribute 'register_load_context_function'. Did you mean: 'register_call_context_function'?
Standalone code to reproduce the issue.
import tf_keras
However, I tried building tf-keras
from source using the following commit - d31ff61 and I was able to import tf_keras
successfully.
Hi,
Thanks for reporting the issue.
Since the migration, there were many changes made, the error you are getting is due to one such change here cff6ac9.
To get rid of the error, you need to install tf-keras-nightly
as well as tf-nightly
.
Note that, tf-keras-nightly
is legacy Keras
code, to use the Keras 3
with multi-backend support, use keras-nightly
and import Keras
directly.
@sachinprasadhs thanks for your response!
Installing tf-keras-nightly
seems to work fine. However, I still see the issue when I pip install tf-keras
. Does that mean that the fix is yet to be made available in the tf-keras pypi package (https://pypi.org/project/tf-keras/)? (I'm trying to use the legacy Keras
code only).
Yes, the fix will be available once both Tensorflow
and tf-keras
does the next stable release, until then continue using as suggested. Thanks!
This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.
@sachinprasadhs After installing tf-keras-nightly
as recommended by you, I'm seeing the following error when I try - from tf_keras.engine import keras_tensor
:
ModuleNotFoundError: No module named 'tf_keras.engine'
Can you please help with this?
Can you try
from tf_keras.__internal__ import KerasTensor
@sachinprasadhs that seems to work, but I encountered another issue further down the line when I try to import tf_utils
as from tf_keras.utils import tf_utils
- ImportError: cannot import name 'tf_utils' from 'tf_keras.utils'
I see that modules engine
and utils
are still a part of tf_keras
. Do you know what is the root cause for this issue?
@sachinprasadhs friendly ping :)
from tf_keras import utils
should work, you can't import tf_utils, what are you trying to do exactly?
@sachinprasadhs I'm trying to run an object detection model that depends on tensorflow-addons
.
- In this line in addons, I replaced
from keras.src.engine import keras_tensor
withfrom tf_keras.engine import keras_tensor
to make it work with Keras 2, but I ran into `ModuleNotFoundError: No module named 'tf_keras.engine'. - Similarly, in this line I replaced
from keras.src.utils import tf_utils
withfrom tf_keras.utils import tf_utils
, but it results intoImportError: cannot import name 'tf_utils' from 'tf_keras.utils'
.
For tf_keras
, you will also need the tf_keras.src
prefix in the import path to access private APIs.
For example, from tf_keras.src.utils import tf_utils
.
Feel free to reopen if it does not resolve your issue.