Keras

On this page 7

Keras is a high-level deep learning API written in Python that lets developers define, train, evaluate and export neural networks with a small set of consistent building blocks. Among AI frameworks & tools, it occupies the layer above numerical engines: since Keras 3, the same model code runs on JAX, TensorFlow or PyTorch as interchangeable backends.

Keras was created by François Chollet and first released in 2015. It became the official high-level interface of TensorFlow 2 as tf.keras, and the multi-backend Keras 3 release in late 2023 turned it back into an independent, framework-agnostic library. Its design goal is progressive disclosure of complexity: simple models need a few lines, while advanced users can override every step of training.

How Keras structures a deep learning model

Keras structures every model as a graph of layers, which are objects that hold trainable weights and a forward computation. A Model groups layers, tracks their variables and exposes the standard workflow of compile(), fit(), evaluate() and predict().

The compile() step binds three components to the model: an optimizer such as Adam or SGD, a loss function that measures error, and a list of metrics for monitoring. The fit() loop then iterates over batches, computes gradients through the active backend and updates the weights, while callbacks handle checkpoints, early stopping and learning-rate schedules.

Keras offers three ways to build models, and the choice decides how much flexibility and introspection the developer gets. The table below compares them.

StyleHow the model is definedBest suited for
Sequential APIA linear stack of layersSimple feed-forward networks and prototypes
Functional APIA directed acyclic graph of layer callsMultiple inputs, outputs, skip connections
Model subclassingPython class with a custom call() methodDynamic control flow and research code
Model-building styles in Keras

The Functional API is the recommended default for production because the resulting graph can be plotted, serialized and inspected layer by layer. Subclassed models trade that transparency for full Python freedom inside the forward pass.

The multi-backend architecture of Keras 3

Keras 3 separates the user-facing API from the numerical backend that executes tensor operations and computes gradients. The backend is selected with the KERAS_BACKEND environment variable or a configuration file before Keras is imported, and it cannot be switched inside a running process.

The keras.ops namespace provides a NumPy-like operation set that maps to each backend. Custom layers written only with keras.ops stay portable across backends, whereas code that calls torch or tf functions directly ties the model to one engine.

BackendMain strengthTypical consideration
JAXXLA compilation, fast training on TPUs and GPUsFunctional style requires stateless thinking
TensorFlowMature serving with SavedModel and TF ServingLarger install and slower eager debugging
PyTorchInteroperability with the PyTorch ecosystemCompilation gains depend on torch.compile
OpenVINOOptimized inference on Intel hardwareInference only, no training support
Keras 3 backends and their typical strengths

Because weights are stored independently of the backend, a model trained with JAX can be reloaded and served with TensorFlow or embedded in a PyTorch pipeline. This portability is the central technical argument for Keras 3.

Keras compared with neighboring frameworks

Keras differs from PyTorch, TensorFlow and JAX mainly in level of abstraction: it is an API that sits on top of them rather than a competing tensor engine. The comparison below reflects how practitioners usually position these tools.

ToolAbstraction levelTraining loopPrimary audience
KerasHigh-level APIBuilt-in fit(), overridableApplied engineers and educators
PyTorchLow-level tensor libraryWritten by the userResearchers and model developers
TensorFlowFull platform with graph runtimeUsually through KerasProduction and mobile teams
JAXComposable function transformsWritten by the user or FlaxLarge-scale research
PyTorch LightningTraining wrapperStructured Trainer loopPyTorch users who want less boilerplate
Position of Keras among related deep learning tools

The closest conceptual neighbor is PyTorch Lightning, which also removes boilerplate, but it remains tied to PyTorch. Keras is unique in offering a single model definition that several engines can execute.

Practical use and configuration of Keras

Keras is used most often for computer vision, text classification, time series forecasting and tabular deep learning, and the KerasHub library supplies pretrained models such as image classifiers and language models with matching preprocessing. Training data can come from tf.data pipelines, PyTorch DataLoader objects, NumPy arrays or Python generators.

Several configuration choices have a large effect on speed and memory. The decision table summarizes common situations.

SituationRecommended choiceReason
Training on TPUsJAX backendBest XLA support and scaling
Limited GPU memoryMixed precision mixed_float16Halves activation memory on modern GPUs
Model must join a PyTorch codebasePyTorch backendLayers behave as torch.nn.Module objects
Long training runsModelCheckpoint and EarlyStopping callbacksProtects progress and limits overfitting
Multi-device trainingkeras.distribution APIData and model parallelism without rewrites
Common Keras configuration decisions

Models are saved in the native `.keras` format, a zip archive containing the architecture configuration and weights. For deployment, Keras can export inference artifacts such as a TensorFlow SavedModel, which then feeds serving systems or conversion to ONNX or LiteRT.

Limitations and common mistakes with Keras

The convenience of Keras hides details that matter at scale, and most problems arise when backend-specific code leaks into supposedly portable models. Experienced users watch for the following issues.

  • Version confusion between tf.keras from Keras 2 and standalone Keras 3 breaks imports and saved models.
  • Legacy HDF5 files from older releases may need conversion before loading in Keras 3.
  • Custom layers without a get_config() method fail to deserialize from the .keras format.
  • Calling backend functions directly inside layers removes portability to other backends.
  • Very unusual research loops can feel constrained, although overriding train_step() usually solves this.

Performance also varies by backend and model type, so benchmarking each backend on the actual workload is more reliable than assuming one engine is always fastest.

The short version

Keras is a toolkit that makes building neural networks feel like assembling building blocks. A developer picks layers, stacks or connects them, and tells Keras how to learn from data. Keras then handles the heavy mathematics in the background.

The newest version adds one useful trick: it can run on three different engines, JAX, TensorFlow or PyTorch, without rewriting the model. It is like writing a document once and printing it on any brand of printer. The key takeaway is that Keras trades a little low-level control for speed and simplicity.

Where it makes a difference

A small machine learning team at a regional logistics company needs a model that reads photos of damaged parcels and sorts them into four damage categories. Using KerasHub, an engineer loads a pretrained image classifier, replaces the final layer with a four-class output, and fine-tunes it on about 12,000 labeled photos with the JAX backend on a single cloud GPU.

Training with mixed precision and an EarlyStopping callback finishes in under two hours and reaches about 93 percent validation accuracy. The company’s existing serving stack runs on TensorFlow, so the team reloads the same .keras file with the TensorFlow backend and exports a SavedModel. The result is a model trained on one engine and served on another with no code rewrite, which shortened the project by several weeks.