Skip to content
Pixel
العربية

7-2Unit 7 · Machine Learning and Artificial Intelligence

Neural Networks and Deep Learning

13 min readStart exercises
In this lesson
  1. The Big Idea
  2. The Structure of a Neural Network: Layers of Units
  3. How a Neuron Works
  4. Training: Adjusting the Weights to Reduce the Loss
  5. Deep Learning and Hierarchical Features
  6. Why Deep Learning Brought a Leap in AI
  7. Try It Yourself
  8. Common Mistakes
  9. Key Points
  10. FAQ

The Big Idea

A neural network is a mathematical model built from simple computing units arranged in layers: an input layer, hidden layers and an output layer. Each unit multiplies its inputs by weights, adds them up, adds a bias, and usually applies an activation function. Training means adjusting those weights gradually until the loss on the training data goes down. Deep learning uses many layers in sequence that learn increasingly combined representations, from edges to parts to the whole object. Despite its big achievements, depth alone does not guarantee success and does not mean general understanding.

The Structure of a Neural Network: Layers of Units

A neural network is a mathematical model whose name was historically inspired by the idea of nerve cells, but it is not a copy of the brain. It is made of computing units called nodes or neurons, plus the links between them, which are defined by the model's architecture.

In the simplified teaching example, units are organised into layers (a layer is a group of neurons at one stage), and each layer connects to the next. In real networks the pattern of connections varies with the type of network.

Three kinds of layer, by role

| Layer | Role | |---|---| | Input layer | Represents the values entering the model, such as the pixel values of an image | | Hidden layer (middle layer) | Turns the data into new representations the network learns, i.e. extracts features | | Output layer | Produces values suited to the task, such as probability scores for each class |

The short version students remember: input receives, hidden processes, output answers.

Example: dog or cat?

Data enters through the input layer: the image's pixel values. It passes through hidden layers that may learn patterns linked to edges, shapes or other representations. The output layer then produces scores or probabilities for "dog" and "cat", and a decision rule is applied (for example: pick the class with the highest score).

A simplified neural network A network of four columns of nodes. The input layer has four nodes fed by pixel values; values enter the model here. Two hidden layers of five nodes each turn the data into new representations. The output layer has two nodes, Dog and Cat, producing task values such as class scores. Every node connects to every node in the next layer, and an arrow at the bottom shows data flowing from input to hidden to output. A simplified neural network Pixel values Dog Cat Input layer values enter the model Hidden layers turn the data into new representations Output layer task values, e.g. class scores Data flows: input → hidden → output
Data flows one way: input, then hidden, then output.

Who decides the shape of the network?

The designer chooses the architecture: how many layers and how many units in each, based on the task, the data, the resources and the results of experiments. More depth or more units can help with some problems, but it raises the cost (time and computing) and doesn't guarantee better performance or generalisation.

How a Neuron Works

In the common simplified model, a neuron does four things:

  1. Receives several values from the previous layer.
  2. Multiplies each value by its own weight, then adds the results: this is the weighted sum.
  3. Adds a bias, a fixed value the neuron learns that shifts the result up or down.
  4. Applies an activation function to the result, then passes the output to the next part of the network.

The activation function lets the network learn relationships that are not simply proportional. One of the simplest keeps positive values as they are and turns negative values into zero, written max(0, z).

Weights: how much, and in which direction

A weight is a numerical factor that sets how much and in which direction a value contributes to the calculation:

  • A large weight means the input has a strong influence; a small one, a weak influence.
  • A weight can be positive, raising the sum, or negative, lowering it.

But careful: a weight on its own does not mean a particular word or feature will decide the result, because a prediction comes from many parameters interacting across the whole network.

A worked example

A neuron has three inputs, 2, 1 and 3, with weights 0.5, −1 and 0.4, and a bias of 0.3:

z = (2 × 0.5) + (1 × −1) + (3 × 0.4) + 0.3
  = 1 − 1 + 1.2 + 0.3
  = 1.5
output = max(0, 1.5) = 1.5

Notice how the negative weight cancelled out the second input's contribution. If the sum had been negative, the activation function in this example would have output zero.

Computing a neuron's output Three inputs, x1 = 2, x2 = 1 and x3 = 3, connect to the neuron by lines whose thickness reflects their weights: w1 = 0.5, w2 = −1 and w3 = 0.4. The neuron computes a weighted sum and adds the bias b = 0.3, then passes the result through an activation function, here max(0, z), and the output goes to the next layer. The working below: z = (2×0.5) + (1×−1) + (3×0.4) + 0.3 = 1 − 1 + 1.2 + 0.3 = 1.5, and output = max(0, 1.5) = 1.5. Note: line thickness shows weight size, and a negative weight pulls the sum down. How a neuron computes its output x1 = 2 w1 = 0.5 x2 = 1 w2 = −1 x3 = 3 w3 = 0.4 Σ + b b = 0.3 weighted sum + bias Activation max(0, z) Output 1.5 to the next layer z = (2×0.5) + (1×−1) + (3×0.4) + 0.3 z = 1 + −1 + 1.2 + 0.3 = 1.5 output = max(0, 1.5) = 1.5 Line thickness = weight size. A negative weight pulls the sum down.
One neuron: multiply by the weights, add up, add the bias, apply the activation function.

