Skip to content

Commit 958661c

Browse files
committed
fix: actually set KERAS_BACKEND to chosen backend
Add warning if KERAS_BACKEND and actually loaded backend do not match. This can happen if keras is imported before BayesFlow.
1 parent e0ec0e4 commit 958661c

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

bayesflow/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(self, display_name, package_name, env_name, install_url, priority):
4949

5050
found_backends.sort(key=lambda b: b.priority)
5151
chosen_backend = found_backends[0]
52+
os.environ["KERAS_BACKEND"] = chosen_backend.env_name
5253

5354
warnings.warn(
5455
f"Multiple Keras-compatible backends detected ({', '.join(b.display_name for b in found_backends)}).\n"
@@ -71,6 +72,9 @@ def __init__(self, display_name, package_name, env_name, install_url, priority):
7172

7273
from bayesflow.utils import logging
7374

75+
if keras.backend.backend().lower() != os.environ["KERAS_BACKEND"].lower():
76+
logging.warning("Automatic backend selection failed, most likely because Keras was imported before BayesFlow.")
77+
7478
logging.info(f"Using backend {keras.backend.backend()!r}")
7579

7680
if keras.backend.backend() == "torch":

0 commit comments

Comments
 (0)