Keras
Overview
Keras is a deep learning API written in Python, focused on enabling fast experimentation with a clean, layered model-building interface. Keras 3 is multi-backend: the same model code runs on JAX, TensorFlow, or PyTorch, selected via the KERAS_BACKEND environment variable. Read this when building/training neural networks at a high level, choosing a backend, or saving and loading models.
Official sources
Install / setup
pip install --upgrade keras
# then install a backend (one of), e.g.:
pip install tensorflow # or: pip install jax / pip install torch
export KERAS_BACKEND="jax"
Commands from the official getting-started guide: https://keras.io/getting_started/
Core concepts
- Backend — JAX, TensorFlow, or PyTorch; set
KERAS_BACKEND (or ~/.keras/keras.json) before import keras; cannot change after import.
- Sequential API — linear stack of layers for simple feed-forward models.
- Functional API — build arbitrary DAGs of layers via tensor calls; the recommended general-purpose API.
- Subclassing — subclass
keras.Model/keras.layers.Layer and implement call() for full control.
- compile / fit / evaluate / predict — declare optimizer, loss, and metrics, then train and run inference.
- Callbacks — hooks like
ModelCheckpoint, EarlyStopping, TensorBoard during training.
- .keras format — the modern single-file model serialization format (
model.save("m.keras")).
Best practices
Common pitfalls
- Importing Keras before setting
KERAS_BACKEND → backend locks to the default; set the env var first.
- Confusing standalone
keras (Keras 3) with tf.keras bundled in TensorFlow → pick one import path consistently.
- Loading legacy
.h5/SavedModel into Keras 3 without matching custom objects → register them or re-export to .keras.
Examples
import os
os.environ["KERAS_BACKEND"] = "jax" # before importing keras
import keras
from keras import layers
model = keras.Sequential([
layers.Dense(128, activation="relu"),
layers.Dense(10, activation="softmax"),
])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
# model.fit(x_train, y_train, epochs=3, validation_split=0.1)
model.save("model.keras")
Further reading
Related skills
- ../tensorflow — a Keras backend;
tf.keras ships with TensorFlow
- ../pytorch — a supported Keras 3 backend
- ../numpy — array inputs to Keras models
1---2name: keras3description: Keras is a high-level, multi-backend deep learning API (Keras 3) that runs on JAX, TensorFlow, or PyTorch, offering Sequential/Functional/subclassed models with a clean compile/fit/evaluate workflow. Consult this when building neural networks quickly, defining layers and custom models, configuring optimizers/losses/metrics and callbacks, choosing or switching the KERAS_BACKEND, saving/loading .keras models, or porting code across JAX/TF/PyTorch backends.4domain: stack5category: aiml6tags: [keras, deep-learning, neural-networks, tensorflow, jax, pytorch, python]7official_sources:8 - https://keras.io/9 - https://github.com/keras-team/keras10 - https://keras.io/getting_started/11verified: 2026-06-1712metadata:13 source: https://skillmd.com/skills/gabriel7silva/keras14---1516# Keras1718## Overview19Keras is a deep learning API written in Python, focused on enabling fast experimentation with a clean, layered model-building interface. Keras 3 is multi-backend: the same model code runs on JAX, TensorFlow, or PyTorch, selected via the `KERAS_BACKEND` environment variable. Read this when building/training neural networks at a high level, choosing a backend, or saving and loading models.2021## Official sources22- Docs: https://keras.io/23- Repo: https://github.com/keras-team/keras24- Install: https://keras.io/getting_started/2526## Install / setup27```bash28pip install --upgrade keras29# then install a backend (one of), e.g.:30pip install tensorflow # or: pip install jax / pip install torch31export KERAS_BACKEND="jax"32```33Commands from the official getting-started guide: https://keras.io/getting_started/3435## Core concepts36- **Backend** — JAX, TensorFlow, or PyTorch; set `KERAS_BACKEND` (or `~/.keras/keras.json`) before `import keras`; cannot change after import.37- **Sequential API** — linear stack of layers for simple feed-forward models.38- **Functional API** — build arbitrary DAGs of layers via tensor calls; the recommended general-purpose API.39- **Subclassing** — subclass `keras.Model`/`keras.layers.Layer` and implement `call()` for full control.40- **compile / fit / evaluate / predict** — declare optimizer, loss, and metrics, then train and run inference.41- **Callbacks** — hooks like `ModelCheckpoint`, `EarlyStopping`, `TensorBoard` during training.42- **.keras format** — the modern single-file model serialization format (`model.save("m.keras")`).4344## Best practices45- Set `KERAS_BACKEND` before importing Keras; it cannot be switched afterward (https://keras.io/getting_started/).46- Prefer the Functional API for multi-input/output or branching models over deep subclassing (https://keras.io/guides/functional_api/).47- Use `EarlyStopping` + `ModelCheckpoint` callbacks rather than fixed epoch counts (https://keras.io/api/callbacks/).48- Save in the native `.keras` format for full architecture+weights portability (https://keras.io/guides/serialization_and_saving/).4950## Common pitfalls51- Importing Keras before setting `KERAS_BACKEND` → backend locks to the default; set the env var first.52- Confusing standalone `keras` (Keras 3) with `tf.keras` bundled in TensorFlow → pick one import path consistently.53- Loading legacy `.h5`/SavedModel into Keras 3 without matching custom objects → register them or re-export to `.keras`.5455## Examples56```python57import os58os.environ["KERAS_BACKEND"] = "jax" # before importing keras59import keras60from keras import layers6162model = keras.Sequential([63 layers.Dense(128, activation="relu"),64 layers.Dense(10, activation="softmax"),65])66model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])67# model.fit(x_train, y_train, epochs=3, validation_split=0.1)68model.save("model.keras")69```7071## Further reading72- https://keras.io/guides/ — developer guides (Functional API, custom training, serialization)73- https://keras.io/api/ — full layers/models/callbacks API reference74- https://keras.io/examples/ — code examples across vision, NLP, and more7576## Related skills77- ../tensorflow — a Keras backend; `tf.keras` ships with TensorFlow78- ../pytorch — a supported Keras 3 backend79- ../numpy — array inputs to Keras models