Keras 3 Multi-Backend: TensorFlow, JAX and PyTorch

Deep Learning with Keras and TensorFlow 9 min min read Updated: Nov 16, 2026 Advanced
Keras 3 Multi-Backend: TensorFlow, JAX and PyTorch
Advanced Topic 7 of 7

One Library, Several Engines

Keras 3 can run on top of TensorFlow, JAX or PyTorch. You write your model once and choose the engine that suits your needs: TensorFlow for deployment tools, JAX for speed, PyTorch for its ecosystem.

Choosing the Backend

The backend must be set before importing Keras, and it cannot be changed after the import in the same Python session.

python import os os.environ["KERAS_BACKEND"] = "jax" # "tensorflow", "jax" or "torch" import keras print(keras.backend.backend()) # jax

You can also set it outside Python, either with an environment variable or in the Keras config file.

bash # Linux / Mac export KERAS_BACKEND=torch # Windows (PowerShell) $env:KERAS_BACKEND = "torch" # Or edit the file ~/.keras/keras.json and change the "backend" value
Install the backend: Keras only uses a backend that is installed, for example pip install jax or pip install torch. For GPU support, follow the installation guide of that framework, and use a separate clean environment for each backend.

Write Portable Code with keras.ops

python import keras from keras import ops x = ops.array([[1.0, 2.0], [3.0, 4.0]]) y = ops.matmul(x, ops.transpose(x)) print(ops.mean(y)) print(ops.softmax(x))

keras.ops mirrors the NumPy API and works on every backend. Use it in custom layers and losses instead of tf. functions to stay portable.

Model Code Stays the Same

python model = keras.Sequential([ keras.Input(shape=(784,)), keras.layers.Dense(128, activation="relu"), keras.layers.Dense(10, activation="softmax"), ]) model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) model.fit(x_train, y_train, epochs=3) # runs on whichever backend is active

Backend Comparison

BackendStrengthsWatch out
TensorFlowMature deployment (TFLite, TF Serving), tf.dataNone for beginners, a good default
JAXVery fast compiled training, TPUsFewer beginner tutorials
PyTorchLarge research ecosystem, easy debuggingUse torch ops in custom training code

Data Pipelines

Keras 3 fit() accepts NumPy arrays, tf.data.Dataset objects, PyTorch DataLoader objects and Python generators, no matter which backend is selected. So you can reuse your data code.

What Is Not Portable?

  • Code that calls tf.* functions directly, such as tf.GradientTape in custom loops
  • Saving with model.export() for the SavedModel format, which requires TensorFlow
  • Backend-specific custom layers

Models saved in the .keras format can be loaded on any backend.

Recommendation: Learn with the TensorFlow backend, because this course uses its deployment tools. Write custom layers with keras.ops so that you can switch later if you want.

Conclusion

Keras 3 gives you freedom from lock-in. Set the backend first, then write portable code with keras.ops.

Quick Summary: Set KERAS_BACKEND before import keras, use keras.ops for portable math and save models as .keras.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators