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:
# 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"))
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.
| Argument | What it does | Example |
|---|---|---|
units | Number of neurons, and so the size of the layer's output | Dense(32) |
activation | The function applied to each neuron's total | activation="relu" |
name | A readable name for summaries | name="hidden_1" |
use_bias | Whether to include biases (almost always yes) | use_bias=True (default) |
kernel_initializer | How the starting weights are chosen | The 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:
| Activation | Output | Where to use it |
|---|---|---|
"relu" | 0 for negatives; unchanged for positives | Hidden layers. The default choice: fast and effective |
"tanh" | −1 to 1 | Some hidden layers, especially in recurrent networks |
"sigmoid" | 0 to 1 | Output for yes/no (binary) problems |
"softmax" | Probabilities that add up to 1 | Output for choosing one of several classes |
| None (linear) | Any number | Output 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:
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
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:
| Task | Example | Output layer |
|---|---|---|
| Predict a number | A house price | Dense(1) |
| Yes or no | Will this customer leave? | Dense(1, activation="sigmoid") |
| One of several classes | Which of 3 grape varieties? | Dense(3, activation="softmax") |
| Several labels at once | Which 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 thumb | Why |
|---|---|
| Start small: 1 or 2 hidden layers | Most table problems don't need deep networks |
| Use powers of 2: 16, 32, 64, 128 | A habit, not a rule, but it keeps choices simple |
| Funnel shape: get narrower towards the output, e.g. 64 → 32 → 16 | Each layer summarises the one before |
| Grow only if needed | If the model can't learn even the training data well, it may be too small |
| Watch for overfitting | Big 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.
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}")
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
What the output tells us
- The summary lists each layer, its output shape and its parameters.
Noneis 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.Sequentialstacks 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
- 01Introduction to KerasWhat Keras is, setup and your first model
- 02Tensors and Data for KerasShapes, scaling and splitting data
- 03The Sequential API and Dense LayersYou are here
- 04Compiling ModelsLosses, optimisers and metrics
- 05Training and Evaluatingfit(), validation and callbacks
- 06The Functional APIMultiple inputs, outputs and branches
- 07RegularisationDropout, batch normalisation and early stopping
- 08Image Classification with KerasCNNs and data augmentation
- 09Text and Sequences with KerasEmbeddings, LSTMs and text classification
- 10Pretrained Models, Saving and DeploymentFine-tuning and sharing your models
Next module
Compiling Models