Deep Learning with Keras 3
Patterns and best practices based on Deep Learning with Python, 2nd Edition by François Chollet, updated for Keras 3 (Multi-Backend).
Core Workflow
- Prepare Data: Normalize, split train/val/test, create
tf.data.Dataset
- Build Model: Sequential, Functional, or Subclassing API
- Compile:
model.compile(optimizer, loss, metrics)
- Train:
model.fit(data, epochs, validation_data, callbacks)
- Evaluate:
model.evaluate(test_data)
Model Building APIs
Sequential - Simple stack of layers:
model = keras.Sequential([
layers.Dense(64, activation="relu"),
layers.Dense(10, activation="softmax")
])
Functional - Multi-input/output, shared layers, non-linear topologies:
inputs = keras.Input(shape=(64,))
x = layers.Dense(64, activation="relu")(inputs)
outputs = layers.Dense(10, activation="softmax")(x)
model = keras.Model(inputs=inputs, outputs=outputs)
Subclassing - Full flexibility with call() method:
class MyModel(keras.Model):
def __init__(self):
super().__init__()
self.dense1 = layers.Dense(64, activation="relu")
self.dense2 = layers.Dense(10, activation="softmax")
def call(self, inputs):
x = self.dense1(inputs)
return self.dense2(x)
Quick Reference: Loss & Optimizer Selection
| Task |
Loss |
Final Activation |
| Binary classification |
binary_crossentropy |
sigmoid |
| Multiclass (one-hot) |
categorical_crossentropy |
softmax |
| Multiclass (integers) |
sparse_categorical_crossentropy |
softmax |
| Regression |
mse or mae |
None |
Optimizers: rmsprop (default), adam (popular), sgd (with momentum for fine-tuning)
Domain-Specific Guides
| Topic |
Reference |
When to Use |
| Keras 3 Migration |
keras3_changes.md |
START HERE: Multi-backend setup, keras.ops, import keras |
| Fundamentals |
basics.md |
Overfitting, regularization, data prep, K-fold validation |
| Keras Deep Dive |
keras_working.md |
Custom metrics, callbacks, training loops, tf.function |
| Computer Vision |
computer_vision.md |
Convnets, data augmentation, transfer learning |
| Advanced CV |
advanced_cv.md |
Segmentation, ResNets, Xception, Grad-CAM |
| Time Series |
timeseries.md |
RNNs (LSTM/GRU), 1D convnets, forecasting |
| NLP & Transformers |
nlp_transformers.md |
Text processing, embeddings, Transformer encoder/decoder |
| Generative DL |
generative_dl.md |
Text generation, VAEs, GANs, style transfer |
| Best Practices |
best_practices.md |
KerasTuner, mixed precision, multi-GPU, TPU |
Essential Callbacks
callbacks = [
keras.callbacks.EarlyStopping(monitor="val_loss", patience=3),
keras.callbacks.ModelCheckpoint("best.keras", save_best_only=True),
keras.callbacks.TensorBoard(log_dir="./logs")
]
model.fit(..., callbacks=callbacks)
Utility Scripts
| Script |
Description |
| quick_train.py |
Reusable training template with standard callbacks and history plotting |
| visualize_filters.py |
Visualize convnet filter patterns via gradient ascent |
1---2name: deep-learning3description: Comprehensive guide for Deep Learning with Keras 3 (Multi-Backend: JAX, TensorFlow, PyTorch). Use when building neural networks, CNNs for computer vision, RNNs/Transformers for NLP, time series forecasting, or generative models (VAEs, GANs). Covers model building (Sequential/Functional/Subclassing APIs), custom training loops, data augmentation, transfer learning, and production best practices.4---56# Deep Learning with Keras 37 8Patterns and best practices based on *Deep Learning with Python, 2nd Edition* by François Chollet, updated for Keras 3 (Multi-Backend).910## Core Workflow11121. **Prepare Data**: Normalize, split train/val/test, create `tf.data.Dataset`132. **Build Model**: Sequential, Functional, or Subclassing API143. **Compile**: `model.compile(optimizer, loss, metrics)`154. **Train**: `model.fit(data, epochs, validation_data, callbacks)`165. **Evaluate**: `model.evaluate(test_data)`1718## Model Building APIs1920**Sequential** - Simple stack of layers:21```python22model = keras.Sequential([23 layers.Dense(64, activation="relu"),24 layers.Dense(10, activation="softmax")25])26```2728**Functional** - Multi-input/output, shared layers, non-linear topologies:29```python30inputs = keras.Input(shape=(64,))31x = layers.Dense(64, activation="relu")(inputs)32outputs = layers.Dense(10, activation="softmax")(x)33model = keras.Model(inputs=inputs, outputs=outputs)34```3536**Subclassing** - Full flexibility with `call()` method:37```python38class MyModel(keras.Model):39 def __init__(self):40 super().__init__()41 self.dense1 = layers.Dense(64, activation="relu")42 self.dense2 = layers.Dense(10, activation="softmax")4344 def call(self, inputs):45 x = self.dense1(inputs)46 return self.dense2(x)47```4849## Quick Reference: Loss & Optimizer Selection5051| Task | Loss | Final Activation |52|------|------|------------------|53| Binary classification | `binary_crossentropy` | `sigmoid` |54| Multiclass (one-hot) | `categorical_crossentropy` | `softmax` |55| Multiclass (integers) | `sparse_categorical_crossentropy` | `softmax` |56| Regression | `mse` or `mae` | None |5758**Optimizers**: `rmsprop` (default), `adam` (popular), `sgd` (with momentum for fine-tuning)5960## Domain-Specific Guides6162| Topic | Reference | When to Use |63|-------|-----------|-------------|64| **Keras 3 Migration** | [keras3_changes.md](references/keras3_changes.md) | **START HERE**: Multi-backend setup, `keras.ops`, `import keras` |65| **Fundamentals** | [basics.md](references/basics.md) | Overfitting, regularization, data prep, K-fold validation |66| **Keras Deep Dive** | [keras_working.md](references/keras_working.md) | Custom metrics, callbacks, training loops, `tf.function` |67| **Computer Vision** | [computer_vision.md](references/computer_vision.md) | Convnets, data augmentation, transfer learning |68| **Advanced CV** | [advanced_cv.md](references/advanced_cv.md) | Segmentation, ResNets, Xception, Grad-CAM |69| **Time Series** | [timeseries.md](references/timeseries.md) | RNNs (LSTM/GRU), 1D convnets, forecasting |70| **NLP & Transformers** | [nlp_transformers.md](references/nlp_transformers.md) | Text processing, embeddings, Transformer encoder/decoder |71| **Generative DL** | [generative_dl.md](references/generative_dl.md) | Text generation, VAEs, GANs, style transfer |72| **Best Practices** | [best_practices.md](references/best_practices.md) | KerasTuner, mixed precision, multi-GPU, TPU |7374## Essential Callbacks7576```python77callbacks = [78 keras.callbacks.EarlyStopping(monitor="val_loss", patience=3),79 keras.callbacks.ModelCheckpoint("best.keras", save_best_only=True),80 keras.callbacks.TensorBoard(log_dir="./logs")81]82model.fit(..., callbacks=callbacks)83```8485## Utility Scripts8687| Script | Description |88|--------|-------------|89| [quick_train.py](scripts/quick_train.py) | Reusable training template with standard callbacks and history plotting |90| [visualize_filters.py](scripts/visualize_filters.py) | Visualize convnet filter patterns via gradient ascent |