Keras is an open-source neural-network library written in Python. It is designed to enable fast experimentation with deep neural networks, offering a high-level API that can run on top of multiple backends such as TensorFlow, Theano, or Microsoft Cognitive Toolkit. Originally developed by François Chollet, Keras emphasizes user-friendliness, modularity, and extensibility, making it a popular choice for both beginners and researchers. Since its integration into TensorFlow as tf.keras, it has become the official high-level API for TensorFlow.
1 History
1.1 Development and initial release
François Chollet began developing Keras in 2015 as part of a research project at Google. The first public release (version 0.1.0) appeared in March 2015. The library was designed to simplify the construction and training of neural networks by providing a clean, consistent API that abstracted away many lower‑level details. Its rapid adoption was driven by its ease of use and the growing popularity of deep learning.
1.2 Integration with TensorFlow
In 2016, Google announced that Keras would be integrated into TensorFlow. TensorFlow 1.0 (released February 2017) included tf.contrib.keras, a wrapper providing most Keras functionality. With TensorFlow 2.0 (September 2019), tf.keras became the official high-level API and the primary way to build models in TensorFlow. This integration eliminated many compatibility issues and made Keras the standard interface for TensorFlow users.
1.3 Subsequent versions and adoption
After the TensorFlow integration, Keras continued to evolve as a standalone package (keras on PyPI) but with a strong alignment to tf.keras. Version 2.x brought performance improvements, new layers (e.g., normalization, attention), and better support for distributed training. Keras has become one of the most widely used deep‑learning frameworks in both academia and industry, with extensive documentation and a large community.
2 Features
2.1 User-friendly API
Keras provides a concise, intuitive API that reduces the cognitive load of building neural networks. Users can construct models with minimal boilerplate code. The library emphasizes “ease of use” with sensible defaults for activation functions, weight initializers, and optimizers, making it accessible to those new to deep learning while still powerful enough for advanced research.
2.2 Modular architecture
Keras is built around the concept of modules: each neural network is composed of configurable building blocks. Models are created by composing layers, which can be reused and repurposed. This modular design facilitates rapid prototyping and experimentation.
2.2.1 Model building: Sequential, Functional, and Subclassing
Keras supports three model‑building paradigms:
- Sequential API: A linear stack of layers, best suited for simple feed‑forward or sequential architectures.
- Functional API: Allows the creation of arbitrary graph‑like models (multi‑input, multi‑output, shared layers). It provides great flexibility while remaining high‑level.
- Subclassing: Users can subclass
tf.keras.Modeland implement custom forward passes via thecallmethod, giving full control for advanced research.
2.2.2 Layers, activations, and optimizers
Keras includes a comprehensive library of built‑in layers (Dense, Conv2D, LSTM, etc.), activation functions (ReLU, softmax, tanh, etc.), and optimizers (SGD, Adam, RMSprop, etc.). All are configurable with intuitive parameters. Users can also define custom layers and optimizers by subclassing the respective base classes.
2.3 Backend-agnostic design
Originally, Keras could run on top of multiple backends (TensorFlow, Theano, CNTK) through a common interface. This allowed users to switch backends without changing model code. Most backends beyond TensorFlow are now deprecated, but the design principle of abstraction remains.
2.3.1 TensorFlow backend
Since the integration, TensorFlow is the primary (and effectively only) supported backend. tf.keras provides all Keras functionality directly within TensorFlow, benefiting from TensorFlow’s performance optimizations, graph execution, and ecosystem.
2.3.2 Theano and CNTK (historical)
Keras originally supported Theano (University of Montreal) and Microsoft Cognitive Toolkit (CNTK) as alternative backends. Theano development ceased in 2017, and CNTK became less maintained. As of Keras 2.4.0, support for these backends was removed, and the library now exclusively relies on TensorFlow.
2.4 Support for multiple hardware accelerators
Keras transparently leverages hardware accelerators for faster training.
2.4.1 CPU, GPU, TPU
Keras runs on CPUs by default. With TensorFlow, it automatically detects and uses available GPUs (NVIDIA CUDA-compatible). TensorFlow also supports Google’s Tensor Processing Units (TPUs) for high‑throughput training, and tf.keras models can be deployed on TPUs with minor configuration changes.
2.4.2 Distributed training
For large‑scale training, Keras integrates with TensorFlow’s distribution strategies (e.g., MirroredStrategy, MultiWorkerMirroredStrategy, TPUStrategy). These strategies allow training across multiple GPUs or machines with minimal code changes, typically by wrapping the model inside a strategy scope.
3 Architecture and components
3.1 Core data structures: tensors and models
The fundamental data structure in Keras is the tensor (a multi‑dimensional array). Models are represented as tf.keras.Model or tf.keras.Sequential objects. A model encapsulates the computational graph (layers, weights, operations) and provides high‑level methods for training, evaluation, and prediction.
3.2 Layers
Layers are the primary building blocks of Keras models. Each layer processes input data and produces output, and may contain trainable parameters (weights and biases).
3.2.1 Dense, convolutional, recurrent layers
- Dense (fully connected):
keras.layers.Dense(units, activation)– used for classic feed‑forward networks. - Convolutional:
Conv2D,Conv1D,Conv3D– essential for image and signal processing. - Recurrent:
SimpleRNN,LSTM,GRU– designed for sequential data (time series, text).
3.2.2 Custom layer creation
Users can create custom layers by subclassing tf.keras.layers.Layer and implementing __init__, build (to create weights), and call (forward logic). This enables advanced behavior such as novel activation functions or weight sharing.
3.3 Losses, metrics, and optimizers
Keras provides a wide range of built‑in loss functions, metrics, and optimizers. All are callable objects that can be configured and combined.
3.3.1 Built-in loss functions
Common losses include MeanSquaredError, CategoricalCrossentropy, BinaryCrossentropy, KLDivergence, and Huber. They are specified as strings or class instances.
3.3.2 Custom metrics
Metrics (e.g., accuracy, precision, recall) can be used to monitor training. Custom metrics are created by subclassing tf.keras.metrics.Metric and implementing update_state, result, and reset_state. They can be passed to model.compile.
3.4 Callbacks
Callbacks are functions that execute at specific points during training (e.g., end of each epoch). They allow logging, checkpointing, early stopping, and dynamic learning rate adjustments.
3.4.1 ModelCheckpoint, EarlyStopping, TensorBoard
- ModelCheckpoint: Saves model weights (or entire model) after each epoch based on a monitored quantity (e.g., validation loss).
- EarlyStopping: Halts training when a monitored metric stops improving, preventing overfitting.
- TensorBoard: Logs metrics, histograms, and graph visualizations for monitoring in TensorBoard.
3.5 Training and evaluation workflow
Keras provides a high‑level workflow that abstracts the training loop, yet also allows custom loops for advanced scenarios.
3.5.1 Model.compile, Model.fit, Model.evaluate
compileconfigures the model for training (optimizer, loss, metrics).fittrains the model for a specified number of epochs, using data intf.data.Datasetor NumPy arrays. It supports validation, callbacks, and batching.evaluatecomputes loss and metrics on test data.predictgenerates predictions for new inputs.
3.5.2 Custom training loops with tf.GradientTape
For fine‑grained control (e.g., gradient penalties, irregular training schedules), users can write custom training loops. Using tf.GradientTape, they record forward‑pass operations, compute gradients manually, and apply them with an optimizer. This approach is common in research.
4 Usage and workflows
4.1 Building a simple model
4.1.1 Sequential model example
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, validation_data=(x_val, y_val))
4.1.2 Functional API example
inputs = tf.keras.Input(shape=(784,))
x = tf.keras.layers.Dense(64, activation='relu')(inputs)
x = tf.keras.layers.Dense(64, activation='relu')(x)
outputs = tf.keras.layers.Dense(10, activation='softmax')(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='adam', loss='categorical_crossentropy')
4.2 Data preprocessing
4.2.1 Keras preprocessing layers
Keras includes preprocessing layers that can be integrated directly into the model: Rescaling, Normalization, StringLookup, CategoryEncoding, and image augmentation layers (e.g., RandomFlip, RandomRotation). These layers can be placed at the start of a model, ensuring that preprocessing is part of the exported graph.
4.2.2 tf.data integration
Keras models can consume tf.data.Dataset objects directly. This allows efficient data pipelines with caching, shuffling, batching, and prefetching. The combination of tf.data and Keras simplifies handling large datasets.
4.3 Model serialization and saving
4.3.1 Save and load whole model
Models can be saved in two formats: the native Keras .keras format (recommended) and the TensorFlow SavedModel format. The entire architecture, weights, and training configuration are preserved.
model.save('my_model.keras')
loaded_model = tf.keras.models.load_model('my_model.keras')
4.3.2 Save and load weights only
For lighter storage or transfer learning, users can save only the weights (in .h5 or TensorFlow checkpoint format):
model.save_weights('weights.h5')
model.load_weights('weights.h5')
4.4 Transfer learning and fine-tuning
Keras simplifies transfer learning by allowing users to load a pretrained model (e.g., ResNet, VGG, BERT from tf.keras.applications) and either freeze its layers or fine‑tune them. Typically, the base model is instantiated with include_top=False, custom top layers are added, and then part or all of the base model is set trainable=False before training on a new dataset.
5 Ecosystem and extensions
5.1 KerasTuner
KerasTuner is a hyperparameter tuning library that integrates with Keras models. It supports various search algorithms (Random Search, Hyperband, Bayesian Optimization) and can automatically optimize learning rates, number of layers, units, and other hyperparameters. Users define a HyperModel class and call tuner.search().
5.2 KerasCV (computer vision)
KerasCV provides modular building blocks for computer vision tasks: image classification, object detection, segmentation, and data augmentation. It includes pretrained models (e.g., YOLOV8, RetinaNet) and preprocessing layers such as RandomFlip, MixUp, and CutMix.
5.3 KerasNLP (natural language processing)
KerasNLP offers a high‑level API for NLP tasks, including tokenization, model architectures (BERT, GPT, T5), and preprocessing. It supports training and fine‑tuning transformer‑based models on text classification, summarization, and question answering.
5.4 Keras Reinforcement Learning (Keras-RL)
Keras‑RL is a library that implements reinforcement‑learning algorithms (DQN, DDPG, A2C, PPO) using Keras models. It provides agents, environments, and training loops, allowing researchers to apply deep RL to custom tasks.
6 Comparison with other frameworks
6.1 Keras vs. PyTorch
PyTorch is known for its imperative, “define‑by‑run” style, offering more granular control and native Pythonic feel. Keras (especially tf.keras) provides a higher‑level, more declarative API that is often easier for beginners and rapid prototyping. PyTorch has become more popular in academic research, while Keras remains strong in industry applications and among those who prefer simplicity.
6.2 Keras vs. TensorFlow Core
TensorFlow Core refers to the lower‑level TensorFlow API (custom graph construction, gradient tapes, tf.function). Keras builds on top of this, abstracting away many details. For standard models and training loops, Keras is the recommended approach; TensorFlow Core is used for custom operations, performance‑critical code, or when fine‑grained control is required.
6.3 Keras vs. JAX/Flax
JAX is a library for high‑performance numerical computing (autodiff, JIT compilation). Flax is a neural‑network library built on JAX, offering a style similar to Keras but with more flexibility and emphasis on functional programming. JAX/Flax is popular in research for its speed and composability, but demands more manual setup. Keras provides a more mature, all‑in‑one ecosystem with built‑in tools for data pipeline, training, and deployment.