๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #225

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.

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

LSTM Equations โ€“ FAQs

Quick answers about learning LSTM Equations in Deep Learning.

This free note from CodingNow 2.0 explains LSTM Equations in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including LSTM Equations, is 100% free with no signup required.
With focused practice, most students grasp LSTM Equations in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now