Expert: deep learning and AI · Neural networks and deep learning · topic 44 of 62 · 35 min
Layers, activations and the forward pass
Explanation
From one neuron to a network
Put many neurons side by side and you get a layer. Feed one layer's outputs into the next and you get a network. A dense (fully connected) layer connects every input to every neuron in the layer. "Deep" learning simply means networks with many hidden layers between the input and the output.
A dense layer as maths
If a layer has 3 inputs and 2 neurons, it has a 2 × 3 weight matrix (one row of 3 weights per neuron) and 2 biases. Its output is: for each neuron, the weighted sum of the inputs plus that neuron's bias, then the activation. In matrix form: output = activation(W·x + b). GPUs are very good at exactly this kind of matrix maths.
x = [1.0, 2.0, 3.0]
W = [[0.2, 0.0, -0.1], # neuron 1
[0.5, -0.5, 0.25]] # neuron 2
b = [0.1, 0.0]
z = [sum(w * xi for w, xi in zip(row, x)) + bj for row, bj in zip(W, b)]
relu = [max(0.0, v) for v in z]
print([round(v, 2) for v in z], [round(v, 2) for v in relu])Why activations must be non-linear
If every layer only multiplied and added, stacking ten layers would still be one big linear function, no better than a single layer. Non-linear activations between layers let the network bend its decision boundaries and model complex patterns, which solves problems like XOR.
| Activation | Formula | Where it is used |
|---|---|---|
| ReLU | max(0, z) | The default for hidden layers: simple and fast |
| Sigmoid | 1 / (1 + e^(−z)), between 0 and 1 | Output layer for yes/no (binary) problems |
| Tanh | Between −1 and 1 | Some older and recurrent networks |
| Softmax | e^(zᵢ) ÷ Σ e^(zⱼ), all outputs add to 1 | Output layer for choosing one of many classes |
Softmax
For a 3-class problem (say, routing a ticket to billing, delivery or technical), the last layer outputs 3 raw scores called logits. Softmax turns them into probabilities that are positive and add up to 1. Bigger scores get bigger probabilities. In code, subtract the largest score from all scores before taking exponentials; the answer is the same, but huge scores no longer overflow.
import math
logits = [2.0, 1.0, 0.1]
m = max(logits)
exps = [math.exp(v - m) for v in logits]
print([round(e / sum(exps), 4) for e in exps])Counting parameters
A dense layer from a inputs to b neurons has a × b weights plus b biases. A network with layers of size 784 → 128 → 10 (a classic handwritten-digit model) has 784 × 128 + 128 = 1,00,480 parameters in the first layer and 128 × 10 + 10 = 1,290 in the second: 1,01,770 in total. Large language models have billions.
Common mistakes
- Using softmax for multi-label problems where several labels can be true at once. Use one sigmoid per label instead.
- Mixing up the weight matrix shape. Check: (neurons in this layer) × (inputs).
- Computing softmax without subtracting the max, which overflows for large scores.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Layers, activations and the forward pass · 1/6
Neurons arranged side by side form a layer. Layers stacked one after another form a network.
Prefer a video by a teacher? Find free videos on “neural network forward pass layers activation functions relu softmax explained” on YouTube ↗
In real life: Recognising handwritten PIN codes
Reading handwritten 6-digit PIN codes on postal envelopes is a classic use of neural networks. Each digit image (for example 28 × 28 pixels) is flattened into 784 inputs, passed through hidden layers with ReLU, and the output layer has 10 neurons with softmax: one probability for each digit 0–9. The highest probability is the predicted digit; low-confidence cases can be sent to a human.
Practice (0/3 done)
Write softmax(logits) that returns probabilities rounded to 4 decimal places. Subtract the maximum logit before calling math.exp so that large values do not overflow.
Write dense_relu(x, W, b): W is a list of rows, one row of weights per neuron; b has one bias per neuron. For each neuron compute Σ(w × x) + bias, apply ReLU (max(0, value)) and return the list rounded to 4 decimal places.
Write count_params(sizes) for a fully connected network whose layer sizes are given in order, for example [784, 128, 10]. Each pair of neighbouring layers (a, b) adds a × b weights and b biases. Return the total.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Training: loss, backpropagation and optimisersNeural networks and deep learning · same module
- CNNs, RNNs and transformers at a glanceNeural networks and deep learning · same module
- The OSI modelComputer Networking (CCNA level) · How networks work
- What full-stack meansFull-Stack Development · What full-stack development is
- TCP/IP and the network layersCyber Security: from zero to SOC analyst · Networking for security
- How large language models workNLP, embeddings and large language models
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Why do networks need non-linear activations between layers?
Answer: Without them, stacked layers collapse into one linear function
Non-linearity is what lets depth add power.
What is ReLU(−3)?
Answer: 0
ReLU outputs max(0, z).
Which output activation fits "pick one of 5 product categories"?
Answer: Softmax
Softmax gives one probability per class, summing to 1.
A dense layer goes from 100 inputs to 50 neurons. How many parameters?
Answer: 5,050
100 × 50 weights + 50 biases.
An image can have several tags at once (beach, sunset, people). Which output fits best?
Answer: One sigmoid per tag
Multi-label problems need independent probabilities per label.
What does a weight tell you in a neuron?Related
Answer: How strongly, and in which direction, an input affects the output
Big positive weights push the output up; negative ones push it down.
Which prediction gives the LARGEST log loss when the true label is 1?Related
Answer: p = 0.02
A confident wrong answer (p near 0 for a true 1) is punished most.
Which optimiser adapts the step size for each weight and is a common default?Related
Answer: Adam
Adam keeps running averages of gradients per weight.
Roughly how much memory do the weights of a 13-billion-parameter model need in float16?Related
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 3 more topics in “Neural networks and deep learning” before its exam: Training: loss, backpropagation and optimisers, CNNs, RNNs and transformers at a glance, Deep learning tools and hardware.
- Module 9 exam: Neural networks and deep learning · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, NLP, embeddings and large language models. 62 topics left in the course before the final exam.