Skip to main content

How a deep neural network works

Why This Matters

Most of the AI systems that make the news are deep neural networks: image recognition, speech, language models. You will never review their millions of weights, but knowing how a network computes an answer and how it learns explains why it fails the way it does, why coverage means something different here, and why small input changes can flip its answer.

What you will learn

  • The structure of a deep neural network: input, hidden and output layers, neurons, weights and biases
  • How a neuron calculates its output, and what the activation function does
  • How a network is trained: loss, backpropagation and epochs

Syllabus: AI-3.4.1.

From perceptron to deep network

The perceptron is the earliest neural network: one layer, one neuron. It learns a binary classification by drawing a straight line between the two classes, so it can only learn problems where such a line exists (linearly separable problems): spam or not spam on two features, or the logical AND. You will build one in the next lesson.

A deep neural network (DNN) stacks many layers of neurons. Usually every neuron is connected to every neuron in the next layer (fully connected): this is also called a multi-layer perceptron. With enough layers and neurons it can learn very complicated, non-linear patterns.

 input layer        hidden layers          output layer
  (the data)    (learned representations)   (the result)

   x1 ──┬──────> (h1) ──┬──────> ( ) ──┐
        │╲      ╱       │╲            ├──> P("cat") = 0.92
   x2 ──┼─╲──> (h2) ────┼──────> ( ) ──┘
        │  ╲  ╱         │╱
   x3 ──┴──> (h3) ──────┘
  • The input layer receives the data: the pixel values of an image, the fields of a loan application.
  • The hidden layers transform it, layer by layer, into more and more abstract representations. In an image network, early layers respond to edges, later layers to shapes, the last ones to “ear” or “wheel”.
  • The output layer produces the result: for example the probability that the image shows a cat.

What one neuron does

Every connection has a weight; every neuron has a bias. A neuron:

  1. multiplies each input (the outputs of the previous layer, its activations) by the weight of its connection
  2. adds them up, plus its bias
  3. passes the sum through a non-linear activation function

output = activation(w1·x1 + w2·x2 + … + bias)

Without the activation function, a stack of layers would collapse into one big weighted sum: a straight line again. The non-linearity is what lets deep networks learn curves. Two common activation functions:

  • ReLU: max(0, sum). Negative sums become 0. Used in hidden layers.
  • Sigmoid: squeezes any number into 0 to 1. Used for a probability in the output.

Note: this bias is a number in a neuron. It has nothing to do with the bias in data or in decisions (unfair treatment of a group, lesson 35). The exam uses both meanings.

A worked forward pass

A tiny network: 2 inputs, 3 hidden neurons with ReLU, 1 output neuron with sigmoid.

Hidden neuronWeight from x1Weight from x2Bias
h11.0−1.00.0
h20.50.5−0.5
h3−1.02.00.0

Output neuron: weights 1.0 (from h1), 1.0 (from h2), −0.5 (from h3), bias −0.5, sigmoid.

Input x1 = 2, x2 = 1:

  • h1 = ReLU(1.0·2 + (−1.0)·1 + 0.0) = ReLU(1.0) = 1.0
  • h2 = ReLU(0.5·2 + 0.5·1 − 0.5) = ReLU(1.0) = 1.0
  • h3 = ReLU(−1.0·2 + 2.0·1 + 0.0) = ReLU(0.0) = 0
  • output = sigmoid(1.0·1.0 + 1.0·1.0 + (−0.5)·0 − 0.5) = sigmoid(1.5) = 0.818

This is the forward pass: data flows from input to output. A real network does the same with millions of neurons, which is why it needs GPUs (lesson 3): it is all multiplication and addition, in parallel.

Notice that h3 output 0 for this input: it was not activated. Which neurons are activated by which inputs is the basis of the coverage measures in lesson 23.

How a network learns

  1. Start: weights get small random values; biases small random values or zero.
  2. Forward pass: run a training example through the network.
  3. Loss: compare the output with the correct answer. The difference is the error, or loss.
  4. Backpropagation: send the error back through the network, and adjust every weight and bias a little in the direction that would have made the error smaller.
  5. Repeat for every training example. One pass through all the training data is an epoch.
  6. Continue for many epochs, until the output is good enough (or stops improving).

The size of each adjustment is set by the learning rate, an algorithm hyperparameter (lesson 11).

What this means for testing

  • The knowledge is in the weights, spread over millions of numbers. There is no line of code to review. You test behaviour.
  • Random starting weights mean two training runs on the same data give slightly different networks. Retraining is not a deterministic build.
  • Small input changes can cross a boundary. The network’s decision boundaries are complicated curves in a space with thousands of dimensions; a change you cannot see can push an input across one. That is the basis of adversarial examples (lesson 45).
  • Code coverage is meaningless: every input runs every line of the network’s code. Lesson 23 shows what to measure instead.

Exercise

Calculate the output of the network above for the input x1 = 0, x2 = 1. Which hidden neurons are activated (output above 0)?

Show suggested answer
  • h1 = ReLU(1.0·0 − 1.0·1 + 0) = ReLU(−1.0) = 0
  • h2 = ReLU(0.5·0 + 0.5·1 − 0.5) = ReLU(0.0) = 0
  • h3 = ReLU(−1.0·0 + 2.0·1 + 0) = ReLU(2.0) = 2.0
  • output = sigmoid(1.0·0 + 1.0·0 − 0.5·2.0 − 0.5) = sigmoid(−1.5) = 0.182

Only h3 is activated. With the earlier input (2, 1), h1 and h2 were activated and h3 was not. Together the two inputs activate all three hidden neurons.

Exam-style questions

Quiz

1. How does a neuron in a deep neural network calculate its output?

  • A. It selects the largest input
  • B. It calculates the weighted sum of the previous layer’s activations plus a bias, and applies an activation function
  • C. It averages all inputs without weights
  • D. It looks up the answer in the training data
Show answer
B.
Quiz

2. What is an epoch?

  • A. One forward pass of a single example
  • B. One pass through the complete training data
  • C. The time needed to deploy a model
  • D. One layer of the network
Show answer
B.
Quiz

3. Which problem can a single perceptron NOT learn?

  • A. A problem in which the classes can be separated by a straight line
  • B. The logical AND
  • C. A problem in which the classes cannot be separated by a straight line, such as XOR
  • D. Spam classification on two features that separate the classes linearly
Show answer
C. A perceptron learns linearly separable problems only. The next lesson shows it.