Training: Adjusting the Weights to Reduce the Loss

Before training, the network knows nothing. Training is the process of gradually adjusting the network's parameters (weights and biases) to reduce a loss function on the training data, using an optimisation algorithm. Usually backpropagation computes the gradients needed for this adjustment, i.e. it works out for each weight whether to increase or decrease it, and roughly by how much.

The loss function is a number measuring how far the network's outputs are from the target values. The smaller the loss, the closer the predictions are to correct on the training data.

| Stage | What happens | |---|---| | Before training | Parameters usually start with random or carefully chosen near-random values, so predictions are usually poor | | During training | The loss between outputs and targets is measured, then the parameters are adjusted gradually to reduce it | | After training | Performance is measured on separate validation or test data to see how well the model generalises to new examples; training does not guarantee every prediction is correct |

The neural-network training loop A loop of four steps: 1) training data, inputs with target values; 2) the network predicts with its current weights; 3) compute the loss, how far the prediction is from the target; 4) nudge the weights: backpropagation finds the gradients and an optimiser adjusts them. An arrow returns to the prediction step, repeating until the loss on the training data goes down. Below: separate test data, never used in training, measures generalisation on new examples, with a warning in red: low training error plus high test error means overfitting, memorising rather than learning. How a network is trained 1) Training data inputs + target values 2) The network predicts with current weights 3) Compute the loss how far from the target? 4) Nudge the weights backpropagation finds the gradients, an optimiser adjusts Repeat until the loss on the training data goes down Separate test data never used in training Measure generalisation on new examples Low training error + high test error = overfitting (memorising, not learning)
The loop lowers the loss on the training data; the separate test reveals whether the network really learned or just memorised.

Overfitting and generalisation

Training aims to find parameters that reduce the loss and improve performance. But a low training error on its own is not enough. Overfitting can happen: the network memorises the training examples in detail, does brilliantly on them, then makes mistakes on new examples.

So we evaluate the model on separate data that was never used to adjust its parameters. The ability we want is called generalisation: working correctly on new data the model has never seen.

Deep Learning and Hierarchical Features

Deep learning is learning with multi-layer neural networks that can learn successive representations from data. "Deep" refers to the depth of the architecture, i.e. many hidden layers one after another. There is no single universal number of layers that separates a deep model from any other.

From edges to a face: hierarchical features

Successive layers may learn more abstract representations the deeper you go. In some image networks, a possible progression is described for teaching purposes like this:

| Layer position | Examples of features extracted | |---|---| | Shallow layers | Simple features such as lines and changes in colour | | Middle layers | Shapes of parts such as eyes, nose and ears | | Deep layers | The structure of the whole face or object |

Hierarchical features in deep learning Four panels in sequence for an image-recognition network. Input image: a simple face as pixel values. Shallow layers: scattered short strokes representing lines and colour changes. Middle layers: separate parts, two eyes, a nose and a mouth. Deep layers: the whole face built from the parts. An arrow at the bottom says deeper layers give more abstract, more combined representations, with a note that this is an approximate teaching example, not a fixed path in every network. Hierarchical features in an image network Input image pixel values Shallow layers lines and colour Middle layers eyes, nose, ears Deep layers the whole face Deeper layers: more abstract, more combined representations An approximate teaching example, not a fixed path in every network
Building up in a hierarchy: each layer combines what the layer before it picked up.

This is an approximate example to help understanding, not a fixed path every network follows. These successive representations may help a network learn complex relationships and perform well if the data, architecture, training and evaluation are suitable; adding depth alone does not guarantee it.

Why has deep learning spread now?

A combination of factors made it take off:

  • Better architectures and algorithms, activation functions and optimisation methods.
  • More data and better software tools.
  • More computing power: chips such as GPUs and TPUs speed up many operations, but they are not required for every use or every prediction.

Why Deep Learning Brought a Leap in AI

Deep learning made big progress on specific tasks such as image classification, speech recognition and language processing. It may approach or beat human performance on a specific test, but it can fail outside the evaluation conditions, and that does not mean it has general intelligence or general understanding.

| Field | Examples | |---|---| | Image recognition | Face recognition systems, diagnostic support for medical images, object detection for self-driving cars | | Speech recognition | Voice control of smart speakers, speech-to-text, machine translation | | Natural language processing (NLP) | Machine translation, text summarisation, answering questions |

