Keras is Python's high-level neural network API. In Keras 3, it provides a unified multi-backend engine that runs seamlessly on top of TensorFlow, PyTorch, or JAX (configured via os.environ["KERAS_BACKEND"] = "torch" prior to importing).
The canonical Keras workflow:
$$\text{Build Architecture} \longrightarrow \text{Compile Objective} \longrightarrow \text{Fit & Train} \longrightarrow \text{Predict & Evaluate}$$
🔗 Related Cheat Sheets: NumPy Cheat Sheet · Pandas Cheat Sheet · Scikit-Learn Cheat Sheet · Supervised Learning Algorithms · Core AI Algorithms · Python Cheat Sheet
1. End-to-End Quickstart Example
The following self-contained script builds, compiles, trains, and evaluates a binary classification network:
import numpy as np
import keras
from keras import layers, callbacks
# 1. Synthesize binary classification data
X_train = np.random.random((1000, 20))
y_train = np.random.randint(2, size=(1000, 1))
X_val = np.random.random((200, 20))
y_val = np.random.randint(2, size=(200, 1))
# 2. Build Sequential network topology
model = keras.Sequential([
layers.Input(shape=(20,)),
layers.Dense(64, activation='relu'),
layers.Dropout(0.3),
layers.Dense(32, activation='relu'),
layers.Dense(1, activation='sigmoid')
])
# 3. Compile objective (always use learning_rate parameter)
model.compile(
optimizer=keras.optimizers.Adam(learning_rate=1e-3),
loss='binary_crossentropy',
metrics=['accuracy', keras.metrics.AUC(name='auc')]
)
# 4. Fit with EarlyStopping
early_stop = callbacks.EarlyStopping(monitor='val_loss', patience=3, restore_best_weights=True)
history = model.fit(
X_train, y_train,
epochs=20,
batch_size=32,
validation_data=(X_val, y_val),
callbacks=[early_stop],
verbose=1
)
# 5. Predict probabilities and discrete classes
y_prob = model.predict(X_val)
y_pred = (y_prob > 0.5).astype("int32")
print("Validation Accuracy:", model.evaluate(X_val, y_val, verbose=0)[1])
2. Core Architecture Design Patterns
2.1 Sequential Model (Linear Stacks)
Best for simple feedforward networks without branching:
from keras import Sequential, layers
model = Sequential([
layers.Input(shape=(784,)),
layers.Dense(256, activation='relu'),
layers.BatchNormalization(),
layers.Dropout(0.2),
layers.Dense(10, activation='softmax')
])
2.2 Functional API (Industrial Standard)
Required for residual skip-connections (ResNets), multi-input / multi-output networks, and shared weights:
from keras import layers, Model
inputs = layers.Input(shape=(64, 64, 3), name="image_input")
# Convolutional backbone with skip connection
x = layers.Conv2D(32, (3, 3), padding='same', activation='relu')(inputs)
residual = x
x = layers.Conv2D(32, (3, 3), padding='same', activation='relu')(x)
x = layers.add([x, residual]) # Residual Add
x = layers.MaxPooling2D((2, 2))(x)
x = layers.GlobalAveragePooling2D()(x)
# Parallel multi-task prediction heads
category_out = layers.Dense(10, activation='softmax', name="category")(x)
regression_out = layers.Dense(1, name="price")(x)
model = Model(inputs=inputs, outputs=[category_out, regression_out], name="multi_task_net")
model.summary()
3. Core Neural Network Layers (keras.layers)
| Layer Category | Class Name | Constructor Example | Notes & Parameters |
|---|---|---|---|
| Dense | Dense |
layers.Dense(128, activation='relu') |
Fully connected layer; kernel_regularizer |
| Regularization | Dropout |
layers.Dropout(0.3) |
Randomly sets inputs to 0 during training |
| Normalization | BatchNormalization |
layers.BatchNormalization() |
Accelerates convergence across mini-batches |
| Normalization | LayerNormalization |
layers.LayerNormalization() |
Standard in RNNs and Transformers |
| 2D Convolution | Conv2D |
layers.Conv2D(64, (3, 3), padding='same', activation='relu') |
Spatial feature extraction; strides=(1, 1) |
| 2D Pooling | MaxPooling2D |
layers.MaxPooling2D(pool_size=(2, 2)) |
Spatial downsampling |
| Global Pooling | GlobalAveragePooling2D |
layers.GlobalAveragePooling2D() |
Averages feature maps; replaces heavy Flatten |
| Embedding | Embedding |
layers.Embedding(input_dim=10000, output_dim=128) |
Maps integer token IDs to dense vectors |
| Recurrent | LSTM / GRU |
layers.LSTM(64, return_sequences=True) |
Sequence modeling; recurrent_dropout=0.1 |
| Bidirectional | Bidirectional |
layers.Bidirectional(layers.GRU(64)) |
Forward and backward context concatenation |
4. Preprocessing & Utilities (keras.utils)
from keras import utils
# 1. Sequence padding and truncation (NLP)
padded = utils.pad_sequences(
sequences,
maxlen=100,
padding='post', # Append zeros at the end
truncating='post' # Truncate values from the end
)
# 2. Categorical One-Hot Encoding
y_one_hot = utils.to_categorical(y_int_labels, num_classes=10)
# 3. Directory-based image dataset loader
train_ds = utils.image_dataset_from_directory(
"dataset/train/",
labels='inferred',
image_size=(224, 224),
batch_size=32,
shuffle=True
)
5. Model Compilation Matrix
| Task Type | Loss Function (loss) |
Output Activation | Evaluation Metrics |
|---|---|---|---|
| Binary Classification | 'binary_crossentropy' |
sigmoid (1 neuron) |
['accuracy', keras.metrics.AUC()] |
| Multi-class (One-Hot) | 'categorical_crossentropy' |
softmax ($K$ neurons) |
['accuracy', 'categorical_accuracy'] |
| Multi-class (Integer IDs) | 'sparse_categorical_crossentropy' |
softmax ($K$ neurons) |
['accuracy'] |
| Continuous Regression | 'mse' (Mean Squared Error) / 'huber' |
Linear (no activation) | ['mae', keras.metrics.RootMeanSquaredError()] |
Optimizer Declarations
⚠️ Note: Always use
learning_rateinstead of the deprecatedlrshorthand.
from keras import optimizers
# 1. Adam & AdamW with weight decay
opt_adam = optimizers.Adam(learning_rate=1e-3, beta_1=0.9, beta_2=0.999)
opt_adamw = optimizers.AdamW(learning_rate=1e-3, weight_decay=1e-4)
# 2. SGD with Nesterov momentum
opt_sgd = optimizers.SGD(learning_rate=1e-2, momentum=0.9, nesterov=True)
# 3. Exponential learning rate decay schedule
lr_schedule = optimizers.schedules.ExponentialDecay(
initial_learning_rate=1e-2,
decay_steps=10000,
decay_rate=0.9
)
opt_decayed = optimizers.Adam(learning_rate=lr_schedule)
6. Model Inference & Evaluation
⚠️ Note:
model.predict_classes()was removed in Keras 2.6+. Use modern NumPy argmax / thresholding:
# 1. Raw continuous predictions
raw_preds = model.predict(X_test, batch_size=64)
# 2. Binary hard labels
binary_labels = (raw_preds > 0.5).astype("int32")
# 3. Multi-class class indices
multi_labels = np.argmax(raw_preds, axis=1)
# 4. Benchmark evaluation
test_loss, test_acc = model.evaluate(X_test, y_test, batch_size=64)
7. Lifecycle Callbacks
from keras import callbacks
my_callbacks = [
# 1. Early Stopping: abort if val_loss stagnates for 4 epochs, restore best weights
callbacks.EarlyStopping(
monitor='val_loss',
patience=4,
restore_best_weights=True,
verbose=1
),
# 2. Model Checkpoint: save only the best model in native .keras format
callbacks.ModelCheckpoint(
filepath='best_model.keras',
monitor='val_accuracy',
save_best_only=True,
verbose=1
),
# 3. Reduce LR On Plateau: halve learning rate when loss plateaus for 2 epochs
callbacks.ReduceLROnPlateau(
monitor='val_loss',
factor=0.5,
patience=2,
min_lr=1e-6,
verbose=1
),
# 4. TensorBoard logs
callbacks.TensorBoard(log_dir='./logs', histogram_freq=1)
]
model.fit(X_train, y_train, epochs=50, callbacks=my_callbacks)
8. Model Persistence (Saving & Loading)
| Operation | Canonical Keras Syntax | Purpose & Description |
|---|---|---|
| Save Full Model | model.save("model.keras") |
Keras 3 recommended format: stores architecture, weights, optimizer state, and compilation metadata |
| Load Full Model | loaded = keras.models.load_model("model.keras") |
Restores model for immediate inference or resumed training |
| Save Weights Only | model.save_weights("weights.weights.h5") |
Saves tensor weights without network architecture |
| Load Weights Only | model.load_weights("weights.weights.h5") |
Injects weights into an identical empty topology |
| Export Config | config = model.get_config() |
Exports architecture as a Python dictionary |
| Summary Inspection | model.summary() |
Inspects layer output shapes and parameter counts |