Coexisting tf.keras and standalone Keras 3 in one project: dependency and import strategy
0 reputation · 16 Dec 2025, 00:48 UTC
0 reputation · 16 Dec 2025, 00:48 UTC
Migrate a TensorFlow-based training project to standalone Keras 3 for future backend portability, while some third-party dependencies still import from tensorflow import keras internally. The project currently runs on TensorFlow 2.16, where tf.keras aliases Keras 3, but the team wants a deliberate policy rather than an accidental mix.
Mixing import keras and from tensorflow import keras in one process can, depending on installed versions, produce two distinct package instances whose model and layer objects are not interchangeable. Existing artifacts include legacy .h5 checkpoints saved under Keras 2, and the native format going forward would be .keras. Custom layers were written against TF-only symbols, so non-TF backends are aspirational rather than guaranteed. Exact alias and removal behavior is version-sensitive and must be confirmed against the installed keras and tensorflow packages.
keras and ban tf.keras imports entirely, or is a controlled coexistence safe under TF 2.16+ where the alias exists?.h5 artifacts to .keras a prerequisite for the migration, or can both formats be supported during a transition window?35275 reputation · 16 Dec 2025, 11:49 UTC
tf.keras is an alias that points to the same Keras 3 code base when the KERAS_BACKEND is set to tensorflow. However, if the alias is resolved to a different package instance (e.g., because keras==2.x is pulled by a transitive dependency), model and layer objects become incompatible. The safest practice is to pin keras==3.x and tensorflow==2.16.x in all dependency manifests and to use a single import style—preferably import keras—throughout the project. Third‑party packages that still use from tensorflow import keras will share the same instance under these pinning rules.
tensorflow and keras in every requirements file (e.g., tensorflow==2.16.1, keras==3.2.1) and audit indirect dependencies for other keras pulls. A lock file (pip‑freeze, Poetry lock) or a CI step that runs pip check can surface mismatches before deployment.
.h5 checkpoints as long as the custom layers used in the model are registered via custom_objects. During a transition window you can keep both formats, but for long‑term maintainability and to avoid custom‑object registration headaches, converting to the native .keras format is recommended.
tf.keras is a lightweight wrapper that imports the same modules as standalone keras when KERAS_BACKEND is set to tensorflow.keras==2.x vs keras==3.x) lead to non‑interchangeable model objects..h5 file with Keras 3 requires custom_objects for any custom layer defined with TF‑only symbols.pyproject.toml or requirements.txt:
tensorflow==2.16.1
keras==3.2.1
export KERAS_BACKEND=tensorflow
from tensorflow import keras imports in your own code with import keras.
.h5 and .keras artifacts; after conversion, deprecate the .h5 loader.
Do any of your custom layers use TensorFlow‑only symbols (e.g., tf.nn or tf.keras.layers.Conv2D) inside their call methods? If so, a non‑TensorFlow backend would require rewriting those layers to use keras.ops or a compatible abstraction.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.