The secret: features are learned automatically

In many traditional methods, people designed the features by hand: an engineer might decide that "number of straight lines" is an important feature for recognising digits. Deep-learning networks can learn many representations from the data itself. Even so, choosing the task, the data, the architecture, the objective and the evaluation remains a human responsibility.

These abilities supported the development of models that generate text, images, audio and software (next lesson's topic), and the quality and risks of their outputs must be evaluated for every use.

A practical example: a voice assistant turns spoken commands into text and works well in many situations nobody wrote separate rules for, because it generalised patterns learned from many examples. But it may make mistakes with a dialect that was under-represented in its data, with heavy noise, or with a rare word.

Try It Yourself

Try
Compute a neuron's output: change the inputs, weights and bias and watch what a negative weight and the activation function do
Negative weight: this input lowers the sum.
z = 2 × 0.5 + 1 × (-1) + 3 × 0.4 + 0.3
  = 1 + (-1) + 1.2 + 0.3
  = 1.5
output = max(0, 1.5) = 1.5
Weighted sum + bias (z)
1.5
Output · Activation (example): max(0, z)
1.5
This is one example of an activation function: it keeps positive values and turns negative ones into zero.
z = 1.5, output = 1.5
Try
Steps in training a neural network
Put what happens while training and evaluating a neural network in order, from start to finish.
1. Compute the gradients with backpropagation
2. Nudge the weights to reduce the loss, and repeat many times
3. Measure performance on separate test data to see how well it generalises
4. Feed the training examples through the layers to get predictions
5. Compare the predictions with the target values and compute the loss
6. Initialise the weights with random or near-random values
Use the up and down buttons to move each step, then press Check.
Try
Which layer does this?
In a network that reads handwritten Arabic numerals from scanned school registers, sort each description by the layer responsible for it.
Items (7)
Tap an item to select it, then tap the category it belongs to.
Nothing here yet
Nothing here yet
Nothing here yet

Common Mistakes

"A neural network is a mini copy of the human brain and thinks like it"

Neural networks only took their name, historically, from the idea of nerve cells.

A neural network is a mathematical model of computing units (multiplying, adding, applying functions) whose parameters are adjusted by training.

"The bias in a neuron is the same thing as bias in the data"

Same English word, completely different meaning.

A neuron's bias is a number it learns and adds to the weighted sum. Bias in data (lesson 7-1) is skewed data leading to skewed judgments.

"A big weight on one feature means that feature alone decides the result"

A prediction comes from many parameters interacting across all the layers.

A weight sets how much, and in which direction, one value contributes to one neuron's calculation; it can be positive or negative.

"If the training error is very low, the model is excellent"

The network may simply have memorised the training examples: that is overfitting.

Judge a model by its performance on separate data it never trained on, i.e. by how well it generalises.

"More layers always means better performance"

Depth raises the cost, needs suitable data and training, and there is no magic number of layers.

Depth may help represent more complex relationships, but it doesn't guarantee better performance or generalisation.

Key Points

  • A neural network is a mathematical model of units (nodes/neurons) in layers: input receives, hidden layers transform representations and extract features, output answers.
  • A neuron: weighted sum of inputs + bias, then usually an activation function, then the result goes to the next layer.
  • A weight sets how much and in which direction an input contributes; it can be positive or negative and does not decide the result alone.
  • Training adjusts the parameters gradually to reduce a loss function with an optimisation algorithm; backpropagation computes the gradients.
  • A low training error is not enough: overfitting can happen, so we measure generalisation on separate test data.
  • Deep learning uses many layers that build hierarchical features (lines → parts → whole); there is no universal layer count for "deep", and depth alone doesn't guarantee performance.
  • It spread thanks to several factors: better architectures and algorithms, data and tools, and computing power (GPUs and TPUs).
  • Its leap in images, speech and language comes from learning features automatically from data, but it excels at specific tasks, not general intelligence.

FAQ

What is the difference between a weight and the bias in a neuron?

A weight multiplies one particular input to set how much, and in which direction, it contributes; every input has its own weight. The bias is a single value added to the whole weighted sum, shifting it up or down. Both are parameters the network learns during training.

What does overfitting mean, in simple words?

The network memorises the training examples instead of learning the general pattern, so its training error is very low but it makes mistakes on new examples. You catch it by measuring performance on separate data that was not used for training.

How many layers does a network need to count as deep learning?

According to the Arabic edition, there is no single universal number. Deep refers to the depth of the architecture, meaning many hidden layers in sequence that learn successive representations, not a particular count.

Can deep learning run without a GPU?

Yes, in many cases. GPUs and TPUs speed up many operations, especially in large-scale training, but the book makes clear they are not required for every use or prediction.

Done reading? Test yourself