Skip to content

Fix Keras imports. #2829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions tensorflow_addons/optimizers/discriminative_layer_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@
from tensorflow_addons.optimizers import KerasLegacyOptimizer
from typeguard import typechecked

from keras import backend
from keras.utils import tf_utils
try:
# New versions of Keras require importing from `keras.src` when
# importing internal symbols.
from keras.src import backend

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this line? Shouldn't from keras import backend work since its public API?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below, on line 144, backend._current_graph() is called. Since this is an internal symbol, it requires the keras.src import. from keras import backend will only allow public symbols to be accessed on backend, while from keras.src import backend allows all symbols from backend.py to be accessed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the details. Sounds good.

from keras.src.utils import tf_utils
except ImportError:
from keras import backend
from keras.utils import tf_utils


@tf.keras.utils.register_keras_serializable(package="Addons")
Expand Down
7 changes: 6 additions & 1 deletion tensorflow_addons/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
from tensorflow_addons.utils import resource_loader

if Version(tf.__version__) >= Version("2.9"):
from keras.testing_infra.test_utils import layer_test # noqa: F401
try:
# New versions of Keras require importing from `keras.src` when
# importing internal symbols.
from keras.src.testing_infra.test_utils import layer_test # noqa: F401
except ImportError:
from keras.testing_infra.test_utils import layer_test # noqa: F401
else:
from keras.testing_utils import layer_test # noqa: F401

Expand Down
7 changes: 6 additions & 1 deletion tensorflow_addons/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@

# TODO: Remove once https://github.com/tensorflow/tensorflow/issues/44613 is resolved
if tf.__version__[:3] > "2.5":
from keras.engine import keras_tensor
try:
# New versions of Keras require importing from `keras.src` when
# importing internal symbols.
from keras.src.engine import keras_tensor
except ImportError:
from keras.engine import keras_tensor
else:
from tensorflow.python.keras.engine import keras_tensor

Expand Down