Skip to content

Expert: deep learning and AI · Neural networks and deep learning · topic 46 of 62 · 30 min

CNNs, RNNs and transformers at a glance

Explanation

Different data shapes need different network designs. Convolutional neural networks (CNNs) slide small filters over images to find edges, textures and objects. Recurrent networks (RNNs, LSTMs) read sequences step by step. Transformers use attention to look at all parts of a sequence at once and now power most language and many vision models. Transfer learning reuses a pre-trained network for a new task.

Why special designs?

A plain dense network treats every input separately. For a 1,000 × 1,000 photo, that is a million inputs per colour and billions of weights in the first layer alone, and it ignores the fact that nearby pixels belong together. Special architectures build in useful assumptions about the data, which makes them smaller, faster and much better.

Convolutional neural networks (CNNs)

A convolution slides a small filter (for example 3 × 3 numbers) across the image. At each position it multiplies the filter by the pixels under it and adds them up, producing a feature map. One filter might respond to vertical edges, another to a certain texture. Because the same filter is reused everywhere, a CNN needs few weights and finds a pattern wherever it appears. Early layers learn edges, middle layers learn shapes like wheels or eyes, and deep layers learn whole objects.

python
signal = [1, 2, 3, 4, 5]
kernel = [1, 0, -1]
out = [sum(signal[i + j] * kernel[j] for j in range(len(kernel)))
       for i in range(len(signal) - len(kernel) + 1)]
print(out)
A 1-D convolution ("valid" mode, no padding): shows [-2, -2, -2]. This kernel measures change, like an edge detector. Deep learning libraries slide the filter without flipping it.

Pooling layers shrink feature maps. 2 × 2 max pooling keeps the largest value in each 2 × 2 block, halving the width and height and making the network less sensitive to small shifts.

Recurrent networks (RNNs and LSTMs)

Text, speech and sensor readings are sequences. An RNN reads them one step at a time, carrying a hidden state (a memory) from step to step. Plain RNNs forget long-range information because gradients fade over many steps; LSTMs and GRUs add gates that decide what to keep and what to forget. They were the standard for language until about 2017, but they are slow to train because each step waits for the previous one.

Attention and transformers

A transformer (introduced in the 2017 paper "Attention Is All You Need") processes all positions of a sequence at once. Its self-attention mechanism lets every word look at every other word and decide which ones matter for its meaning: in "the bank of the river", "bank" pays attention to "river". Because all positions are processed in parallel, transformers train very efficiently on GPUs. They power large language models (module 10) and, as vision transformers, many image models too.

Transfer learning

Training a big network from scratch needs huge data and compute. Transfer learning starts from a network already trained on a large dataset (for example ImageNet, with over a million labelled images) and adapts it to your task. Often you freeze the early layers, which already detect general edges and shapes, and retrain only the last layers on your few thousand examples. This is how small teams build strong image and text models.

Common mistakes

  • Using a dense network on raw images. Fix: use a CNN or a pre-trained vision model.
  • Training from scratch on a small dataset. Fix: start from a pre-trained model.
  • Feeding images of a different size or colour scaling from what the pre-trained model expects. Fix: use the model's own preprocessing.

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

CNNs, RNNs and transformers at a glance · 1/6

CNN · RNN · Transformer

Different data needs different network designs. Here are the big three.

Prefer a video by a teacher? Find free videos on “convolutional neural network explained rnn lstm transformer attention simple” on YouTube ↗

In real life: Reading vehicle number plates

Automatic number-plate recognition at toll plazas and parking lots typically uses a CNN-based detector to find the plate in the camera frame, then a recognition model (CNN, recurrent or transformer-based) to read the characters. Teams usually start from pre-trained models and fine-tune them on local plates, because Indian plates vary in fonts, colours (white, yellow, green for electric vehicles) and condition.

Practice (0/2 done)

Task 1 · Code · Python

Write conv1d(signal, kernel) for a "valid" 1-D convolution as used in deep learning: output[i] = Σ signal[i + j] × kernel[j] for every position where the kernel fits fully (no flipping, no padding). The output length is len(signal) − len(kernel) + 1. Return [] if the kernel is longer than the signal.

Task 2 · Code · Python

Write max_pool_2x2(grid) for a 2-D list with an even number of rows and columns. Split it into non-overlapping 2 × 2 blocks and return a new grid of the maximum of each block.

Quick quiz

Question 1

1.Why are CNNs good for images?

Question 2

2.What does 2 × 2 max pooling do to a 32 × 32 feature map?

Question 3

3.What is the main idea of self-attention?

Question 4

4.Why did transformers largely replace RNNs for language?

Question 5

5.You have 2,000 labelled photos of local crop diseases. What is the best start?

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 are CNNs good for images?

Answer: Small shared filters find local patterns anywhere in the image with few weights

Weight sharing and local filters suit images.

What does 2 × 2 max pooling do to a 32 × 32 feature map?

Answer: Makes it 16 × 16

Each 2 × 2 block becomes one value.

What is the main idea of self-attention?

Answer: Each word looks at the other words to decide which matter for its meaning

Attention weighs the relevance of every other position.

Why did transformers largely replace RNNs for language?

Answer: They process all positions in parallel and handle long-range links well

Parallel training and attention made them faster and stronger.

You have 2,000 labelled photos of local crop diseases. What is the best start?

Answer: Fine-tune a pre-trained image model (transfer learning)

Pre-trained features make small datasets go much further.

Why can an LLM state a wrong fact confidently?Related

Answer: It generates likely-sounding text and does not verify facts

That is hallucination; check important outputs.

From How large language models work

Why are GPUs fast for training neural networks?Related

Answer: They run thousands of simple maths operations in parallel, ideal for matrix multiplication

Training is dominated by parallel matrix maths.

From Deep learning tools and hardware

Why do networks need non-linear activations between layers?Related

Answer: Without them, stacked layers collapse into one linear function

Non-linearity is what lets depth add power.

From Layers, activations and the forward pass

Which variant updates the weights after each small group of examples, such as 64?Related

Answer: Mini-batch gradient descent

Mini-batch is the standard in deep learning.

From Gradient descent: learning step by step

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. 1 more topic in “Neural networks and deep learning” before its exam: 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: Training: loss, backpropagation and optimisers