Skip to content

Expert: deep learning and AI · Neural networks and deep learning · topic 44 of 62 · 35 min

Layers, activations and the forward pass

Explanation

A neural network stacks neurons into layers: an input layer, one or more hidden layers and an output layer. Each dense layer multiplies its inputs by a weight matrix, adds biases and applies an activation such as ReLU. Non-linear activations let networks learn curved patterns. Softmax turns the final scores into class probabilities.

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.

python
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])
Shows [0.0, 0.25] [0.0, 0.25]. Neuron 1's total is 0.2 − 0.3 + 0.1 = 0.0.

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.

ActivationFormulaWhere it is used
ReLUmax(0, z)The default for hidden layers: simple and fast
Sigmoid1 / (1 + e^(−z)), between 0 and 1Output layer for yes/no (binary) problems
TanhBetween −1 and 1Some older and recurrent networks
Softmaxe^(zᵢ) ÷ Σ e^(zⱼ), all outputs add to 1Output 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.

python
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])
Shows [0.659, 0.2424, 0.0986]. The first class is most likely.

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

Input → hidden → output

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)

Task 1 · Code · Python

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.

Task 2 · Code · Python

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.

Task 3 · Code · Python

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

Question 1

1.Why do networks need non-linear activations between layers?

Question 2

2.What is ReLU(−3)?

Question 3

3.Which output activation fits "pick one of 5 product categories"?

Question 4

4.A dense layer goes from 100 inputs to 50 neurons. How many parameters?

Question 5

5.An image can have several tags at once (beach, sunset, people). Which output fits best?

Read more (free)

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.

From The artificial neuron

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.

From Logistic regression and classification

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.

From Training: loss, backpropagation and optimisers

Roughly how much memory do the weights of a 13-billion-parameter model need in float16?Related

Answer: 26 GB

13 × 10⁹ × 2 bytes ≈ 26 GB.

From Deep learning tools and hardware

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

  1. 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.
  2. Module 9 exam: Neural networks and deep learning · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, NLP, embeddings and large language models. 62 topics left in the course before the final exam.

← Previous: The artificial neuron