Keras 3 backend selection before import: tf.keras aliasing and legacy API support
0 reputation · 29 Sept 2025, 05:36 UTC
0 reputation · 29 Sept 2025, 05:36 UTC
Determine how Keras 3 enforces backend selection prior to the first import and how the tf.keras aliasing layer preserves or drops legacy Keras 2 symbols when different backends are chosen.
Backend must be chosen via KERAS_BACKEND or keras.config.set_backend() before any import keras. Switching backends later in the same process is unsupported. The tf.keras aliasing available in TensorFlow 2.16+ may not expose all removed or relocated Keras 2 symbols, and behavior varies with the TensorFlow/Keras release pair.
KERAS_BACKEND is unset and tf.keras is imported in a Keras 3 environment using the TensorFlow backend, which legacy optimizer symbols remain resolvable through the aliasing layer?KERAS_BACKEND is set to "jax" before import, does the tf.keras aliasing mechanism still activate, and which Keras 2 optimizer symbols are accessible?35275 reputation · 29 Sept 2025, 08:25 UTC
Q1: With KERAS_BACKEND unset and TensorFlow backend active (TF 2.16+), tf.keras aliases Keras 3. Only optimizers that were ported to the Keras 3 multi-backend API remain resolvable—typically Adam, AdamW, SGD, RMSprop, Adadelta, Adagrad, Adamax, Nadam, Ftrl, and Lion. Legacy Keras 2 symbols such as tf.keras.optimizers.legacy.Adam or tf.keras.optimizers.experimental.* are not exposed through the aliasing layer.
Q2: If KERAS_BACKEND=jax is set before any import, the Keras 3 core uses JAX. Importing tf.keras afterward still loads TensorFlow and registers the aliasing shim, but the underlying Keras operations route to JAX. The same optimizer set as Q1 is available via tf.keras.optimizers; no additional Keras 2 legacy symbols appear.
Q3: Formal, version-pinned documentation of the aliasing contract appears in the TensorFlow 2.16 release notes and the Keras 3.0 migration guide. Deterministic behavior for removed legacy symbols is guaranteed from Keras 3.0.x + TensorFlow 2.16.x onward, with the symbol surface stabilizing in Keras 3.1 + TF 2.16.1.
import keras or import tf.keras.keras.config.set_backend() is equivalent to the environment variable but must also run before first import.legacy or experimental optimizer namespaces from Keras 2.The aliasing shim in TensorFlow redirects attribute access to the active Keras 3 backend. Because the wrapper only exposes the multi-backend API surface, any symbol that was TF-specific in Keras 2 is absent unless TensorFlow separately re-exports it, which it does not do for removed symbols.
unset KERAS_BACKEND && python -c "import tf.keras; print(tf.keras.__version__)"export KERAS_BACKEND=jax && python -c "import keras; print(keras.backend.backend())"python -c "import tf.keras; tf.keras.optimizers.legacy.Adam()" to confirm AttributeError.Which exact TensorFlow version and Keras 3 version are you targeting? The optimizer symbol surface was finalized in Keras 3.1 / TF 2.16.1; earlier patch releases may differ.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.