iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Sequential

keras.Sequential is the “layers in a line” API — the fastest way to define a model. Works for MLPs, simple CNNs/RNNs, anything where each layer feeds the next without branching.

Build, compile, train, evaluate

EXAMPLE
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, callbacks

# 1) Build — pass a list of layers
model = keras.Sequential([
    layers.Input(shape=(20,)),                # explicit input shape
    layers.Dense(64, activation='relu'),
    layers.Dropout(0.2),
    layers.Dense(32, activation='relu'),
    layers.Dense(1,  activation='sigmoid'),
])
model.summary()

# 2) Or .add()
m2 = keras.Sequential()
m2.add(layers.Input(shape=(28, 28, 1)))
m2.add(layers.Conv2D(32, 3, activation='relu'))
m2.add(layers.MaxPool2D())
m2.add(layers.Flatten())
m2.add(layers.Dense(10, activation='softmax'))

# 3) Compile — wire up optimiser / loss / metrics
model.compile(
    optimizer=keras.optimizers.AdamW(learning_rate=1e-3, weight_decay=1e-2),
    loss='binary_crossentropy',
    metrics=['accuracy', keras.metrics.AUC(name='auc')],
)

# 4) Fit
history = model.fit(
    X_train, y_train,
    validation_split=0.2,
    epochs=30,
    batch_size=64,
    callbacks=[
        callbacks.EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True),
        callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3),
        callbacks.ModelCheckpoint('best.keras', save_best_only=True, monitor='val_auc'),
        callbacks.TensorBoard('runs/exp1'),
    ],
    verbose=2,
)

# 5) Evaluate + predict
results = model.evaluate(X_test, y_test, return_dict=True)
print(results)

probs = model.predict(X_test)
classes = (probs > 0.5).astype(int).flatten()

# 6) tf.data input pipeline — for big datasets
ds_tr = (tf.data.Dataset.from_tensor_slices((X_train, y_train))
         .shuffle(10_000)
         .batch(64)
         .prefetch(tf.data.AUTOTUNE))
ds_va = tf.data.Dataset.from_tensor_slices((X_val, y_val)).batch(64).prefetch(tf.data.AUTOTUNE)

model.fit(ds_tr, validation_data=ds_va, epochs=30)

# 7) Save + load
model.save('classifier.keras')
reloaded = keras.models.load_model('classifier.keras')
# .keras = single-file format (recommended); legacy: .h5 or SavedModel directory

# 8) Common patterns

# 8a) MLP for tabular
mlp = keras.Sequential([
    layers.Input((n_features,)),
    layers.Normalization(),                  # learns mean/std from .adapt(X_train)
    layers.Dense(128, activation='relu'),
    layers.BatchNormalization(),
    layers.Dropout(0.3),
    layers.Dense(64, activation='relu'),
    layers.Dropout(0.3),
    layers.Dense(num_classes, activation='softmax'),
])

# 8b) Small CNN for images
cnn = keras.Sequential([
    layers.Input((32, 32, 3)),
    layers.Rescaling(1./255),
    layers.Conv2D(32, 3, padding='same', activation='relu'),
    layers.MaxPool2D(),
    layers.Conv2D(64, 3, padding='same', activation='relu'),
    layers.MaxPool2D(),
    layers.Conv2D(128, 3, padding='same', activation='relu'),
    layers.GlobalAveragePooling2D(),
    layers.Dense(num_classes, activation='softmax'),
])

# 8c) Simple RNN/LSTM/GRU for sequences
rnn = keras.Sequential([
    layers.Input((max_len,)),
    layers.Embedding(vocab_size, 64),
    layers.Bidirectional(layers.LSTM(64, return_sequences=False)),
    layers.Dense(num_classes, activation='softmax'),
])

# 9) Sequential limits
# Sequential = ONE input, ONE output, linear stack. Use the Functional API for:
#   - Multiple inputs / outputs
#   - Branching / skip connections / multi-task
#   - Custom training step (subclass keras.Model)
# Functional example:
#   inputs = keras.Input((28, 28, 1))
#   x = layers.Conv2D(32, 3)(inputs)
#   x = layers.MaxPool2D()(x)
#   outputs = layers.Dense(10)(layers.Flatten()(x))
#   model = keras.Model(inputs, outputs)

# 10) Performance tips
# - Use a real tf.data pipeline once dataset > ~100k rows
# - Use mixed precision: tf.keras.mixed_precision.set_global_policy('mixed_float16')
# - Use early stopping + LR scheduling — better than picking epochs blindly
# - Profile with TensorBoard's Profiler if training is slow

Why it matters

Sequential covers 70% of nets. Switch to Keras Functional (or a subclassed keras.Model) the moment you need branches, multiple inputs, or skip connections — not by default.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
import tensorflow as tf
from tensorflow.keras import layers, Sequential
model = Sequential([
    layers.Dense(64, activation='relu', input_shape=(784,)),
    layers.Dense(10, activation='softmax'),
])
Try it Yourself »

Exercise

Stack layers in order.

model = Sequential([ layers. (64, activation='relu'), ])

Test yourself

Q1. Sequential is best for…
Q2. For multi-input or shared layers, use…
Q3. Compile is needed before…

Discussion

Loading…