5.6.13Machine Learning (Aerospace Applications)

LSTM — gates, cell state

1,762 words8 min readdifficulty · medium1 backlinks

WHAT is an LSTM?

WHY do we need it? In aerospace we predict things over long horizons — e.g. estimating fatigue accumulation in a wing spar from a long vibration history, or predicting remaining useful life (RUL) of a turbine blade. The relevant event (a micro-crack initiation) may have happened thousands of timesteps ago. A plain RNN forgets it; an LSTM can hold it in ctc_t.


HOW it works — the four computations

At each timestep we concatenate the previous output and current input: [ht1,xt][h_{t-1}, x_t]. All gates are affine maps of this vector passed through a squashing function.

Why sigmoid for gates? σ(0,1)\sigma\in(0,1) acts like a valve opening fraction: 00 = fully closed (block), 11 = fully open (pass). Why tanh for candidate? tanh(1,1)\tanh\in(-1,1) lets the written value be positive or negative, centered at zero — good for signed features.


DERIVATION — why the cell update kills vanishing gradients

Let's derive from scratch why ct=ftct1+c_t = f_t\odot c_{t-1} + \dots helps gradients survive.

Step 1 — the RNN problem. In a plain RNN, ht=tanh(Wht1+)h_t = \tanh(W h_{t-1}+\dots). Backprop through kk steps multiplies kk Jacobians: hthtk=j=1kdiag(tanh)W.\frac{\partial h_t}{\partial h_{t-k}} = \prod_{j=1}^{k} \operatorname{diag}(\tanh')\,W. Why this step? Chain rule across timesteps. Each factor has tanh1|\tanh'|\le 1 and W\|W\| often <1<1, so the product 0\to 0 exponentially — gradients vanish.

Step 2 — the LSTM cell path. Take the derivative of the cell recurrence along the cc line only: ctct1=ft.\frac{\partial c_t}{\partial c_{t-1}} = f_t. Why this step? In ct=ftct1+itc~tc_t = f_t\odot c_{t-1} + i_t\odot\tilde c_t, treating other terms as inputs, the direct partial is just ftf_tno weight matrix, no tanh', just an elementwise gate.

Step 3 — chain along the cell path. ctctk=j=tk+1tfj.\frac{\partial c_t}{\partial c_{t-k}} = \prod_{j=t-k+1}^{t} f_j. Why this matters: if the forget gate stays near 11 (network chooses to remember), the product stays near 11 — the gradient flows back undiminished. The LSTM can learn to keep the highway open. That's the constant error carousel.


Figure — LSTM — gates, cell state

Worked Example 1 — one scalar timestep

Take scalar states. Given ct1=0.5c_{t-1}=0.5, and pre-activations chosen so that ft=0.9,  it=0.8,  ot=0.6,  c~t=0.4f_t=0.9,\; i_t=0.8,\; o_t=0.6,\; \tilde c_t=0.4.

  • Cell: ct=ftct1+itc~t=0.9(0.5)+0.8(0.4)=0.45+0.32=0.77c_t = f_t c_{t-1} + i_t \tilde c_t = 0.9(0.5)+0.8(0.4)=0.45+0.32=0.77. Why? Keep 90% of old memory, add 80% of the new candidate.
  • Output: ht=ottanh(ct)=0.6tanh(0.77)=0.6(0.6469)=0.388h_t = o_t\tanh(c_t)=0.6\tanh(0.77)=0.6(0.6469)=0.388. Why? Read out 60% of the (squashed) cell content.

Worked Example 2 — "remember forever"

Suppose the task needs to store a value seen at t=0t=0 for 1000 steps. If the network sets ft=1,  it=0f_t=1,\; i_t=0 for all t>0t>0, then ct=1ct1+0=ct1c1000=c0.c_t = 1\cdot c_{t-1} + 0 = c_{t-1} \Rightarrow c_{1000}=c_0. Why this step? With forget gate wide open and input gate shut, the memory is copied perfectly — a plain RNN literally cannot do this without exploding/vanishing.

Worked Example 3 — aerospace framing

Vibration sensor stream xtx_t feeds an LSTM predicting bearing RUL. A rare high-amplitude spike at t=200t=200 should influence the prediction at t=5000t=5000. Training pushes i2001i_{200}\approx 1 (write the spike into cc), then ft1f_t\approx 1 afterwards (hold it), and o50001o_{5000}\approx 1 (read it out when forecasting). Why? The gates are learned data-dependent switches — gradient descent discovers this routing automatically.


Common Mistakes


Flashcards

What two states does an LSTM carry between timesteps?
The cell state ctc_t (long-term) and hidden state hth_t (short-term / output).
Write the cell-state update equation.
ct=ftct1+itc~tc_t = f_t\odot c_{t-1} + i_t\odot\tilde c_t.
What is ct/ct1\partial c_t/\partial c_{t-1} and why does it matter?
It equals ftf_t; with no weight matrix or tanh' it lets gradients flow undiminished when ft1f_t\approx 1 (constant error carousel).
Why sigmoid for gates but tanh for the candidate?
Sigmoid (0,1)(0,1) acts as a fractional valve; tanh (1,1)(-1,1) allows signed, zero-centered candidate values.
What does the output gate do?
ht=ottanh(ct)h_t = o_t\odot\tanh(c_t) — controls how much of the (squashed) cell state is read out as the hidden/output vector.
Why initialize the forget-gate bias positive?
So ft1f_t\approx1 initially → the network remembers by default and gradients survive early training.
In a plain RNN, why do gradients vanish?
ht/htk=diag(tanh)W\partial h_t/\partial h_{t-k}=\prod \operatorname{diag}(\tanh')W; factors 1\le1 multiply to 00 exponentially.
Setting ft=1,it=0f_t=1,i_t=0 gives what behaviour?
ct=ct1c_t=c_{t-1} — the memory is copied perfectly, storing a value indefinitely.

Recall Feynman: explain to a 12-year-old

Imagine a toy train on a track carrying a note. At each station there are three gatekeepers with dials. Gatekeeper 1 (forget) decides how much of the old note to erase. Gatekeeper 2 (input) decides how much of a new note to add. Gatekeeper 3 (output) decides how much of the note to read aloud right now. Because the train just carries the note along the track (adding, not rewriting from scratch), it can keep an important message for a very long time — that's why the LSTM remembers things a normal robot brain forgets.


Connections

Concept Map

suffers from

overwrites

adds

conveyor belt avoids

maintains

feeds

forget f_t

input i_t

written via i_t

via output o_t

derivative equals f_t

enables

Vanilla RNN

Vanishing gradient

Hidden state h_t

LSTM unit

Cell state c_t

Concat h_t-1 and x_t

Sigmoid gates

Cell update c_t

Candidate tanh c_t

Gradient flow preserved

Long-horizon RUL and fatigue

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, normal RNN ek hi memory rakhta hai jise har step pe overwrite kar deta hai — isliye purani important baat bahut jaldi "smear" ho jaati hai, aur gradients vanish ho jaate hain. LSTM ka jugaad simple hai: usne ek alag cell state ctc_t banaya, ek tarah ki conveyor belt / highway, jispe information seedhi flow karti hai bina baar-baar multiply hue. Isko control karte hain teen chhote gates — forget (ff), input (ii), aur output (oo) — jo sigmoid se aate hain, matlab 00 se 11 tak ki "valve opening" hoti hai.

Core formula yaad rakho: ct=ftct1+itc~tc_t = f_t\odot c_{t-1} + i_t\odot\tilde c_t, aur output ht=ottanh(ct)h_t = o_t\odot\tanh(c_t). Yaani forget gate decide karta hai purani memory ka kitna hissa rakhna hai, input gate naye candidate c~t\tilde c_t (jo tanh se signed value deta hai) ka kitna add karna hai, aur output gate batata hai ki abhi kitna "padh ke bahar" nikaalna hai. FICO — Forget, Input, Candidate, Output — yaad rakhna easy hai.

Gradient wali beauty yeh hai: ct/ct1=ft\partial c_t/\partial c_{t-1} = f_t, koi weight matrix ya tanh' nahi. Agar network ft1f_t\approx 1 rakhe to gradient bina ghata hue back propagate hota hai — isko constant error carousel kehte hain. Isliye LSTM 1000 steps purani baat bhi yaad rakh sakta hai.

Aerospace mein yeh gold hai: turbine blade ya bearing ka Remaining Useful Life predict karna ho, ya wing vibration se fatigue estimate karna ho — koi critical spike 200th step pe aaya jo 5000th step ki prediction ko affect karta hai, LSTM khud seekh leta hai ki us spike ko ctc_t mein store kar ke hold karo. Ek chhoti si galti se bachna: forget gate ka bias positive initialize karo (bf1b_f\approx1) taaki shuru mein network default se yaad rakhe, warna sab bhoolta rahega.

Go deeper — visual, from zero

Test yourself — Machine Learning (Aerospace Applications)

Connections