Coding Hubs School of AI – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 8448811540
Back to Deep Learning Notes
Topic #415

LSTM Equations

This note assembles every equation from the previous five notes into one complete, unified reference — the full LSTM cell, all six formulas together, exactly as you'd need to recall them for an exam or an interview.

The Complete Set of Equations

\[ \mathbf{f}_t = \sigma(\mathbf{W}_f[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_f) \qquad \text{(forget gate)} \] \[ \mathbf{i}_t = \sigma(\mathbf{W}_i[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_i) \qquad \text{(input gate)} \] \[ \tilde{\mathbf{C}}_t = \tanh(\mathbf{W}_C[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_C) \qquad \text{(candidate state)} \] \[ \mathbf{C}_t = \mathbf{f}_t \odot \mathbf{C}_{t-1} + \mathbf{i}_t \odot \tilde{\mathbf{C}}_t \qquad \text{(cell state update)} \] \[ \mathbf{o}_t = \sigma(\mathbf{W}_o[\mathbf{h}_{t-1},\mathbf{x}_t]+\mathbf{b}_o) \qquad \text{(output gate)} \] \[ \mathbf{h}_t = \mathbf{o}_t \odot \tanh(\mathbf{C}_t) \qquad \text{(hidden state)} \]

The Full Cell, Diagrammed Together

C_{t-1} C_t × f_t + i_t × C̃_t [h_{t-1}, x_t] → f_t (σ), i_t (σ), C̃_t (tanh), o_t (σ) 4 independent linear layers, sharing the same input × tanh(C_t) × o_t = h_t

Every piece from the previous five notes, assembled into the complete LSTM cell.

A Quick-Reference Summary Table

SymbolNameActivationPurpose
\(\mathbf{f}_t\)Forget gateSigmoidHow much old cell state to keep
\(\mathbf{i}_t\)Input gateSigmoidHow much new candidate to add
\(\tilde{\mathbf{C}}_t\)Candidate stateTanhWhat new content to potentially add
\(\mathbf{C}_t\)Cell state—The long-term memory pathway itself
\(\mathbf{o}_t\)Output gateSigmoidHow much cell state to expose
\(\mathbf{h}_t\)Hidden state—This step's working output

Code — All Six Equations, End to End

import torch

def lstm_cell_manual(x_t, h_prev, C_prev, weights):
    combined = torch.cat([h_prev, x_t])
    f_t = torch.sigmoid(weights['W_f'] @ combined + weights['b_f'])
    i_t = torch.sigmoid(weights['W_i'] @ combined + weights['b_i'])
    C_candidate = torch.tanh(weights['W_C'] @ combined + weights['b_C'])
    C_t = f_t * C_prev + i_t * C_candidate
    o_t = torch.sigmoid(weights['W_o'] @ combined + weights['b_o'])
    h_t = o_t * torch.tanh(C_t)
    return h_t, C_t

Common Mistakes

  • Mixing up which activation belongs to which equation under exam/interview pressure — the reliable rule: every gate (forget, input, output) uses sigmoid; the candidate state and the final hidden-state computation involve tanh.
  • Forgetting that all four linear layers (\(f_t, i_t, \tilde C_t, o_t\)) take the same input, \([\mathbf{h}_{t-1}, \mathbf{x}_t]\), just with four independent sets of weights — this is exactly why PyTorch packs all four into one combined weight tensor internally, as noted in LSTM Architecture.

Interview Relevance

Q: "Write out all six LSTM equations from memory." This exact exercise — reproducing the forget gate, input gate, candidate state, cell state update, output gate, and hidden state formulas in order, correctly matching sigmoid to the three gates and tanh to the candidate and final hidden-state computation — is one of the most common whiteboard questions for sequence-modeling-focused ML/DL interviews.

Practice Question

Without looking back at the formulas, write out the cell state update equation and explain, in one sentence each, what \(\mathbf{f}_t\) and \(\mathbf{i}_t\) each control within it.

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →