The Sequential API is Keras's simplest model-building interface โ a linear stack of layers, each feeding directly into the next, directly paralleling PyTorch's nn.Sequential from PyTorch Layers.
Building a Model
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential([
layers.Input(shape=(784,)),
layers.Dense(128, activation='relu'),
layers.Dropout(0.3),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
model.summary() # prints the full architecture, layer shapes, and parameter counts
Alternative: Adding Layers Incrementally
model = keras.Sequential()
model.add(layers.Dense(128, activation='relu', input_shape=(784,)))
model.add(layers.Dropout(0.3))
model.add(layers.Dense(10, activation='softmax'))
A CNN Example
model = keras.Sequential([
layers.Input(shape=(32, 32, 3)),
layers.Conv2D(32, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
Notice Keras's default input shape convention is channel-last, (height, width, channels) โ the opposite of PyTorch's default channel-first (channels, height, width) convention flagged in Tensors. This is a genuinely common source of shape-mismatch confusion when porting code or intuitions between the two frameworks.
Common Mistakes
- Forgetting to specify the input shape (via an explicit
Inputlayer orinput_shapeargument on the first layer) โ without it, Keras can't build the model's weight shapes until it first sees actual data, which can cause confusing downstream errors. - Assuming the same channel ordering as PyTorch โ Keras/TensorFlow defaults to channel-last (
H, W, C), unlike PyTorch's channel-first (C, H, W) default.
Interview Relevance
Q: "What's a common shape-related mistake when porting a CNN architecture from PyTorch to Keras (or vice versa)?" Forgetting the difference in default channel ordering โ PyTorch defaults to channel-first (C, H, W), while TensorFlow/Keras defaults to channel-last (H, W, C). Directly reusing shape assumptions from one framework in the other without adjusting for this produces shape-mismatch errors or silently incorrect results.
Practice Question
What would the input_shape argument be for a Keras Sequential model processing 28ร28 grayscale images?