Complete Keras 3 Tutorial: Multi-Backend Deep Learning Framework for TensorFlow, JAX, and PyTorch
Keras 3 is a major evolution of the world's most popular deep learning framework. With its new multi-backend architecture, Keras 3 allows you to write code once and run it on top of TensorFlow, JAX, or PyTorch without any changes. This tutorial will guide you from installation to advanced usage of Keras 3 for building, training, and deploying deep learning models.
Why Keras 3?
Before Keras 3, developers had to choose a single framework and commit to its ecosystem. Keras 3 changes this paradigm by offering several key advantages:
- Multi-Backend: Write your model once, run it on TensorFlow, JAX, or PyTorch
- Consistent API: One unified API across all backends, reducing the learning curve
- Optimal Performance: Leverage backend-specific optimizations automatically
- Interoperability: Use components from any backend ecosystem
- Backward Compatible: Most Keras 2 code still works in Keras 3
Keras 3 is ideal for teams working across frameworks, researchers who want to compare performance between backends, and engineers who want to write portable code.
Installation
System Requirements
- Python 3.9 or later
- pip (Python package manager)
- GPU with CUDA support (optional, for training acceleration)
Installing Keras 3 with Different Backends
# Install Keras 3 core
pip install keras
Install with TensorFlow backend
pip install keras tensorflow
Install with JAX backend (CPU)
pip install keras jax jaxlib
Install with JAX backend (GPU CUDA 12)
pip install keras "jax[cuda12]"
Install with PyTorch backend
pip install keras torch torchvision
Configuring the Backend
You can set the default backend through environment variables or a configuration file.
Method 1: Environment Variable# Use JAX as backend
export KERASBACKEND=jax
Use PyTorch as backend
export KERASBACKEND=torch
Use TensorFlow as backend (default)
export KERASBACKEND=tensorflow
Method 2: Configuration File
Create or edit ~/.keras/keras.json:
{
"backend": "jax",
"floatx": "float32",
"epsilon": 1e-07,
"imagedataformat": "channelslast"
}
Method 3: Programmatic (at the start of your script)
import os
os.environ["KERASBACKEND"] = "jax"
import keras
print(f"Active backend: {keras.backend.backend()}")
Verifying the Installation
import keras
print(f"Keras version: {keras.version}")
print(f"Backend: {keras.backend.backend()}")
Simple test
import keras.ops as ops
x = ops.ones((2, 3))
print(f"Tensor shape: {x.shape}")
print(f"Tensor:\n{x}")
Basic Usage
Building a Model with the Sequential API
The Sequential API is the simplest way to build a neural network model layer by layer.
import keras
from keras import layers
model = keras.Sequential([
layers.Input(shape=(784,)),
layers.Dense(256, activation="relu"),
layers.Dropout(0.3),
layers.Dense(128, activation="relu"),
layers.Dropout(0.2),
layers.Dense(10, activation="softmax")
])
model.summary()
Building a Model with the Functional API
The Functional API provides more flexibility for complex model architectures, including models with multiple inputs/outputs and skip connections.
import keras
from keras import layers
Define input
inputs = keras.Input(shape=(784,))
Hidden layers
x = layers.Dense(256, activation="relu")(inputs)
x = layers.BatchNormalization()(x)
x = layers.Dropout(0.3)(x)
x = layers.Dense(128, activation="relu")(x)
x = layers.BatchNormalization()(x)
x = layers.Dropout(0.2)(x)
Output layer
outputs = layers.Dense(10, activation="softmax")(x)
Create model
model = keras.Model(inputs=inputs, outputs=outputs, name="mnistclassifier")
model.summary()
Compiling and Training a Model
import keras
from keras import layers
import numpy as np
Create a simple model
model = keras.Sequential([
layers.Input(shape=(784,)),
layers.Dense(128, activation="relu"),
layers.Dense(10, activation="softmax")
])
Compile the model
model.compile(
optimizer=keras.optimizers.Adam(learningrate=0.001),
loss="categoricalcrossentropy",
metrics=["accuracy"]
)
Create dummy data for demonstration
xtrain = np.random.random((1000, 784)).astype("float32")
ytrain = keras.utils.tocategorical(
np.random.randint(10, size=(1000,)), numclasses=10
)
xval = np.random.random((200, 784)).astype("float32")
yval = keras.utils.tocategorical(
np.random.randint(10, size=(200,)), numclasses=10
)
Training
history = model.fit(
xtrain, ytrain,
batchsize=32,
epochs=10,
validationdata=(xval, yval),
verbose=1
)
Image Classification with MNIST
A complete example of MNIST digit classification using a Convolutional Neural Network:
import keras
from keras import layers
import numpy as np
Load MNIST dataset
(xtrain, ytrain), (xtest, ytest) = keras.datasets.mnist.loaddata()
Preprocessing
xtrain = xtrain.astype("float32") / 255.0
xtest = xtest.astype("float32") / 255.0
Add channel dimension
xtrain = np.expanddims(xtrain, -1)
xtest = np.expanddims(xtest, -1)
One-hot encode labels
ytrain = keras.utils.tocategorical(ytrain, 10)
ytest = keras.utils.tocategorical(ytest, 10)
print(f"Training data shape: {xtrain.shape}")
print(f"Test data shape: {xtest.shape}")
Build CNN model
model = keras.Sequential([
layers.Input(shape=(28, 28, 1)),
layers.Conv2D(32, kernelsize=(3, 3), activation="relu"),
layers.MaxPooling2D(poolsize=(2, 2)),
layers.Conv2D(64, kernelsize=(3, 3), activation="relu"),
layers.MaxPooling2D(poolsize=(2, 2)),
layers.Flatten(),
layers.Dropout(0.5),
layers.Dense(10, activation="softmax")
])
model.compile(
optimizer="adam",
loss="categoricalcrossentropy",
metrics=["accuracy"]
)
Training
history = model.fit(
xtrain, ytrain,
batchsize=128,
epochs=5,
validationsplit=0.1
)
Evaluation
testloss, testacc = model.evaluate(xtest, ytest, verbose=0)
print(f"\nTest accuracy: {testacc:.4f}")
Advanced Usage
Custom Training Loop with keras.ops
Keras 3 introduces keras.ops, a numerical operations module that works across all backends. This allows you to write portable custom training loops.
import keras
from keras import layers, ops
model = keras.Sequential([
layers.Input(shape=(784,)),
layers.Dense(128, activation="relu"),
layers.Dense(10, activation="softmax")
])
optimizer = keras.optimizers.Adam(learningrate=0.001)
lossfn = keras.losses.CategoricalCrossentropy()
Custom training step
def trainstep(model, x, y):
# Forward pass with gradient tracking
with keras.backend.GradientTape() as tape:
predictions = model(x, training=True)
loss = lossfn(y, predictions)
# Backward pass
gradients = tape.gradient(loss, model.trainableweights)
optimizer.apply(gradients, model.trainableweights)
return loss
Custom training loop
import numpy as np
xtrain = np.random.random((1000, 784)).astype("float32")
ytrain = keras.utils.tocategorical(
np.random.randint(10, size=(1000,)), numclasses=10
)
batchsize = 32
epochs = 5
for epoch in range(epochs):
epochloss = 0
numbatches = 0
for i in range(0, len(xtrain), batchsize):
xbatch = xtrain[i:i + batchsize]
ybatch = ytrain[i:i + batchsize]
loss = trainstep(model, xbatch, ybatch)
epochloss += float(loss)
numbatches += 1
avgloss = epochloss / numbatches
print(f"Epoch {epoch + 1}/{epochs}, Loss: {avgloss:.4f}")
Custom Layers
You can create custom layers by subclassing keras.layers.Layer:
import keras
from keras import layers, ops
class AttentionLayer(keras.layers.Layer):
def init(self, units, kwargs):
super().init(kwargs)
self.units = units
def build(self, inputshape):
self.W = self.addweight(
shape=(inputshape[-1], self.units),
initializer="glorotuniform",
trainable=True,
name="attentionweight"
)
self.b = self.addweight(
shape=(self.units,),
initializer="zeros",
trainable=True,
name="attentionbias"
)
self.u = self.addweight(
shape=(self.units,),
initializer="glorotuniform",
trainable=True,
name="contextvector"
)
def call(self, inputs):
# inputs shape: (batchsize, seqlen, features)
score = ops.tanh(ops.matmul(inputs, self.W) + self.b)
attentionweights = ops.softmax(
ops.matmul(score, ops.expanddims(self.u, axis=-1)),
axis=1
)
contextvector = ops.sum(inputs attentionweights, axis=1)
return contextvector
def getconfig(self):
config = super().getconfig()
config.update({"units": self.units})
return config
Use the custom layer in a model
model = keras.Sequential([
layers.Input(shape=(50, 128)),
AttentionLayer(64),
layers.Dense(32, activation="relu"),
layers.Dense(1, activation="sigmoid")
])
model.summary()
Custom Model with Subclassing
For full control, you can create models using subclassing:
import keras
from keras import layers, ops
class VariationalAutoEncoder(keras.Model):
def init(self, latentdim, kwargs):
super().init(kwargs)
self.latentdim = latentdim
# Encoder
self.encoderdense1 = layers.Dense(256, activation="relu")
self.encoderdense2 = layers.Dense(128, activation="relu")
self.zmeanlayer = layers.Dense(latentdim)
self.zlogvarlayer = layers.Dense(latentdim)
# Decoder
self.decoderdense1 = layers.Dense(128, activation="relu")
self.decoderdense2 = layers.Dense(256, activation="relu")
self.decoderoutput = layers.Dense(784, activation="sigmoid")
def encode(self, x):
x = self.encoderdense1(x)
x = self.encoderdense2(x)
zmean = self.zmeanlayer(x)
zlogvar = self.zlogvarlayer(x)
return zmean, zlogvar
def reparameterize(self, zmean, zlogvar):
epsilon = keras.random.normal(shape=ops.shape(zmean))
return zmean + ops.exp(0.5 zlogvar) epsilon
def decode(self, z):
x = self.decoderdense1(z)
x = self.decoderdense2(x)
return self.decoderoutput(x)
def call(self, inputs):
zmean, zlogvar = self.encode(inputs)
z = self.reparameterize(zmean, zlogvar)
reconstructed = self.decode(z)
# KL divergence loss
klloss = -0.5 ops.mean(
1 + zlogvar - ops.square(zmean) - ops.exp(zlogvar)
)
self.addloss(klloss)
return reconstructed
Build and compile VAE
vae = VariationalAutoEncoder(latentdim=32)
vae.compile(
optimizer="adam",
loss="binarycrossentropy"
)
Using keras.ops for Portable Operations
keras.ops is the key module in Keras 3 that provides portable numerical operations:
import keras
from keras import ops
Basic operations
a = ops.array([1.0, 2.0, 3.0])
b = ops.array([4.0, 5.0, 6.0])
print(f"Addition: {ops.add(a, b)}")
print(f"Dot product: {ops.dot(a, b)}")
print(f"Mean: {ops.mean(a)}")
print(f"Std: {ops.std(a)}")
Matrix operations
matrix = ops.array([[1.0, 2.0], [3.0, 4.0]])
print(f"Transpose:\n{ops.transpose(matrix)}")
print(f"Determinant: {ops.linalg.det(matrix)}")
Neural network operations
logits = ops.array([2.0, 1.0, 0.1])
print(f"Softmax: {ops.softmax(logits)}")
print(f"ReLU: {ops.relu(ops.array([-1.0, 0.0, 1.0, 2.0]))}")
Random operations (seeded for reproducibility)
keras.utils.setrandomseed(42)
randomtensor = keras.random.normal(shape=(3, 3))
print(f"Random tensor:\n{randomtensor}")
Transfer Learning with Pretrained Models
Keras 3 provides many pretrained models through keras.applications:
import keras
from keras import layers
import numpy as np
Load pretrained model (without top layer)
basemodel = keras.applications.EfficientNetV2B0(
weights="imagenet",
includetop=False,
inputshape=(224, 224, 3)
)
Freeze the base model
basemodel.trainable = False
Build new model for fine-tuning
model = keras.Sequential([
basemodel,
layers.GlobalAveragePooling2D(),
layers.Dropout(0.3),
layers.Dense(256, activation="relu"),
layers.Dropout(0.2),
layers.Dense(5, activation="softmax")
])
model.compile(
optimizer=keras.optimizers.Adam(learningrate=0.001),
loss="categoricalcrossentropy",
metrics=["accuracy"]
)
model.summary()
Fine-tuning: unfreeze the last few layers
basemodel.trainable = True
for layer in basemodel.layers[:-20]:
layer.trainable = False
Re-compile with a smaller learning rate
model.compile(
optimizer=keras.optimizers.Adam(learningrate=1e-5),
loss="categoricalcrossentropy",
metrics=["accuracy"]
)
Callbacks and Monitoring
Keras 3 provides various callbacks for monitoring and controlling training:
import keras
from keras import layers
import numpy as np
model = keras.Sequential([
layers.Input(shape=(784,)),
layers.Dense(128, activation="relu"),
layers.Dense(10, activation="softmax")
])
model.compile(
optimizer="adam",
loss="categoricalcrossentropy",
metrics=["accuracy"]
)
Define callbacks
callbacks = [
keras.callbacks.EarlyStopping(
monitor="valloss",
patience=5,
restorebestweights=True,
verbose=1
),
keras.callbacks.ReduceLROnPlateau(
monitor="valloss",
factor=0.5,
patience=3,
minlr=1e-7,
verbose=1
),
keras.callbacks.ModelCheckpoint(
filepath="bestmodel.keras",
monitor="valaccuracy",
savebestonly=True,
verbose=1
),
keras.callbacks.TensorBoard(
logdir="./logs",
histogramfreq=1
)
]
Training with dummy data
xtrain = np.random.random((1000, 784)).astype("float32")
ytrain = keras.utils.tocategorical(
np.random.randint(10, size=(1000,)), numclasses=10
)
history = model.fit(
xtrain, ytrain,
batchsize=32,
epochs=50,
validationsplit=0.2,
callbacks=callbacks
)
Multi-Backend Benchmarking
One of the most powerful features of Keras 3 is the ability to compare model performance across different backends:
import os
import time
import numpy as np
def benchmarkbackend(backendname, epochs=5):
os.environ["KERASBACKEND"] = backendname
# Reload keras with the new backend
import importlib
import keras
importlib.reload(keras)
from keras import layers
# Build model
model = keras.Sequential([
layers.Input(shape=(28, 28, 1)),
layers.Conv2D(32, (3, 3), activation="relu"),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation="relu"),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation="relu"),
layers.Dense(10, activation="softmax")
])
model.compile(
optimizer="adam",
loss="categoricalcrossentropy",
metrics=["accuracy"]
)
# Generate data
x = np.random.random((5000, 28, 28, 1)).astype("float32")
y = keras.utils.tocategorical(
np.random.randint(10, size=(5000,)), numclasses=10
)
# Benchmark
starttime = time.time()
model.fit(x, y, batchsize=64, epochs=epochs, verbose=0)
elapsed = time.time() - starttime
print(f"{backendname}: {elapsed:.2f} seconds for {epochs} epochs")
return elapsed
Run benchmarks (uncomment installed backends)
benchmarkbackend("tensorflow")
benchmarkbackend("jax")
benchmarkbackend("torch")
Saving and Loading Models
Keras 3 supports multiple saving formats:
import keras
Native Keras format (recommended)
model.save("mymodel.keras")
loadedmodel = keras.saving.loadmodel("mymodel.keras")
Export to SavedModel format (TensorFlow serving)
Only available with TensorFlow backend
model.export("savedmodeldir", format="tfsavedmodel")
Save weights only
model.saveweights("modelweights.weights.h5")
model.loadweights("modelweights.weights.h5")
Save and load architecture only (JSON)
jsonconfig = model.tojson()
newmodel = keras.models.modelfromjson(jsonconfig)
Data Pipeline with Keras Preprocessing Layers
import keras
from keras import layers
import numpy as np
Image data augmentation
dataaugmentation = keras.Sequential([
layers.RandomFlip("horizontal"),
layers.RandomRotation(0.1),
layers.RandomZoom(0.1),
layers.RandomContrast(0.1),
])
Preprocessing layer
preprocessing = keras.Sequential([
layers.Rescaling(1.0 / 255),
layers.Resizing(224, 224),
])
Combine in a model
model = keras.Sequential([
layers.Input(shape=(None, None, 3)),
dataaugmentation,
preprocessing,
keras.applications.MobileNetV3Small(
weights="imagenet",
includetop=False
),
layers.GlobalAveragePooling2D(),
layers.Dense(128, activation="relu"),
layers.Dense(10, activation="softmax")
])
Best Practices
1. Choosing the Right Backend
Each backend has its own strengths:
| Backend | Strengths | Best For |
|---------|-----------|----------|
| TensorFlow | Mature deployment ecosystem (TFLite, TF Serving) | Production deployment, mobile |
| JAX | High JIT compilation performance, auto-differentiation | Research, large-scale training |
| PyTorch | Easy debugging, eager execution, large research community | Prototyping, research |
2. Use keras.ops for Portability
Always use keras.ops instead of backend-specific operations:
# Good - portable across backends
import keras.ops as ops
result = ops.matmul(a, b)
Avoid - tied to a single backend
import torch
result = torch.matmul(a, b)
3. Mixed Precision Training
import keras
Enable mixed precision for faster training
keras.mixedprecision.setglobalpolicy("mixedfloat16")
model = keras.Sequential([
keras.layers.Input(shape=(784,)),
keras.layers.Dense(256, activation="relu"),
keras.layers.Dense(10, activation="softmax",
dtype="float32") # Keep output as float32
])
4. Setting Seeds for Reproducibility
import keras
keras.utils.setrandomseed(42)
For JAX backend, also ensure:
export XLAFLAGS=--xlagpudeterministicops=true
5. Profiling and Optimization
import keras
import time
Use keras.utils.Progbar for manual monitoring
progbar = keras.utils.Progbar(target=100)
for i in range(100):
time.sleep(0.01)
progbar.update(i + 1, values=[("loss", 0.5 - i * 0.005)])
6. Recommended Project Structure
myproject/
├── configs/
│ └── model
config.py
├── data/
│ └── preprocessing.py
├── models/
│ ├── init.py
│ ├── basemodel.py
│ └── customlayers.py
├── training/
│ ├── train.py
│ └── callbacks.py
├── evaluation/
│ └── evaluate.py
├── requirements.txt
└── README.md
Migrating from Keras 2 to Keras 3
If you have Keras 2 code, here are the main changes to be aware of:
# Keras 2 (old)
from tensorflow import keras
from tensorflow.keras import layers
Keras 3 (new)
import keras
from keras import layers
Keras 2: tf.keras.backend.xxx
import tensorflow.keras.backend as K
result = K.sum(x)
Keras 3: keras.ops.xxx
from keras import ops
result = ops.sum(x)
Keras 2: model.save("model.h5")
Keras 3: model.save("model.keras") # new format
Conclusion
Keras 3 is a major leap forward in the deep learning ecosystem. With its multi-backend architecture, you gain the flexibility to choose the best backend for your needs without having to rewrite your code. Key features that make Keras 3 worth learning:
Keras 3 is suitable for developers at all levels, from beginners just learning deep learning to engineers building production systems. With a growing ecosystem and an active community, Keras 3 is a solid choice for your next deep learning project.
For more information, visit the official Keras documentation at keras.io and explore model examples in the official Keras GitHub repository.