Keras is an open-source library and high-level application programming interface for developing deep learning models in Python. It provides abstractions for neural-network layers, model construction, optimization, evaluation, and deployment. Its central design objective is to make common workflows concise and consistent while preserving ways to customize the underlying computation.
First released in 2015, Keras became closely associated with TensorFlow, whose integrated tf.keras interface made it a principal route to building TensorFlow models. Keras 3, released in 2023, reestablished a multi-backend architecture supporting TensorFlow, JAX, and PyTorch. The distinction matters: Keras defines a model-development interface, while a computational backend supplies much of the tensor execution, differentiation, compilation, and hardware support.
Keras is used for tasks ranging from image classification and sequence modeling to generative modeling and adaptation of pretrained networks. Its significance lies less in introducing a particular learning algorithm than in organizing the engineering of neural networks around reusable components. Its history, programming model, backend architecture, and deployment formats also illustrate the practical trade-offs between ease of use, portability, and access to framework-specific capabilities.
Origins and evolution
Keras was created by François Chollet and first released in March 2015. It emerged during a period when building neural networks often required detailed manipulation of symbolic expressions and framework-specific computational machinery. Early Keras placed a compact layer-and-model interface above existing numerical systems, initially Theano and subsequently TensorFlow. Its emphasis on readable code and rapid experimentation distinguished it from interfaces that exposed more of the execution engine directly.
The name comes from the Greek word for “horn,” referring to the classical literary image of true dreams passing through a gate of horn. The project originated in connection with research on neural computation, but it developed into a general-purpose library rather than remaining tied to one research problem. Its components represented familiar operations—dense transformations, convolutions, recurrent processing, and nonlinear activations—in a form that users could combine without implementing their mathematical derivatives individually.
Keras 2 appeared in 2017, with an API designed to accommodate both the standalone library and integration into TensorFlow. TensorFlow incorporated Keras through tf.keras; with TensorFlow 2.0 in 2019, that interface became the framework’s principal high-level model-building API. This integration connected Keras to TensorFlow’s eager execution, distribution strategies, data pipelines, and serving infrastructure. Meanwhile, the older standalone multi-backend implementation reached the end of its development line, and subsequent Keras 2 development centered on TensorFlow. Historical references to “Keras” therefore do not always identify the same package, execution system, or compatibility guarantees.
Keras 3, announced in November 2023, changed that relationship by providing a renewed multi-backend implementation. Rather than restoring the original architecture unchanged, it introduced a common operations namespace and stricter conventions for model state, making portable components practical across modern execution systems. TensorFlow 2.16 made Keras 3 the default implementation exposed through tf.keras; a separate legacy package remained available for software requiring Keras 2 behavior. Consequently, version numbers and import paths are important when interpreting examples, reproducing experiments, or migrating applications.
The project is distributed under the Apache License 2.0. Its development history reflects a recurring tension in software engineering: a stable, accessible public interface must coexist with rapidly changing execution engines. Old tutorials may contain obsolete argument names, saving conventions, or assumptions about graph execution. The continuity of the Keras name does not imply that every program written for an early release runs unchanged on later versions.
Computational backends and portability
Keras separates high-level model descriptions from many details of numerical execution. A backend implements tensor operations, variable handling, differentiation, and execution on supported devices. In Keras 3, TensorFlow, JAX, and PyTorch provide distinct backend environments with their own compilers, device runtimes, and ecosystem integrations. Keras is therefore not an independent replacement for these systems at every level: it supplies a common modeling layer that uses their capabilities.
Backend selection is normally made before importing Keras, for example through the KERAS_BACKEND environment variable. A model written entirely with supported Keras layers and backend-neutral operations can often be reconstructed and run under another backend without changing its architectural definition. This permits an organization to preserve a shared modeling interface while evaluating different execution environments. It does not mean that a live tensor or runtime resource can be moved freely between frameworks, or that every possible model is portable.
The principal abstraction for portable numerical code is keras.ops. It includes array operations and neural-network primitives, such as matrix multiplication, reductions, reshaping, and activation functions. A custom layer that uses keras.ops.matmul rather than a framework-specific matrix multiplication is less tightly coupled to one backend. Keras also supplies facilities for state management and random operations that help custom components satisfy different tracing and execution requirements. Direct calls to backend libraries remain possible, but they can restrict portability.
The distinction becomes especially important with symbolic model construction. A Keras symbolic tensor represents a placeholder within a model’s structure; it is not simply an ordinary eager tensor from the selected backend. In the Functional API, applying arbitrary backend operations directly to such placeholders can violate the model-construction rules. Using Keras operations, or encapsulating suitable computation within a layer, preserves the information Keras needs to track inputs, outputs, and dependencies in a computational graph.
Portability is a compatibility goal, not a promise of identical behavior in every environment. Backends can differ in available operators, supported data types, random-number generation, numerical kernels, and compilation constraints. Some advanced distribution or export features are backend-specific. Small differences in floating-point arithmetic can also accumulate during training. A portable architecture may therefore require renewed performance testing and numerical validation on each backend, even when its source code is unchanged.
Layers, tensors, and model construction
The layer is Keras’s primary unit of computation and state. A layer receives one or more tensors and returns transformed tensors; it may own trainable parameters, non-trainable state, or neither. A dense layer, for example, computes a transformation of the form , where is a weight matrix, a bias vector, and an activation function. Convolution, pooling, normalization, embedding, and attention layers follow the same broad interface while implementing different mathematical operations.
Keras distinguishes configuration from state. Configuration includes choices such as the number of output units or the size of a convolutional kernel. State includes learned weights and other persistent variables. Many layers create shape-dependent weights when first built, either explicitly or on receiving inputs. A dense layer with an input dimension of 128 and 64 output units has parameters when a bias is included. Separating construction from weight creation allows reusable components whose input dimensions are not specified immediately.
The Sequential interface represents a linear stack in which each layer’s output feeds the next layer. It is suitable for models with a single main path, such as a small multilayer perceptron or a straightforward image classifier. An input specification followed by dense, activation, and output layers can define a complete network in a few expressions. This simplicity is also a limitation: multiple inputs, shared branches, and skip connections do not naturally fit a single sequential chain.
The Functional API describes directed networks of connected layers using explicit symbolic inputs and outputs. It supports multiple inputs and outputs, shared layers, and structures such as a residual connection. Calling the same layer object on two inputs shares its weights; constructing two otherwise identical layer objects normally creates separate weights. This distinction is essential for models that compare two inputs using the same encoder. Functional models retain an inspectable topology, which facilitates shape checking, visualization, and extraction of intermediate representations.
Model subclassing offers a more programmatic alternative. A developer defines a subclass of keras.Model, creates components, and implements a call method describing the forward computation. This is useful for unusual control flow or architectures that are awkward to express as a fixed graph. The additional freedom can make automatic inspection and configuration-based reconstruction more difficult. These three interfaces are complementary: a subclassed model can contain Functional or Sequential models, and any model can serve as a component within a larger architecture.
Training, objectives, and evaluation
Keras organizes standard training around compile, fit, evaluate, and predict. Despite its name, compile primarily configures the learning procedure: it associates a model with an optimizer, one or more losses, and evaluation metrics, along with execution options. It is not synonymous with translating an entire program into machine instructions. Depending on the backend and configuration, subsequent execution may additionally involve tracing and compiler optimization.
For a typical supervised learning problem, parameters are adjusted to minimize an objective such as
Here, is the model, is a loss function, and is a regularization term. Keras computes the relevant objective, obtains gradients through the backend, and applies optimizer updates. Available optimizers include stochastic-gradient methods and the Adam optimizer. Choices such as batch size and learning rate affect convergence and are not determined automatically by the architecture.
Losses and metrics have different roles. The loss drives optimization; a metric reports a property of predictions or training behavior. Accuracy can be useful for classification while being unsuitable as a differentiable training objective. Keras supports different losses and metrics for different model outputs, together with sample weighting and task-dependent aggregation. Correct configuration depends on the interpretation of the outputs. For example, a classification loss must be told whether it receives probabilities or unnormalized scores, usually called logits.
The fit method manages batches and epochs, tracks metrics, and invokes callbacks at defined points in the training process. Callbacks can save checkpoints, adjust learning rates, write diagnostic logs, or implement early stopping. Validation measurements help detect overfitting, but repeatedly selecting architectures against the same validation set can itself bias development. A separate test set is used to estimate performance after model-selection decisions have been made.
Inference and evaluation also depend on layer mode. Dropout behaves differently during training and inference, while batch normalization maintains statistics whose use depends on the training flag and configuration. Calling a model directly, using predict, and executing a training step are therefore not interchangeable in every context. Keras standardizes these transitions, but it cannot determine whether the chosen objective, labels, or evaluation protocol correctly represents the intended application.
Custom components and automatic differentiation
Keras provides extension points below its standard layers and training loop. A custom layer generally defines persistent state, a forward computation, and, when needed, a serializable configuration. The conventional pattern creates input-independent sublayers in __init__, shape-dependent weights in build, and tensor calculations in call. Weights created through add_weight become part of the layer’s tracked state, enabling optimizers, checkpointing, and model inspection to handle them consistently.
State tracking is not merely bookkeeping. If a variable is created outside the mechanisms Keras recognizes, it may fail to appear among the model’s trainable parameters or saved weights. Keras 3 also places stronger emphasis on creating state before execution paths that require tracing, particularly for compatibility with JAX. Creating new parameters conditionally during repeated calls can conflict with assumptions about a model’s fixed state structure. A component’s behavior therefore depends both on its mathematical formula and on when and how its variables are established.
The computational backend supplies automatic differentiation. During training, differentiable operations are recorded or transformed so that derivatives of the objective with respect to parameters can be calculated. Neural-network training commonly uses reverse-mode differentiation, corresponding to backpropagation through the model’s operations. This avoids hand-coding derivatives for every layer, but it does not make arbitrary Python code differentiable. Converting tensors to ordinary host-language values, calling unsupported external routines, or using non-differentiable operations can interrupt the gradient path.
For learning procedures that exceed the built-in workflow, Keras permits custom training steps or fully custom loops. Examples include a generative adversarial network with separate generator and discriminator optimizers, a model trained with several interacting losses, or an algorithm that accumulates gradients over multiple batches. Overriding a training step can retain the surrounding fit machinery, including callbacks and epoch-level reporting. A fully custom loop gives more control but transfers responsibility for metric updates, distribution behavior, and checkpointing to the application.
Backend-neutral model computation does not automatically make a custom training loop backend-neutral. TensorFlow commonly uses gradient tapes, PyTorch uses its autograd system, and JAX commonly expresses updates as transformations of functions with explicit state. Keras also exposes stateless interfaces that make parameters and updated state explicit, helping integration with functional execution styles. The appropriate abstraction depends on the experiment: ordinary supervised training benefits from the standard loop, while specialized research may require deliberate use of backend-specific mechanisms.
Data preparation and pretrained models
Keras models operate on numerical tensors, but the quality and organization of training data remain separate concerns. Its training interfaces accept array-like inputs and several forms of iterable data pipelines, with the precise options depending on the backend and data adapter. Data may be supplied from NumPy arrays, TensorFlow datasets, PyTorch data loaders, or Keras utilities. These adapters bridge input formats; they do not by themselves resolve inconsistent labels, missing observations, or inappropriate sampling.
Preprocessing layers allow some transformations to become part of the model. Examples include numerical normalization, categorical lookup, text vectorization, and image resizing. Certain layers have an adapt method that estimates statistics or constructs a vocabulary from data. Those estimates are part of the learned preprocessing state. Fitting them on validation or test examples can introduce data leakage, even when the neural-network weights are trained only on the designated training split. Some preprocessing facilities also have backend-specific or pipeline-specific restrictions that differ from those of ordinary numerical layers.
Integrating preprocessing with a model can reduce discrepancies between training and inference. A saved normalization layer, for instance, can apply the same stored means and variances to incoming data that were used during model development. Data augmentation layers serve a different purpose: transformations such as random flips or rotations generate varied training examples and are typically inactive during inference. Their suitability is task-dependent; a transformation that preserves labels in one setting can destroy relevant information in another.
Keras Applications supplies established computer vision architectures, including versions of VGG, Inception, and ResNet, with options for pretrained weights. Many such weights originate from training on ImageNet. These models support transfer learning: a pretrained feature extractor can be combined with a new output head, trained with the backbone frozen, and later partially or fully updated through fine-tuning. Input scaling, image dimensions, and preprocessing conventions vary among model families and remain part of the model’s effective specification.
Related ecosystem projects extend this approach with additional pretrained architectures, task wrappers, and preset configurations. Such resources make sophisticated models easier to instantiate, but the availability of pretrained weights does not establish their suitability for every dataset. Performance can change under distribution shift, and model inputs may differ substantially from the data used during pretraining. Evaluation must therefore distinguish the reusable implementation from the empirical evidence supporting a particular use.
Hardware execution, compilation, and distribution
Keras inherits much of its hardware support from its backend. Models can execute on central processors, graphics processing units, and, in supported environments, tensor processing units. Device availability depends on backend builds, drivers, accelerator libraries, and supported operations. Selecting a GPU-capable backend does not guarantee that every operation runs on the GPU; unsupported operations, transfers between devices, and input-pipeline bottlenecks can affect overall execution.
Compilation can transform groups of tensor operations into more efficient executable code. Keras exposes options such as jit_compile that request just-in-time compilation where supported. The result depends on the backend and the model: compilation may fuse operations, reduce interpreter overhead, or improve memory scheduling, but it also introduces tracing and compilation costs. Models containing unsupported operations or highly variable shapes may require different settings. A successful eager-mode program is not necessarily compatible with every compiled execution mode.
Mixed-precision computation uses different numerical formats for different parts of a model. Keras dtype policies can allow computations in lower-precision formats while retaining selected state or calculations in higher precision. On suitable hardware this can improve throughput and reduce memory use. However, smaller numerical ranges can cause overflow or underflow, and some configurations use loss scaling to preserve useful gradient values. The performance benefit depends on the accelerator, tensor dimensions, and balance between arithmetic and memory traffic.
Training across multiple accelerators introduces distributed computing concerns. In data parallelism, replicas process different batches or batch portions, and gradients are combined before updates. Model parallelism instead divides parts of the model or its tensors across devices. TensorFlow-based Keras can use TensorFlow distribution strategies; Keras 3 also provides distribution abstractions, with availability varying by backend and release. There is no single distribution recipe that is automatically optimal across all supported environments.
Meaningful performance comparisons separate compilation, warm-up, input loading, and steady-state computation. Throughput also depends on batch size, sequence length, numerical precision, and whether preprocessing is included. Increasing the global batch size during distributed training can change optimization behavior rather than merely accelerate the same experiment. Keras simplifies access to execution features, but it does not eliminate the need to reason about memory capacity, communication overhead, and the mathematical consequences of changing the training configuration.
Serialization, export, and deployment
Saving a model can serve several different purposes: resuming training, preserving a reusable Python-level model, transferring weights, or producing an inference artifact for another runtime. Keras distinguishes these purposes rather than treating all output files as equivalent. The native Keras 3 format uses the .keras extension and stores model configuration, parameter values, and optimizer state when applicable. This supports reconstruction and continuation of training, provided the required model components and compatible implementations are available.
Serialization of custom objects requires additional information. A built-in layer has a known identifier and configuration schema; a custom class may need registration and a suitable get_config implementation. The saved artifact does not generally contain an unrestricted copy of all Python source code needed to recreate arbitrary behavior. Models that depend on external resources, unconventional call signatures, or unregistered components may therefore need explicit reconstruction code or additional assets.
Saving only weights creates a narrower artifact. It preserves numerical state but not necessarily the architectural description required to interpret that state. Loading weights requires a compatible model structure and matching expectations about variable organization. This is useful for checkpointing or transferring parameters into separately maintained code, but it can create ambiguity if the corresponding architecture, preprocessing, and version information are not preserved. A weight file alone is not a complete experimental specification.
Export targets inference rather than Python-level reconstruction. Keras 3 provides export facilities, including TensorFlow SavedModel export for supported models and environments. A SavedModel can expose callable inference endpoints to TensorFlow serving tools, but it is not loaded as an ordinary native Keras model through Keras 3’s load_model interface. Keras provides other mechanisms, such as TFSMLayer, for incorporating suitable SavedModel endpoints. Other exchange or deployment paths, including ONNX, depend on available exporters, supported operators, and backend compatibility.
Deployment introduces constraints that training code may not reveal. Dynamic shapes, preprocessing operations, custom layers, and numerical data types can affect conversion to a serving or mobile runtime. Techniques such as quantization may reduce storage or accelerate inference, but they can alter accuracy and require representative evaluation. The deployed system also includes request handling, input validation, monitoring, and version management outside Keras itself. A successfully exported artifact establishes technical executability, not end-to-end reliability.
Reproducibility, limitations, and place in the ecosystem
Keras occupies the high-level end of the machine-learning software stack without restricting users to a fixed catalog of models. It can express standard classifiers with little code and also accommodate custom layers, losses, and training procedures. This combination is particularly useful in teaching, prototyping, and repeated experimentation. Its abstractions reduce routine implementation work, but they can also conceal decisions about loss reduction, masking, state updates, and execution mode until those details become relevant.
Reproducibility requires more than setting one random seed. Keras provides utilities for coordinating seeds, yet results can still depend on backend versions, accelerator kernels, operation ordering, and nondeterministic execution. Pseudorandom number generators differ across frameworks, so identical seed values do not imply identical random sequences across backends. Reproducing a training run can require recording software versions, numerical precision, dataset order, preprocessing state, optimizer settings, and hardware-related determinism options.
The library also does not replace experimental design. A concise training script can still contain label leakage, an unrepresentative test split, or an inappropriate metric. Hyperparameter optimization can overfit validation measurements, and a technically correct implementation can fail to generalize beyond its training distribution. Keras supplies mechanisms for fitting and measuring models; claims about generalization depend on the data and evaluation procedure rather than the interface used to build the network.
Its relationship to lower-level frameworks is therefore cooperative as well as comparative. A developer using Keras on PyTorch still relies on PyTorch’s numerical machinery, just as a developer using it on TensorFlow relies on TensorFlow execution. Backend-native code can offer earlier access to specialized operators or more direct control over an unusual algorithm. Keras can provide a consistent model-building vocabulary across those environments, but specialized integrations and custom execution paths may reduce the portability that motivated the common interface.
These boundaries are especially visible during migration. Moving from Keras 2 to Keras 3 can require replacing unsupported symbolic operations, making state creation explicit, updating serialization code, or revising assumptions about nested inputs and outputs. Moving between backends may require further adjustments to custom loops and data pipelines. In such cases, compatibility is best understood as a property of a particular model, its components, and its surrounding workflow—not simply of the Keras name or the fact that two environments implement the same high-level API.