CX Learn by CadetX
Exams My Dashboard
Keras · TensorFlow · Deep Learning
KR-101 · Module 3 Beginner about 35 minutes 8 Lessons Prereq: Modules 1 and 2

Sequential API

The Sequential API is the simplest way to build a model in Keras: you stack layers one after another, like building blocks. In this module you'll master the Dense layer, the most common building block of all, learn to count a model's parameters, read model.summary(), and choose sensible sizes for your networks.

  • Level: Beginner
  • Time: about 35 minutes
  • Needs: Modules 1 and 2

By the end of this module you will be able to

  • Build models with keras.Sequential, using a list or .add()
  • Explain what a Dense layer does and what its units are
  • Choose activation functions for hidden and output layers
  • Count the parameters of any Dense layer by hand
  • Read model.summary() and inspect a layer's weights
  • Match the output layer to the task
  • Choose a sensible number and size of layers

3.1The Sequential API

A Sequential model is a straight stack of layers. Data goes in at the top, passes through each layer in order, and comes out at the bottom. There are two ways to build one, and they create exactly the same model:

two_ways.py
# Way 1: pass a list of layers
model = keras.Sequential([
    keras.Input(shape=(13,)),
    layers.Dense(32, activation="relu"),
    layers.Dense(3, activation="softmax"),
])

# Way 2: start empty and add layers one at a time
model = keras.Sequential()
model.add(keras.Input(shape=(13,)))
model.add(layers.Dense(32, activation="relu"))
model.add(layers.Dense(3, activation="softmax"))
▶ Open Google ColabPaste the code into a new Colab notebook and press Run. Keras is already installed.

The list style is shorter and easier to read. The .add() style is handy when you build layers in a loop. You can also give the model and each layer a name, which makes summaries much easier to read.

When Sequential isn't enough

Sequential models must have exactly one input and one output, with no branches. For anything more complex, such as combining an image and a table of details, you'll use the Functional API in Module 6.

3.2The Dense layer

A Dense layer (also called a fully connected layer) is a group of neurons in which every neuron is connected to every input. The number you give it, like Dense(32), is the number of neurons, which Keras calls units.

A Dense layer with 3 inputs and 4 neurons. Every input connects to every neuron, giving 3 times 4 equals 12 weights, plus one bias per neuron, 4 biases, for 16 parameters in total. The weights form a 3 by 4 grid called the kernel.x1x2x3+b+b+b+b3 inputsDense(4)Weights (kernel): shape (3, 4)wwwwwwwwwwwwBiases: shape (4,)bbbb3 × 4 + 4 = 16 parameters
Figure 1. Each neuron multiplies every input by its own weight, adds them up, adds its bias, then applies the activation function. Keras stores all the weights in one grid called the kernel.
ArgumentWhat it doesExample
unitsNumber of neurons, and so the size of the layer's outputDense(32)
activationThe function applied to each neuron's totalactivation="relu"
nameA readable name for summariesname="hidden_1"
use_biasWhether to include biases (almost always yes)use_bias=True (default)
kernel_initializerHow the starting weights are chosenThe default works well

3.3Activation functions

The activation function decides what each neuron passes on. Without one, a stack of Dense layers can only learn straight-line patterns, no matter how many layers you add. Activations let networks learn curves and complex patterns. Here's what to use:

ActivationOutputWhere to use it
"relu"0 for negatives; unchanged for positivesHidden layers. The default choice: fast and effective
"tanh"−1 to 1Some hidden layers, especially in recurrent networks
"sigmoid"0 to 1Output for yes/no (binary) problems
"softmax"Probabilities that add up to 1Output for choosing one of several classes
None (linear)Any numberOutput for predicting numbers (regression)

A simple rule to remember: relu inside, and let the task choose the output.

3.4Counting parameters

Every weight and bias is a parameter the model learns. For a Dense layer, the count is always:

parameters = inputs × units + units

The first part is the weights (one per connection), and the second is the biases (one per neuron). For example, a Dense(32) layer receiving 13 inputs has 13 × 32 + 32 = 448 parameters. You don't have to calculate this yourself: model.summary() shows it. But knowing where the numbers come from helps you spot mistakes and design sensible models. Build a model below and watch the summary update.

Try it: model stack builder

your_model.py

3.5Matching the output layer to the task

The last layer must match what you're predicting. This is the most important design decision in a Sequential model:

TaskExampleOutput layer
Predict a numberA house priceDense(1)
Yes or noWill this customer leave?Dense(1, activation="sigmoid")
One of several classesWhich of 3 grape varieties?Dense(3, activation="softmax")
Several labels at onceWhich tags fit this photo?Dense(number_of_tags, activation="sigmoid")

Each output needs a matching loss function too, which is the subject of Module 4.

3.6How many layers? How many units?

There's no perfect formula, but these rules of thumb work well for table data:

Rule of thumbWhy
Start small: 1 or 2 hidden layersMost table problems don't need deep networks
Use powers of 2: 16, 32, 64, 128A habit, not a rule, but it keeps choices simple
Funnel shape: get narrower towards the output, e.g. 64 → 32 → 16Each layer summarises the one before
Grow only if neededIf the model can't learn even the training data well, it may be too small
Watch for overfittingBig models on small datasets memorise instead of learning (Module 7)

The Python example below shows this in action: a tiny model underperforms, a sensible one does well, and a model 130 times bigger does no better.

3.7Building and inspecting a model in Python

We'll classify 178 wines into 3 grape varieties using 13 chemical measurements. We'll build a named Sequential model, read its summary, look inside a layer, train it, and then compare three sizes of network.

wine_model.py
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import keras
from keras import layers
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split

