Keras
Keras is a high-level Python API for building and training neural networks that, since Keras 3, runs the same code on JAX, TensorFlow, or PyTorch.
Keras is an open-source, high-level Python API for building and training neural networks. François Chollet released it in March 2015 to make deep learning experiments quick to write: a model is a stack or graph of layers, compile sets the optimizer, loss function, and metrics, and fit runs the training loop. Keras became the official high-level API of TensorFlow, and Keras 3, released in November 2023, runs the same model code on JAX, TensorFlow, or PyTorch, chosen when Keras is imported. Its short, readable code makes it a common choice for teaching, prototyping, and Kaggle competitions, as well as for production models in image classification and natural language processing.
How Keras Works#
A Keras model is built from layers such as Dense, Conv2D, LSTM, and MultiHeadAttention, each holding its own weights. Keras offers three ways to assemble them: the Sequential API for a plain stack, the functional API for graphs with branches, shared layers, or multiple inputs, and subclassing keras.Model for full control of the forward pass. A compiled model trains with fit, which handles batching, backpropagation, metrics, validation data, and callbacks for checkpointing or early stopping, and then scores new data with evaluate and predict. This code trains a small digit classifier on the MNIST dataset that ships with Keras:
import os
os.environ["KERAS_BACKEND"] = "torch" # or "jax" or "tensorflow", set before importing Keras
import keras
# MNIST handwritten digits, with pixels scaled to [0, 1]
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Define the model as a stack of layers
model = keras.Sequential(
[
keras.Input(shape=(28, 28)),
keras.layers.Flatten(),
keras.layers.Dense(128, activation="relu"),
keras.layers.Dense(10, activation="softmax"),
]
)
# Configure training, then train and evaluate
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(x_train, y_train, epochs=2, batch_size=64, verbose=0)
loss, accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f"Test accuracy: {accuracy:.3f}")After two epochs it prints a test accuracy of about 0.96, and changing "torch" to "jax" or "tensorflow" runs the same model on another framework without editing any other line.
Keras 3 and Its Backends#
Keras originally ran on several engines, including Theano, TensorFlow, and CNTK. Keras 2.3, released in September 2019, was its last multi-backend release, after which development moved into TensorFlow as tf.keras. Keras 3 restored backend independence: layers and models are written against keras.ops, a NumPy-style set of operations that each backend implements, so a model saved in the .keras format can be reloaded and trained in any backend. With the PyTorch backend, a Keras model is also a torch.nn.Module that a custom PyTorch training loop can use, and since Keras 3.8 an inference-only OpenVINO backend runs trained models on Intel hardware.
Since TensorFlow 2.16, installing TensorFlow installs Keras 3, and code that still needs Keras 2 uses the separate tf_keras package. Model converters target Keras too: when Ultralytics YOLO exports YOLO26 to a TensorFlow SavedModel, the onnx2tf converter rebuilds the PyTorch network from its ONNX graph as a Keras model, layer by layer, before saving it.
Pretrained Models in KerasHub#
KerasHub provides Keras 3 implementations of popular architectures with pretrained checkpoints, such as ResNet for images, BERT for text, and the Gemma large language models. A preset name loads a model ready for transfer learning: keras_hub.models.ImageClassifier.from_preset("resnet_50_imagenet") returns an ImageNet-trained classifier that fine-tuning adapts to a new dataset with the same fit call used above. Like the rest of Keras 3, KerasHub models run on any of the three training backends.
Keras vs. TensorFlow and PyTorch#
- TensorFlow: A full framework with its own runtime, compiler, and deployment tools such as TensorFlow Serving and LiteRT. Keras is the API many TensorFlow users write models in, and TensorFlow is one of the engines that runs Keras code.
- PyTorch: A framework where users typically write the training loop themselves or use a library such as PyTorch Lightning. Keras supplies the loop through
fitand can use PyTorch as its backend, so the two are not mutually exclusive. - JAX: Google's library of composable function transformations for differentiation, XLA compilation, and vectorization. JAX has no built-in layer or training API, and Keras is one of the ways to get one on top of it.










