From 014cbd8c03e0948bce16e105e49b633b45bd6a8f Mon Sep 17 00:00:00 2001 From: "Dr. Juan Miguel Cejuela" <102431+juanmirocks@users.noreply.github.com> Date: Wed, 27 Aug 2025 17:50:44 +0200 Subject: [PATCH] Import tensorflow.keras (tf_keras) when TF_USE_LEGACY_KERAS --- keras_tuner/backend/config.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/keras_tuner/backend/config.py b/keras_tuner/backend/config.py index 439a2e041..030c9e3d9 100644 --- a/keras_tuner/backend/config.py +++ b/keras_tuner/backend/config.py @@ -12,15 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -import keras +import os -def _multi_backend(): +if os.environ.get("TF_USE_LEGACY_KERAS", None) in ("true", "True", "1"): + from tensorflow import keras + _MULTI_BACKEND = False +else: + import keras version_fn = getattr(keras, "version", None) - return version_fn and version_fn().startswith("3.") - - -_MULTI_BACKEND = _multi_backend() + _MULTI_BACKEND = version_fn and version_fn().startswith("3.") def multi_backend():