LSTM: Why It Was Born, How It Fixes RNN, and Why It Changed Sequence Learning
From the publisher’s feed, collected in the ZeroLabs newsroom.
Sequence data is messy. Words depend on earlier words, notes depend on earlier notes, signals depend on earlier signals, and the past has this annoying habit of refusing to stay irrelevant. That is exactly why recurrent models exist in the first place.But vanilla RNNs break in a very specific way. They are built for memory, yet they are bad at remembering. That contradiction is the entire reason LSTM exists.(Image Credit)1. Why RNN Was Not EnoughThe goal of an RNN was straightforward: handle sequential data where previous information matters.Common examples include:language modelingspeech recognitiontime series forecastingtranslationchat-like sequence predictionIn theory, an RNN should remember all previous information.Consider this sentence:“The boy who lives next door is tall.”To predict “is tall”, the model must remember “boy”, which appeared many words earlier.That is the core job of a sequence model. RNN should do it.But in practice, it fails.2. Where RNN Fails: The Real ProblemThe fundamental issue with a vanilla RNN is simple:It cannot remember long-term dependencies.Why this mattersImagine reading:“I grew up in France… so I speak fluent ______.”You instantly know the answer is French.Why? Because your brain keeps track of information from far back in the sentence.RNN is supposed to do the same thing.3. How RNN Should WorkAt first glance, RNN seems perfect for sequence data:it reads words one by oneit keeps a hidden state as memoryit passes that memory forward through the sequenceSo theoretically:RNN should remember everything from the past.That is the promise.The problem is that the promise collapses under training.4. What Actually Happens: The FailureDuring training, RNNs use Backpropagation Through Time (BPTT).That means:errors flow backward through each time stepgradients are multiplied again and again across the sequenceAnd here is the real problem:Gradients shrink exponentially over time.Mathematically, the gradient through time behaves like a repeated product:In the simplified case:if values are less than 1, gradients shrink → vanishif values are greater than 1, gradients grow → explodeThink of it like this:recent words have strong influenceold words have almost zero influenceWhy?Because activation functions like sigmoid and tanh produce small derivatives, and repeated multiplication makes the gradient nearly zero.Example:So the signal from earlier words dies before it reaches the output.That is why the model becomes biased toward the most recent inputs.6. What This Means in PracticeA vanilla RNN behaves like someone with short-term memory only:it remembers the last few wordsit forgets important context from earlierSo in the sentence:“I grew up in France… so I speak fluent ______.”the model may remember “so I speak fluent” but forget “France”.That may leads to the wrong prediction.7. The Core Problem, Stated ClearlyRNN cannot learn long-term dependencies because gradients vanish over time.That is the vanishing gradient problem.Because of this:RNN focuses mostly on recent inputslong-range relationships are lostearly tokens contribute almost nothing to learningThis is why RNNs work reasonably well for short sequences, but begin to break down on longer ones.8. Why Exactly This HappensThere are two main reasons:activation functions like tanh and sigmoid compress values into small rangesrepeated multiplication through many time steps reduces the gradient toward zeroSo early time steps receive almost no learning signal.That is why:recent tokens affect the output moredistant tokens barely matter9. The Real Question Researchers AskedResearchers asked something much better than “how do we make an RNN slightly less terrible?”They asked:Can we build a neural network that remembers important information for a long time and forgets useless information?That question matters because human memory works that way.We remember:important factskey eventsBut forget:noiseirrelevant detailsThat insight led to Long Short-Term Memory (LSTM).10. The Turning PointResearchers realized:The real problem was not sequence modeling itself.The real problem was memory retention.So the question changed from:“Can we process sequences?”to:“Can we build a network that decides what to remember and what to forget?”That question directly led to LSTM.11. Birth of LSTM (1997)Key paperHochreiter, S., & Schmidhuber, J. (1997). Long Short-Term Memory. Neural Computation, 9(8), 1735–1780.Core ideaInstead of letting gradients vanish, force constant error flow.That is the entire point of the architecture.How LSTM Fixed Memory: The Real Innovation12. The Basic Idea of LSTMLSTM separates memory into two ideas:Short Term Context (STC)Long Term Context (LTC)Key conceptimportant information from STC → moves to LTCif something becomes not important later → removed from LTCThat is the core design philosophy.13. The Key Idea of LSTMLSTM introduces a new concept:Memory Cell (Cell State)Think of it as:a conveyor belt running through time.Information can travel through this cell almost unchanged.This solves the gradient issue because:gradients flow through additive updatesnot repeated multiplicationsSo they do not vanish easily.14. Problem with RNNIn RNN:there is only one path to maintain memory/statethis path tries to store both STC and LTCReality:it is not possible to store both effectively in the same pathSTC dominates LTCresult → long-term dependencies are lostThat is the core problem.15. Solution Idea (LSTM)LSTM introduces another path across timesteps.This path specifically carries LTC (cell state).Important information:stored earlypreserved till later timestepsunless explicitly removedThat is the innovation.16. Enter LSTM: A Memory System, Not Just a NetworkInstead of a single hidden state like an RNN, LSTM introduces something more powerful:A Cell State (Long-Term Memory)Think of it like a highway running through time.information flows almost unchangedmemory can persist across long sequencesstable memory is easier to maintainThis is the central structural advantage.17. The Highway AnalogyImagine:RNN = narrow road → information gets lostLSTM = highway → information flows smoothlyBut here is the twist:Not everything should stay on the highway.So LSTM adds control systems.18. Two Types of MemoryLSTM has two kinds of memory:Long Term Memory → Cell State (Cₜ)Short Term Memory → Hidden State (hₜ)This distinction matters.the cell state stores memory across timethe hidden state exposes what is needed now19. Interaction at Time (t)Inputs:previous cell state → (Cₜ₋₁)previous hidden state → (hₜ₋₁)current input → (xₜ)Outputs:current cell state → (Cₜ)current hidden state → (hₜ)So, each timestep is not just “output and move on”; it is a controlled memory update.20. Main OperationsAt each timestep, LSTM does two main things:Update the cell state:Cₜ₋₁ → CₜCompute the hidden state:hₜThat is the entire recurrent step, but with memory control built in.21. Decision LogicBased on current input (xₜ), LSTM decides:what to remove from long-term memorywhat to add to long-term memoryThis is why it works better than a vanilla RNN, which blindly keeps pushing memory forward.22. The Secret Weapon: GatesLSTM introduced gates.These are small decision makers that ask:“Should I allow this information or not?”They output values between 0 and 1:0 → block completely1 → allow fullyvalues in between → partial flowThree gates exist:Forget GateInput GateOutput GateThese gates act like decision systems.23. The Three Gates That Control MemoryForget GateQuestion it answers:What should we forget from the past?Input GateQuestion it answers:What new information should be stored?Output GateQuestion it answers:What part of memory should influence the output?Together, these gates define the LSTM.Detailed LSTM Architecture24. Forget GatePurposeRemove unnecessary info from the cell state.ComputationInput:hₜ₋₁xₜConcatenate:[hₜ₋₁, xₜ]Then:fₜ = σ( [hₜ₋₁, xₜ]W_f + b_f )Dimensions exampleIf:(xₜ) is 4-dimensionalhidden state has 3 unitThen concatenated input has dimension:4 + 3 = 7So:Weight matrix → (7 × 3)Bias → (3)Output (fₜ) → (1 × 3)Cell update for the forget partCₜ₋₁ ⊙ fₜBecause fₜ ∈ (0,1), each dimension of Cₜ₋₁ is scaled individually:if fₜ ≈ 1 → keepif fₜ ≈ 0 → forgetotherwise → partially reduceInterpretationThe forget gate controls:“How much of each memory dimension should pass?”The decision is based on:Current input: xₜPrevious hidden state: hₜ₋₁25. Input GatePurposeAdd new important information to the cell state.ComputationGate decision:iₜ = σ( [hₜ₋₁, xₜ] W_i + b_i )Candidate memory:C̃ₜ = tanh( [hₜ₋₁, xₜ] W_c + b_c )Dimensions example[hₜ₋₁, xₜ] → (1 × 7)W_i → (7 × 3)b_i → (1 × 3)iₜ → (1 × 3)C̃ₜ → (1 × 3)InterpretationCandidate = “what can be added”Input gate = “what should actually be added”MeaningThe input gate controls how much of (C̃ₜ) is added.So:candidate cell state = potential informationinput gate = filter deciding what enters the memory26. Cell State UpdateNow the key update step:Cₜ = (Cₜ₋₁ ⊙ fₜ) + (iₜ ⊙ C̃ₜ)Breakdown1. First term: Cₜ₋₁ ⊙ fₜOld memory is filtered. This means some useless data is forgotten.2. Second term: iₜ ⊙ C̃ₜNew useful information is added. This is the current input, filtered by relevance.DimensionsCₜ → (1 × 3)Why this mattersThis additive update is the key reason gradients survive longer.Instead of repeatedly multiplying and shrinking signal strength, LSTM adds information to a stable memory path.27. Why LSTM Works Mathematically (Full Derivation)From the cell update:Cₜ = fₜ ⊙ Cₜ₋₁ + iₜ ⊙ C̃ₜIn the simplified derivation, the direct path from Cₜ₋₁ to Cₜ is controlled by fₜ, so:This means:if fₜ ≈ 1, memory and gradients are preservedif fₜ ≈ 0, memory is forgottenExtending Across TimeSo the cell state provides a stable, controlled path for long-term information.By contrast, in RNN:and each factor includes repeated matrix multiplication and activation derivatives, which often shrink toward zero.Gradient Flow through hidden state:hₜ = oₜ ⊙ tanh(Cₜ)So:∂hₜ / ∂Cₜ = oₜ ⊙ (1 − tanh²(Cₜ))This is why the output is bounded and why gradients remain more stable.So LSTM separates gradient flow into two paths:a nonlinear exposed path through hₜa stable memory path through Cₜ28. Key Insight: Vanishing Gradient Problem— In RNN:vanishing gradients cause information loss at each steplong timesteps become disastrous— In LSTM:possible to have very small information lossbecause the cell state supports stable propagationImportant notes:For longer timesteps, the information loss is negligible.Special cases— Case 1If:fₜ = 1Then:Cₜ = Cₜ₋₁ + iₜ ⊙ C̃ₜMeaning: no information lost— Case 2If:iₜ = 0Then:Cₜ = Cₜ₋₁ ⊙ fₜMeaning: no new information addedThis is exactly the kind of controllable memory behavior RNN lacks.29. Long Sequence InsightLSTM can preserve long-term context from start to end.That makes it useful for:long sentencesdelayed dependenciessequences where early tokens matter much laterThis is one of the most important conceptual advantages of the architecture.30. Output GateComputationoₜ = σ( [hₜ₋₁, xₜ] W_o + b_o )Dimensions example[hₜ₋₁, xₜ] → (1 × 7)W_o → (7 × 3)b_o → (1 × 3)oₜ → (1 × 3)Hidden statehₜ = oₜ ⊙ tanh(Cₜ)What this doesApply tanh to Cₜ → values in (-1, 1)Apply filtering using oₜInterpretationThe output gate decides:“Value for current hidden state (hₜ)”This becomes the output passed to the next timestep.Why Apply (tanh) Before the Output Gate Multiplies?Why not directly use: hₜ = oₜ ⊙ Cₜ31. Hidden State in LSTMThe hidden state in LSTM is:hₜ = oₜ ⊙ tanh(Cₜ)Short answerWe apply tanh to control scale and stabilize the output. Otherwise, the hidden state can explode and training becomes unstable.Deep intuitionWhat is (Cₜ)?it is the cell stateit keeps accumulating information over timeImportant point:(Cₜ) is unbounded and can grow very large.That is fine for memory storage, but terrible for direct exposure as output.Problem if you use (Cₜ) directlyIf you do: hₜ = oₜ ⊙ CₜThen:if (Cₜ = 50), the output becomes hugeif (Cₜ= -100), the output becomes extremethis propagates forward and makes the network unstableThat leads to:exploding activationsunstable gradientspoor trainingWhy tanh(Cₜ)?Because tanh does this:tanh(x) ∈ (-1, 1)So no matter how large (Cₜ) gets:the output is always controlledIntuitionThink of it like this:Cₜ = raw memory (can be messy, large, and unfiltered)tanh = compress and normalize memoryoₜ = decide what to showSo the actual flow is:memory stored → ( Cₜ )normalize → ( tanh(Cₜ) )filter → multiply with ( oₜ )Why not let the output gate handle everything?The sigmoid output gate ( oₜ ) only scales from 0 to 1.It cannot normalize large values.Example:(Cₜ = 100)(oₜ = 0.9)Then: hₜ = 0.9 · 100 = 90That is still huge.So:gate controls how muchtanh controls how bigThey do different jobs.(tanh) ensures the hidden state remains bounded, while the output gate decides how much of that bounded information to expose.AnalogyThink of ( Cₜ ) as raw water pressure in a pipe.( tanh ) acts like a regulator that keeps pressure safe.The output gate is the tap deciding how much water to release.Final insightThis also helps with:smoother gradientsbetter generalizationstable training over long sequencesPutting It All Together32. Summary of FlowAt each timestep:— Forget gateremoves irrelevant past info— Input gateadds new important info— Cell state updatecombines old filtered memory with new filtered information— Output gateproduces hidden state33. Full LSTM FlowStep by step:Candidate stateC̃ₜ = tanh( [hₜ₋₁, xₜ] W_c + b_c )Input gateiₜ = σ( [hₜ₋₁, xₜ] W_i + b_i )Forget gatefₜ = σ( [hₜ₋₁, xₜ] W_f + b_f )Cell updateCₜ = ( Cₜ₋₁ ⊙ fₜ ) + ( iₜ ⊙ C̃ₜ )Output gateoₜ = σ( [hₜ₋₁, xₜ] W_o + b_o )Hidden statehₜ = oₜ ⊙ tanh(Cₜ)34. Dimensional SummaryComponent | Shape-------------------------|----------xₜ | (1 × 4)hₜ₋₁ | (1 × 3)Concat [hₜ₋₁, xₜ] | (1 × 7)Weights (W) | (7 × 3)Outputs (gates/cand.) | (1 × 3)35. LSTM Diagram Explained Step-by-Step(Image Credit)------------------------------------> Cell State (C_t)👉 This is the Cell State (memory highway)36. Step 1: Forget GateIt takes two inputs:previous hidden state (hₜ₋₁)current input (xₜ)h(t-1) ----\ > [Forget Gate] ---> (multiplies with C(t-1))x(t) ------/What it meansThe forget gate decides what past information should be removed from memory.It produces a value between 0 and 1, and multiplies it with the previous memory:Cₜ₋₁ ⊙ fₜSo:0 means forget completely1 means keep completely36. Step 2: Input GateThis part has two pieces:Input Gate using sigmoidCandidate Memory using tanhh(t-1), x(t) → [Input Gate]h(t-1), x(t) → [Candidate Values]Then they are combined.What it meansThe input gate decides what new information should be added.So:Candidate (C̃ₜ) = possible new memoryGate (iₜ) = how much of it to allowThe result is:iₜ ⊙ C̃ₜ37. Step 3: Update Cell StateNow combine the old memory and the new memory.This is the most important part of LSTM:Cₜ = (fₜ ⊙ Cₜ₋₁) + (iₜ ⊙ C̃ₜ)Memory is updated by:removing old irrelevant informationadding new useful informationThis is why LSTM works:no repeated shrinking multiplicationsmooth information flowlong-term memory stays alive38. Step 4: Output Gateh(t-1), x(t) → [Output Gate] C_t → tanh → × Output Gate → h(t)What it meansThe output gate decides what part of memory to use right now.The final output is:hₜ = oₜ × tanh(Cₜ)So, the cell state stores memory, and the output gate decides what gets exposed.39. Final Full Diagram LogicWhen you combine everything:Left → forget old memoryMiddle → add new memory, update cell stateRight → produce outputAnd the cell state flows straight across like a highway.That horizontal path is the reason LSTM remembers long-term. The gates just control the traffic.40. Super Simple AnalogyThink of LSTM like a smart notebook:✂️ Forget Gate → erase useless notes✍️ Input Gate → write important things📖 Output Gate → read only what is needed41. Intuition in One LineRNN = “Try to remember everything” ❌LSTM = “Remember smartly” ✅42. Why LSTM Solves RNN ProblemsMain innovations:a. Memory CellStores long-term information.b. Gating MechanismControls information flow.c. Additive State UpdatesAvoids exponential gradient decay.d. Constant Error FlowGradients can propagate through many timesteps.Because of this:LSTM can learn dependencies across hundreds or thousands of steps43. Training ProcessStepsForward pass through sequenceCompute lossBackpropagation Through Time (BPTT)Update weights using gradient descentComplexityTime: O(T · n²)Memory: high, because all states must be stored44. Improvements Over TimeLSTM did not remain the only gated recurrent design. Researchers pushed it further.GRU (Cho et al., 2014)simpler version of LSTMfewer gatesoften faster and easier to trainPeephole LSTMgates can see the cell stateadds extra access to memory informationBidirectional LSTMuses past and future contextuseful when the full sequence is availableAttention Mechanismlater reduced reliance on LSTMimproved long-range dependency handling45. Real Applications of LSTMBefore Transformers, LSTM dominated sequence learning.Examples:Google Translatespeech recognitionhandwriting recognitiontime series predictionNLP tasks like translation and chatbotsstock predictionhealthcare time seriesvideo processing46. Limitations of LSTMLSTM is powerful, but it is not magic.It still has limits:slow traininghard to parallelizememory intensivesequential computationdifficulty with very long sequencesreplaced by Transformers in many tasks47. Evolution After LSTMModel progression:RNN → LSTM → GRU → Attention → TransformersBecause even LSTM has limitations, which led to:Attention is All You Need (Transformer).48. Key Academic SourcesHere are referenced foundational and influential papers:Hochreiter, S., & Schmidhuber, J. (1997). Long Short-Term Memory. Neural Computation, 9(8), 1735–1780.Bengio, Y., Simard, P., & Frasconi, P. (1994). Learning long-term dependencies with gradient descent. IEEE Trans. Neural Networks.Hochreiter, S. (1991). Untersuchungen zu dynamischen neuronalen Netzen.Gers, F. A., Schmidhuber, J., & Cummins, F. (2000). Learning to forget: Continual prediction with LSTM. Neural Computation.Graves, A. (2012). Supervised Sequence Labelling with RNNs.Graves, A., Mohamed, A., & Hinton, G. (2013). Speech recognition with deep RNNs.Cho, K. et al. (2014). Learning Phrase Representations using RNN Encoder–Decoder.Greff, K. et al. (2017). LSTM: A Search Space Odyssey.Olah, C. (2015). Understanding LSTM Networks.Sak, H., Senior, A., & Beaufays, F. (2014). Long short-term memory recurrent neural network architectures for large scale acoustic modeling.49. FinallyLSTM did not just improve RNN.It changed the idea of memory in neural networks.Instead of forcing the model to remember everything,it taught the model how to remember.RNN failed because gradients vanished over time, making it forget long-term dependencies.LSTM was born to solve that by using a cell state and gates to control what to remember, what to forget, and what to use.50. About the AuthorAlok Ranjan Singh is a machine learning enthusiast and writer who enjoys breaking down deep learning ideas into clear, intuitive explanations. This article reflects an ongoing effort to study sequence models, gradient flow, and the mechanics behind memory in neural networks.Connect with me on LinkedIn for discussions:🔗 LinkedIn Profile LinkLSTM: Why It Was Born, How It Fixes RNN, and Why It Changed Sequence Learning was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.