Chapter 5 — Deep Learning Prerequisites
This is the hardest conceptual jump in the whole course, so take it slowly. Everything here is building toward two specific tools: a CNN (convolutional neural network) and an LSTM (Long Short-Term Memory network). Chapter 6 combines both into one real model. This chapter is just about understanding, deeply, what each one actually does and why you'd want it — no code yet.
Part A: The CNN — A Shape Detective
Think of it like this: imagine a kid with a small magnifying glass sliding it slowly across a comic strip, looking for one specific repeating shape — say, a "BAM!" text bubble — no matter where on the page it appears. They don't need to know it's on panel 3 versus panel 7; they just slide the magnifying glass along and shout "found one!" every time the shape underneath matches. That sliding, shape-matching magnifying glass is a convolution.
More precisely: a convolution is a small "stamp" (technically called a filter or kernel) that slides along the signal, checking at every position: "how much does the little window right here look like my stamp shape?" The output is a new signal showing where matches were strong. A real network doesn't use just one stamp — it learns many stamps at once, each hunting for a different shape: a spindle-shaped stamp, a K-complex-shaped stamp, and others nobody explicitly designed but that turned out to be useful during training.
Try It: Robo Learns to Spot a Spindle
Before more words, watch (and drive) the whole idea happen. This is a five-step interactive workshop — the same sliding-stamp idea above, but you control the slider, watch the heat strip build, zoom it out yourself, compare two different stamps side by side, and then train Robo's stamp by hand across six real rounds.
1D versus 2D is just a question of what you're sliding along. A 2D convolution (the kind used on photos) slides a small square window over a flat image, looking for things like edges or corners. A 1D convolution slides a small window along a single timeline instead — looking for a shape in a signal over time, which is exactly what EEG is. Same underlying idea, one fewer dimension.
Pooling is the "zoom out and summarize" step that usually follows a convolution. Imagine condensing an entire book chapter down to one sentence for a book report — you lose the exact wording, but you keep the gist. Pooling does this to the convolution's output: it shrinks a small region down to one number (often just "what was the strongest match in this neighborhood"), so the network ends up caring that a spindle-shape was found somewhere around here, without needing to care about the exact sample it occurred at. That's genuinely useful, since the real world never places a spindle at exactly the same millisecond twice.
THE CONNECTION BACK TO CHAPTER 3
Here's a satisfying fact worth sitting with: a 1D CNN trained on raw EEG, left to its own devices, tends to rediscover something very close to the band-power features you computed by hand in Chapter 3. It's not magic — filters that respond strongly to slow, rolling waves are effectively re-inventing a delta detector; filters tuned to fast, tight oscillations are re-inventing a sigma/spindle detector. The CNN isn't doing something fundamentally different from Chapter 4's classical approach. It's learning its own version of the same ingredients, instead of you hand-designing them.
Part B: The LSTM — A Notebook That Remembers
Think of it like this: Chapter 4's Random Forest looks at each 30-second epoch completely alone, like reading a single sentence ripped out of a novel with no idea what chapter it's from. An LSTM instead reads the book panel by panel, remembering what happened a few panels back — because the story (tonight's sleep) has rules. You basically never jump straight from deep N3 to REM, the same way a mystery novel doesn't reveal the culprit in a random, disconnected sentence with zero setup. Context from earlier constrains what can plausibly happen next.
You already felt this directly in the "Predict the Next Stage" game coming up next: shown a bare, ambiguous epoch alone, a guess is genuinely hard. Shown the same epoch with the last few stages as context, the right answer often becomes obvious. That's the entire reason this course reaches for a network with memory instead of judging every epoch in isolation — and it's exactly what an LSTM's hidden state provides automatically, for every epoch, without anyone hand-coding "don't jump from N3 to REM" as a rule.
Try It: Robo Remembers
Same idea as before — watch it happen before reading more words. Meet Robo's two notebooks, the three "doormen" gates, and step through one real night epoch by epoch, with a couple of moments where you guess before Robo reveals what it actually does.
Why "Long Short-Term" — the Two Notebooks
The name is actually a precise description, not marketing. An LSTM keeps two kinds of memory working side by side: a fast-changing short-term notebook (updated almost every step, like your immediate train of thought — easily overwritten) and a slower, more protected long-term notebook (the cell state), which carries important information across many steps without being casually erased by every small detail that comes along. Picture a sticky note on your desk next to a locked journal: the sticky note gets scribbled over constantly, the journal only gets a new entry when something actually matters.
The Three Doormen (Gates)
What decides what's "important enough" for the long-term journal? Three small, learned decision-makers, usually called gates:
- The forget gate — decides what old information in the journal is no longer relevant and can be crossed out.
- The input gate — decides what's genuinely new and worth writing into the journal from the current epoch.
- The output gate — decides what part of the journal actually gets read out and used for this epoch's decision.
All three are learned during training, the same way a convolution's "stamp" shapes are learned — nobody hand-writes the rule "forget Wake after three epochs of N3." The network discovers which patterns of forgetting, writing, and reading actually improve its predictions.
Try It: Predict the Next Stage
Before any code, feel the core insight yourself. The next lesson gives you a short history of recent stages and asks you to predict what comes next — the exact job an LSTM's memory does automatically, every single epoch.
IF YOU'RE BUILDING YOUR FIRST CNN
Don't start on real EEG. Build a tiny 1D CNN on a simple synthetic signal first — something where you already know the answer, like detecting a sine-wave bump you inserted yourself. Get the plumbing (shapes, training loop, loss going down) working on something trivial before pointing it at anything real.
DONE WHEN
You can explain, without notes, why a 1D CNN sliding over raw EEG is effectively rediscovering the band-power features from Chapter 3 — and why an LSTM's "memory" is doing, automatically, what you just did by hand in the next lesson's game.
Chapter 6 combines both ideas — a CNN to extract shape-based features per epoch, an LSTM to carry memory across epochs — into one real, working model: TinySleepNet.