Keras is TensorFlow's high-level API for building and training models โ the layer of abstraction most people actually interact with day to day, sitting on top of the lower-level tensor/GradientTape mechanics from the previous two notes.
The Three Ways to Build a Keras Model
| API | Best For | Covered In |
|---|---|---|
| Sequential | Simple, linear stacks of layers | Keras Sequential API |
| Functional | Architectures with branching, multiple inputs/outputs, skip connections | Keras Functional API |
| Subclassing | Full custom control over the forward computation, similar to PyTorch's nn.Module | Keras Custom Models |
This is a direct parallel to the PyTorch spectrum from PyTorch Layers and nn.Module โ Keras's Sequential API maps to PyTorch's nn.Sequential, while Keras's subclassing API maps closely to a custom nn.Module.
The High-Level Workflow, Previewed
import tensorflow as tf
from tensorflow import keras
model = keras.Sequential([
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, validation_data=(x_val, y_val))
Notice how much of the manual bookkeeping from PyTorch's training loop (PyTorch Training Loop) โ the zero_grad/forward/backward/step cycle, epoch iteration, metric tracking โ is handled internally by just .compile() and .fit(). This higher level of abstraction is Keras's defining characteristic, covered fully in Keras Training.
Common Mistakes
- Assuming "TensorFlow" and "Keras" are two separate, competing frameworks to choose between โ since TensorFlow 2.x, Keras is TensorFlow's official high-level API, built directly into it (
tf.keras), not a separate library requiring a choice. - Defaulting to the Sequential API for an architecture that genuinely needs branching or multiple inputs โ this forces awkward workarounds; the Functional API is the correct tool.
Interview Relevance
Q: "What's the relationship between TensorFlow and Keras today?" Since TensorFlow 2.x, Keras is TensorFlow's official, built-in high-level API (tf.keras) โ not a separate framework. It provides Sequential, Functional, and Subclassing APIs for building models at different levels of flexibility, all running on top of TensorFlow's core tensor operations and automatic differentiation (GradientTape) underneath.
Practice Question
Which Keras API would you choose for a model needing two separate input branches (e.g. an image and a text description) that get combined partway through the network?