CNNs for Text
P6.dl-architectures.07 · Audience: Guest, Language professional, IT/ML learner · Prerequisites: Regularisation and Normalisation
Where next?
Later in Deep-Learning Architectures
P6.dl-architectures.07 · Audience: Guest, Language professional, IT/ML learner · Prerequisites: Regularisation and Normalisation
Later in Deep-Learning Architectures
Convolutional Neural Networks scan text with a small sliding window, scoring every consecutive group of tokens against a learned pattern. This module shows how a 1D convolution works on a real token sequence, what feature maps and max-pooling produce — and why the fixed window means CNNs cannot capture long-range context, motivating the Transformer.
From images to text — the convolution idea. Convolutional Neural Networks were designed for images. An image detector that recognises a cat's ear does not need to know whether the ear is in the top-left corner or the bottom-right: the same small filter slides across every position.
For text, the same idea applies. Instead of sliding over pixels, a 1D CNN slides over word positions. A filter of width 3 looks at every consecutive triplet of tokens:
| Window | Tokens |
|---|---|
| Position 1 | Lions · are · the |
| Position 2 | are · the · most |
| Position 3 | the · most · dangerous |
Each window produces one number — a measure of how strongly that triplet matches the filter. All those numbers together form a feature map.
A 1D CNN filter of width k is a vector of k learnable weights w = [w₁, w₂, …, wₖ]. Applied to token representations x₁, x₂, …, xₙ, it computes a dot product with each consecutive window of k values. Sliding across all valid positions produces a feature map of length n − k + 1.
Why it matters — CNNs were used for text classification (e.g. Kim 2014) before attention-based models replaced them. Understanding CNNs clarifies what the Transformer improves upon: full attention replaces the fixed window with a dynamic, position-aware comparison between every pair of tokens.
Reference: Kim 2014 — Convolutional Neural Networks for Sentence Classification
Self-check
Q1 — CNN terminology: a CNN's sliding window of learnable weights applied to local input features is called a what?
Curated resources to explore convolutional neural networks further.
| Type | Resource |
|---|---|
| Video | 3Blue1Brown — But what is a convolution? (YouTube) |
| Course | Coursera — Deep Learning Specialization, Course 4: Convolutional Neural Networks, Andrew Ng (free audit) |
| Course | fast.ai — Practical Deep Learning for Coders Lesson 7: CNNs (free) |
| Reference | CS231n Stanford — Convolutional Neural Networks for Visual Recognition (cs231n.github.io) |
Practise this lesson's 5 self-checks
One per screen: you reveal each answer and mark it yourself. Nothing is recorded.
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
A scratch console for this page's ideas — ungraded, nothing you run here is recorded.
Scratch console
A scratch console with the scientific stack (pandas, numpy, scikit-learn). Runs on the server — no network, resource-limited and measured.
Output appears here.
Loading your notes...