In sentences, sounds, sensor logs and stock prices, order creates meaning. Give a network that looks at one frame at a time a loop that remembers "what it just saw," and you get an RNN. Add doors (gates) so that memory does not fade, and you get LSTM and GRU. We follow with numbers whether memory really survives, and confirm by experiment that even with gates, learning does not happen by itself.
In 3-1, the fruit fly recognized whether a shadow was "approaching." But what if it received only a single photo of the shadow?
The neural networks of 3-2 and 3-3 take one input, produce one output, and are done. They do not remember what they saw before. Such a network is called a feedforward (flowing only forward) neural network. A single shadow of a given size may be approaching or receding. The answer lies not in one frame but in the order of the frames. That is also why the experiment in 3-1 fed in two frames (earlier and later) together.
The sequences "100 → 110 → 120" (rising) and "120 → 110 → 100" (falling) are made of the same three numbers. There are two ways to feed them into a feedforward neural network.
ht = f(xt, ht−1)Reuse the same weights at every time step
The idea that solves the problem of Section 2 is simple. When processing one step, the network also receives the summary made at the previous step (the hidden state). Drawn as a picture, an arrow leaving the box returns to the box itself and forms a loop. Unroll this loop along the time axis, and it looks like a feedforward neural network with the same box repeated once per step. All the boxes use the same weights. Just as the CNN in 3-3 used the same filter at every "position," an RNN uses the same weights at every "time step." So the number of weights stays the same even when the length changes.
ht = tanh(wx·xt + wh·ht−1)Elman, 1990
Applying backpropagation on the unrolled view gives BPTT (backpropagation through time) [R8]. For the error at the last output to reach the first step, it is multiplied by wh × the tanh gradient (1 − h²) at every step it goes back. If this value is less than 1, it is raised to the power of the number of steps and approaches 0 (vanishing gradient); if it is greater than 1, it can explode. In 1994, Bengio et al. showed that this makes it fundamentally hard to learn long-term dependencies with gradient descent [R2].
Making wh large keeps memory longer (1.5), but tanh sticks near ±1 and the gradient 1 − h² approaches 0. Holding on to memory and correcting that memory through learning collide with each other. It is the same root problem as "the sigmoid gradient is at most 0.25" in 3-2.
ct = ft ⊙ ct−1 + it ⊙ gt · ht = ot ⊙ tanh(ct)Hochreiter & Schmidhuber, 1997 · forget gate Gers et al., 2000
| Symbol | Name | Range | What it does (weights set for this cell) |
|---|---|---|---|
| ct | Cell state | Unbounded | A conveyor belt carrying long-term memory. It passes to the next step through just one multiplication (f) and one addition |
| ft | Forget gate | 0~1 (σ) | How much of the past memory to keep. Held constant at σ(bf) |
| it | Input gate | 0~1 (σ) | How much of the new value to write in. σ(10 × start flag − 5): opens only at the first step |
| gt | Candidate value | −1~1 (tanh) | What to write in. tanh(3 × xt) |
| ot | Output gate | 0~1 (σ) | How much of the memory to let out. Kept always open at σ(5) ≈ 0.993 |
| ht | Hidden state (output) | −1~1 | ot ⊙ tanh(ct). Goes to the next layer and to the gates at the next step |
| ⊙ | Element-wise product | Multiplies matching positions of vectors. With a single number, as in this example, it is just multiplication |
The weights of this cell were set by hand for explanation. The rule is "write only at the first step, and hardly erase after that." So when the first signal is flipped, the memory 20 steps later flips exactly too. For the RNN in Section 4, flipping the first signal of the same input left the last value identical to four decimal places. This means LSTM is a structure that can hold long-term memory. Because the cell state is carried on only by one multiplication (f ≈ 1) and an addition, the gradient passed backward barely shrinks either [R3].
Two additions. First, the original 1997 LSTM had no forget gate. Gers et al. added the forget gate to fix the problem of the cell state only growing on endlessly continuing input [R4], and today "LSTM" usually means this version. Second, the fact that the structure can hold memory does not guarantee that learning will find those weights. The experiment in Code 2 shows that difference (below Section 6).
ht = (1 − zt) ⊙ ht−1 + zt ⊙ h̃tCho et al., 2014
GRU merges LSTM’s forget gate and input gate into a single update gate z. It keeps (1 − z) of the past memory and fills the freed space with the new candidate h̃ by z. It is a balance scale: the more it keeps, the less it writes, and the more it writes, the less it keeps. On top of this it adds a reset gate r, which decides how much of the past memory to use when computing the candidate. Since it does not keep a separate cell state and hidden state, it has no output gate either [R5].
The weight groups shrink from LSTM’s 4 to 3, so by the textbook formula the parameters are exactly 3/4. Keras’s default GRU keeps one extra set of biases, so it has slightly more (912 vs. 1,152 with 16 hidden units and input size 1, about 0.79×). Which one is better depends on the task.
A sequence in which every step is ±1 and the answer is the first step (all later steps are identical-looking distractors). 16 hidden units, Adam 0.005, 30 epochs, 2,000 training and 1,000 test samples. Results for 2 seeds are listed separately. Around 50% is guessing level, i.e. a learning failure.
It learns to squeeze the input into a narrow middle layer (the bottleneck) and then restore it to the original. No answer labels are needed: the input itself is the answer. The fruit fly mushroom body does the exact opposite and expands.
A narrow bottleneck forces it to keep only what matters most. The sequences in this figure are 16-step pieces made by mixing two kinds of sine waves. It has been proven that the best result a (linear) autoencoder without activation functions can reach by learning is the same as principal component analysis (PCA) [R6], so this figure computes and shows that optimal result directly. Stacked, nonlinear autoencoders can compress better than PCA [R7].
In sequence models, this was used as a structure (encoder–decoder) in which an RNN encoder compresses a sentence into a fixed-length vector and an RNN decoder unfolds it into another sentence [R5]. The limitation that a long sentence is hard to fit entirely through one narrow bottleneck gives rise to attention in the next part, 3-8.
Figure symbols differ a little from book to book and paper to paper. Here we summarize this series’ conventions and look, in chronological order, at what problems sequence models solved as they developed.
| Symbol | Meaning | Feedforward | RNN | LSTM | GRU | Autoencoder |
|---|---|---|---|---|---|---|
| Box | One layer (weight multiply + activation function) | ✓ | ✓ | ✓ | ✓ | ✓ |
| → | Direction values flow | Forward only | Forward + next time step | Forward + next time step | Forward + next time step | Forward only |
| ↺ Loop | Receives the value from the previous time step again | — | h | h and c | h | — (sequence versions use RNNs) |
| σ | Doors (gates) that squash to 0–1 | — | — | 3 (f, i, o) | 2 (z, r) | — |
| tanh | Activation function that squashes to −1–1 | Optional | ✓ | Candidate g and output | Candidate h̃ | Optional |
| ⊙ · ⊕ | Element-wise product · addition | — | — | ✓ | ✓ | — |
| Hourglass | Layer sizes that narrow, then widen | — | — | — | — | ✓ |
Answer three questions and it picks a model to start from. It is a first candidate, not the answer. The final decision is always made by comparing on validation data (3-2, Section 9).
Both code examples use only libraries preinstalled in Colab. After "Download notebook (.ipynb)", open it in Colab with File → Upload notebook, or copy the code and paste it into a new notebook cell.
Computes the one-number RNN of Section 4 and the hand-set LSTM of Section 5 on the same sequence. The hidden states, the gradients passed backward, and the change when the first signal is flipped come out as the same numbers as on the page. To use your own data, change only SEQ.
Trains three models on sequences in which every step is ±1 and the answer is the first step. Lengths 10 · 50 · 100, 2 seeds. The table below Section 6 is built directly from this output.
These are prompts to paste into AI assistants such as ChatGPT, Qwen, or Claude. Pressing "Copy" attaches the chosen code after the prompt. Replace the parts in 〔 〕 with your own situation.
An AI's answer is only a starting point. Always check any papers, figures, or library functions it suggests against the original sources and official documentation. If you used AI help for a competition entry or paper, disclose the fact and extent of its use according to the rules of the organizers and the journal.
This is the same dictionary as the yellow dotted underlines in the text. The same terms are used throughout the series. Click a section name to jump to where the term first appears.
Analogies are a first step to understanding, but they can also seed misunderstanding. Fruit fly and brain analogies are marked by strength. Direct evidence that they actually do the same thing · Partial only some properties are similar · None no known counterpart
| Concept | Everyday analogy | Fruit fly / brain analogy | Where the analogy breaks down |
|---|---|---|---|
| Hidden state | The plot summary you build in your head as you read a book | Partial Sustained activity of groups of neurons | Which circuits maintain short-term memory in the brain differs by region and is still being studied |
| Unrolling · weight sharing | The same employee working by the same work rules every day | None | The brain does not make identical "copies" for each moment. Unrolling is a picture for computation |
| Vanishing gradient | The first words getting blurred in a game of telephone | None Backpropagation itself has not been confirmed in the brain | In telephone the forward-moving signal blurs, but a vanishing gradient is the learning signal going backward getting smaller |
| Cell state | A factory conveyor belt | None | Items on a belt travel unchanged, but the cell state shrinks a little at each step by the forget gate |
| Gate | A faucet (how far to open it, 0–1) | Partial Neuromodulators that regulate synaptic transmission (e.g. dopamine in the fruit fly mushroom body) | Dopamine is involved in learning (changes in connection strength). LSTM gates are computations that set the flow at each step, and are not learning in themselves |
| GRU’s update gate | A balance scale: the less old memory you keep, the more new content you hold | None | Unlike a real scale, it uses a different ratio at every step and for every number |
| Autoencoder bottleneck | Summarizing a long meeting in three lines, then writing it out again | Partial Counterexample: the mushroom body expands instead of narrowing | Unlike a human summary, an autoencoder does not let a person decide "what matters"; it decides only by the reconstruction error |
E-1 About the author · E-2 Academic references · E-3 Figure sources
A guide for instructors, on-the-job (OJT) trainers, and tutors. Times are estimates; adjust them to your actual class.
| Audience | University students and working professionals who have finished 3-2. Practitioners handling sensor, log, text and time-series data |
|---|---|
| Prerequisites | Backpropagation, vanishing gradients, sigmoid and tanh from 3-2. Knowing weight sharing from 3-3 makes Section 3 easy |
| Learning objectives | ① Explain, with two methods (averaging and concatenation), why feedforward networks struggle with order ② Show the RNN hidden state and vanishing gradients in numbers ③ Explain what the gates of LSTM and GRU do, and distinguish "the structure can hold memory" from "learning finds it" |
| Estimated time | Reading 45–55 min · animation demo 25 min · Colab practice 30 min (Code 1: 10 min, Code 2: 5 min run + 15 min interpretation) |
Intro 5 min (the same scene of Section 1) → Sections 2 and 3, 10 min → Section 4 vanishing gradients, 10 min → Section 5 flipping the first signal, 15 min → discussion of the experiment table below Section 6, 5 min → check questions, 5 min.
Classify team data with the Section 9 model picker, 15 min → Sections 4 and 5, 20 min → run and interpret Code 2, 30 min (how to report "it depends on the seed") → Section 7 anomaly-detection ideas, 15 min → prompt cards, 10 min.
Beginners: Sections 1, 3, 5 and 9 only. Intermediate learners: have them trace the c computation of Section 5 by hand for three steps, compare it with the lstm function in Code 1, and derive the parameter formula of Section 6 themselves.
| Section | Questions to pause and ask | Hands-on tips |
|---|---|---|
| 1 One frame | "Can you tell it is approaching from the third frame alone?" | Pause at "Current frame only," then switch to "Sequence" |
| 2 Feedforward | "If sentence length differs every time, what happens to concatenation?" | Toggle length 3 and 5 and watch the number of input slots change |
| 3 Loops & unrolling | "How many sets of weights do the five unrolled boxes have?" | Toggle between the folded view ↔ unrolled view |
| 4 RNN | "If increasing w_h keeps memory longer, why does learning get harder?" | Change from 0.9 → 1.5 and compare h12 and the gradient numbers |
| 5 LSTM | "If you flip the first signal, what happens to the memory 20 steps later?" | At b_f = 5, switch +1/−1, then change to b_f = 1 and see the difference |
| 6 GRU | "Why did LSTM and GRU, which have gates, fail at T = 100?" | Read seeds 0 and 1 of the experiment table side by side |
| 7 Autoencoder | "Why does the error suddenly drop when the bottleneck is 4?" | Increase the bottleneck from 1 → 4 watching the green curve, then switch to the mushroom body view |
| 8 Notation & history | "How does the 1997 LSTM differ from today’s LSTM?" | Click through the timeline to the 2000 forget gate |
| 9 Model picker | "Where does our team’s data fit?" | Have each participant choose answers, then discuss where the recommendations diverge |
| Misconception | Correct explanation | Scene to show |
|---|---|---|
| Using LSTM solves the long-memory problem | LSTM is a structure that can hold long-term memory. Whether learning finds those weights depends on the task, training time and initialization | Experiment table below Section 6 (T = 100) |
| SimpleRNN always forgets after just a few steps | Depending on initialization and the task, it can remember for quite long. In this experiment, both seeds succeeded up to T = 50 | Experiment table below Section 6 (T = 50) |
| LSTM had a forget gate from the start | It was not in the original 1997 paper; Gers et al. added it in 2000 | Section 8 timeline |
| GRU is always faster and better than LSTM | It only has fewer parameters, about 3/4; performance depends on the task. At T = 50 in this experiment, only GRU failed | Section 6 |
| Autoencoders imitate how the brain stores memory | It is a computational technique that summarizes by narrowing. The fruit fly mushroom body actually expands | Section 7 mushroom body view |
Order is lost. 100 → 110 → 120 (rising) and 120 → 110 → 100 (falling) both average to 110, so the network cannot tell them apart.
h_1 = tanh(1) ≈ 0.762, h_2 = tanh(0.9 × 0.762) = tanh(0.686) ≈ 0.595.
It is multiplied over 19 steps, so 0.9933 to the 19th power ≈ 0.880×. The actual value in Section 5 (0.988 → 0.864, about 0.875×) is slightly smaller because the input gate is not fully closed (0.007), so the distractors leak in little by little. This sequence’s distractors are ten −1s and nine +1s.
One set = 16 × (16 + 1) + 16 = 288. SimpleRNN 288, LSTM 4 × 288 = 1,152, GRU 3 × 288 = 864. Keras’s default GRU adds 48 biases, giving 912.
No. With properly set weights, as in Section 5, an LSTM can hold long-term memory. The failure means gradient descent did not find those weights within 30 epochs. You can draw a conclusion only after re-running the experiment with different training time, learning rate and amount of data, and repeating it with several seeds.
In the lstm function of Code 1, change the input gate to always stay open (i = σ(5)). Compute what happens to the cell state 20 steps later when the first signal is flipped, and write the role of the input gate in two sentences.
In Code 2, try at least one way to turn the T = 100 failure into a success (more epochs, a different learning rate, more data, etc.). Report the results in a table with 3 or more seeds, and write with evidence what worked.
| Beginners | Skipping the gradient graph of Section 4 and Sections 7 and 8 does not break the flow. Just make sure to show "flipping the first signal" in Section 5 |
|---|---|
| Advanced | Generalize the gradient product formula of Section 4 to multiple hidden units (matrices), and discuss the relationship between eigenvalues and vanishing/exploding gradients [R2] |
| Further learning | Sangsun Ahn, “Data Science for Business” lecture notes (data science in general) · STEP [K-Digital] AI Natural Language Processing (linked to 3-5 and 3-8) · STEP [K-Digital] Fundamentals of Fintech Data Analysis (linked to 3-6) |
| Materials for instructors | Grading criteria for the check questions and sample answers for the assignments are on the answer key page for educators (3-7) |
You’ve read “3-7 Sequence Models” all the way through. The lab members each have a word to sum it up.