CS5720 - Week 9
Slide 168 of 180

Keras Sequential Model

What is Sequential Model?

The Sequential model is Keras' simplest way to build neural networks. It creates a linear stack of layers where each layer has exactly one input and one output.
Perfect for beginners:

Simple to understand - layers stacked one after another
Easy to build - just add layers sequentially
Most common use case - feedforward networks
Great starting point - covers 90% of architectures
💡 Key Insight:
Think of Sequential as building blocks - each layer's output becomes the next layer's input, like a chain.

Common Layer Types

🧠 Dense (Fully Connected)
Each neuron connects to all neurons in previous layer
🖼️ Conv2D (Convolutional)
Applies filters to detect features in images
⬇️ MaxPooling2D
Reduces spatial dimensions, keeps important features
🎲 Dropout
Randomly turns off neurons to prevent overfitting
📏 Flatten
Converts 2D feature maps to 1D for Dense layers

Building Sequential Models

import tensorflow as tf from tensorflow.keras import Sequential from tensorflow.keras.layers import Dense, Dropout # Method 1: Add layers one by one model = Sequential() model.add(Dense(128, activation='relu', input_shape=(784,))) model.add(Dropout(0.2)) model.add(Dense(64, activation='relu')) model.add(Dense(10, activation='softmax')) # Method 2: Pass list of layers model = Sequential([ Dense(128, activation='relu', input_shape=(784,)), Dropout(0.2), Dense(64, activation='relu'), Dense(10, activation='softmax') ]) # Compile the model model.compile( optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'] ) # View model architecture model.summary()
Output:
Click "Run" to see the model summary and architecture details.
Prepared by Dr. Gorkem Kar