# Wine data: 178 wines, 13 chemical measurements, 3 grape varieties
X, y = load_wine(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42, stratify=y)
X_train, X_test = X_train.astype("float32"), X_test.astype("float32")

# ---------- 1. Build a Sequential model ----------
keras.utils.set_random_seed(42)
norm = layers.Normalization(name="scale")
norm.adapt(X_train)

model = keras.Sequential([
    keras.Input(shape=(13,)),
    norm,
    layers.Dense(32, activation="relu", name="hidden_1"),
    layers.Dense(16, activation="relu", name="hidden_2"),
    layers.Dense(3, activation="softmax", name="output"),   # one per variety
], name="wine_classifier")
model.summary(line_length=62)

# ---------- 2. Look inside a layer ----------
weights, biases = model.get_layer("hidden_1").get_weights()
print("hidden_1 weights:", weights.shape, "| biases:", biases.shape)

# ---------- 3. Train and test ----------
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy",
              metrics=["accuracy"])
model.fit(X_train, y_train, epochs=60, batch_size=16, verbose=0)
print(f"Test accuracy: {model.evaluate(X_test, y_test, verbose=0)[1]:.3f}")

probs = model.predict(X_test[:1], verbose=0)[0]
print("First test wine:", [f"{p:.3f}" for p in probs], "-> class", probs.argmax(),
      "| true class", y_test[0])

# ---------- 4. Does bigger mean better? ----------
print()
for hidden in [(2,), (32, 16), (256, 256, 256)]:
    keras.utils.set_random_seed(0)
    m = keras.Sequential([keras.Input(shape=(13,)), norm]
                         + [layers.Dense(u, activation="relu") for u in hidden]
                         + [layers.Dense(3, activation="softmax")])
    m.compile(optimizer="adam", loss="sparse_categorical_crossentropy",
              metrics=["accuracy"])
    m.fit(X_train, y_train, epochs=60, batch_size=16, verbose=0)
    trainable = m.count_params() - 27            # minus the 27 scaling numbers
    print(f"Hidden layers {str(hidden):16} weights={trainable:7,}  "
          f"test accuracy={m.evaluate(X_test, y_test, verbose=0)[1]:.3f}")
Output
Model: "wine_classifier"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Layer (type)             ┃ Output Shape       ┃    Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ scale (Normalization)    │ (None, 13)         │         27 │
├──────────────────────────┼────────────────────┼────────────┤
│ hidden_1 (Dense)         │ (None, 32)         │        448 │
├──────────────────────────┼────────────────────┼────────────┤
│ hidden_2 (Dense)         │ (None, 16)         │        528 │
├──────────────────────────┼────────────────────┼────────────┤
│ output (Dense)           │ (None, 3)          │         51 │
└──────────────────────────┴────────────────────┴────────────┘
 Total params: 1,054 (4.12 KB)
 Trainable params: 1,027 (4.01 KB)
 Non-trainable params: 27 (112.00 B)
hidden_1 weights: (13, 32) | biases: (32,)
Test accuracy: 0.981
First test wine: ['0.994', '0.005', '0.001'] -> class 0 | true class 0

Hidden layers (2,)             weights=     37  test accuracy=0.926
Hidden layers (32, 16)         weights=  1,027  test accuracy=0.981
Hidden layers (256, 256, 256)  weights=135,939  test accuracy=0.981
▶ Open Google ColabPaste the code into a new Colab notebook and press Run. Keras is already installed.

What the output tells us

  • The summary lists each layer, its output shape and its parameters. None is the batch dimension from Module 2: any number of wines.
  • The counts match the formula: hidden_1 has 13 × 32 + 32 = 448, hidden_2 has 32 × 16 + 16 = 528, and the output has 16 × 3 + 3 = 51.
  • The Normalization layer's 27 parameters are "non-trainable": 13 means, 13 variances and a count, all learned by adapt(), not by training.
  • get_weights() returns the kernel with shape (13, 32), one weight per connection, and 32 biases.
  • The model scores 98.1% on unseen wines, and softmax gives probabilities that add up to 1: 99.4% sure the first test wine is variety 0, which is correct.
  • Bigger isn't always better. With just 2 hidden neurons, accuracy drops to 92.6%. But a model with 135,939 weights, 130 times bigger than the sensible one, scores exactly the same 98.1%, while being slower and more likely to overfit.

SummaryKey takeaways

  • keras.Sequential stacks layers in order: use a list, or .add() for loops.
  • A Dense layer connects every input to every neuron; units is the number of neurons.
  • Use relu in hidden layers; choose the output activation by task: none, sigmoid or softmax.
  • Parameters per Dense layer = inputs × units + units.
  • model.summary() shows layers, output shapes and parameter counts; get_weights() shows the actual numbers.
  • Start small, use a funnel shape, and grow only if needed. Bigger isn't always better.

Check your understanding

Your Keras roadmap

  1. 01
    Introduction to KerasWhat Keras is, setup and your first model
  2. 02
    Tensors and Data for KerasShapes, scaling and splitting data
  3. 03
    The Sequential API and Dense LayersYou are here
  4. 04
    Compiling ModelsLosses, optimisers and metrics
  5. 05
    Training and Evaluatingfit(), validation and callbacks
  6. 06
    The Functional APIMultiple inputs, outputs and branches
  7. 07
    RegularisationDropout, batch normalisation and early stopping
  8. 08
    Image Classification with KerasCNNs and data augmentation
  9. 09
    Text and Sequences with KerasEmbeddings, LSTMs and text classification
  10. 10
    Pretrained Models, Saving and DeploymentFine-tuning and sharing your models