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

LSTM Forward Pass

This note runs a complete LSTM forward pass, for one full time step, entirely by hand with real numbers โ€” assembling every equation from LSTM Equations into one continuous worked example, then verifying every value against PyTorch.

The Setup

\[ \mathbf{h}_{t-1} = [0.1, 0.2], \qquad \mathbf{C}_{t-1} = [0.5, -0.3], \qquad x_t = 1.0 \]

Combined input for every gate: \([\mathbf{h}_{t-1}, x_t] = [0.1, 0.2, 1.0]\). For simplicity, this example uses scalar weight rows shared identically for both cell-state dimensions (a genuine LSTM would have independent weights per dimension โ€” this simplification keeps the hand-computation tractable while preserving the exact mechanism).

\[ \mathbf{W}_f = [0.4, -0.2, 0.3], b_f=0.1 \quad \mathbf{W}_i = [0.2, 0.5, -0.1], b_i=0 \quad \mathbf{W}_C = [-0.3, 0.4, 0.6], b_C=0.2 \quad \mathbf{W}_o = [0.5, 0.1, -0.2], b_o=-0.1 \]

Step 1 โ€” Forget Gate

\[ z_f = 0.4(0.1)-0.2(0.2)+0.3(1.0)+0.1 = 0.04-0.04+0.3+0.1 = 0.40 \] \[ f_t = \sigma(0.40) \approx 0.5987 \]

Step 2 โ€” Input Gate

\[ z_i = 0.2(0.1)+0.5(0.2)-0.1(1.0)+0 = 0.02+0.10-0.10 = 0.02 \] \[ i_t = \sigma(0.02) \approx 0.5050 \]

Step 3 โ€” Candidate State

\[ z_C = -0.3(0.1)+0.4(0.2)+0.6(1.0)+0.2 = -0.03+0.08+0.6+0.2 = 0.85 \] \[ \tilde C_t = \tanh(0.85) \approx 0.6911 \]

Step 4 โ€” Cell State Update (applied per dimension of \(\mathbf{C}_{t-1}\))

\[ C_{t,1} = f_t \cdot C_{t-1,1} + i_t \cdot \tilde C_t = (0.5987)(0.5)+(0.5050)(0.6911) \approx 0.2994+0.3490 = 0.6484 \] \[ C_{t,2} = f_t \cdot C_{t-1,2} + i_t \cdot \tilde C_t = (0.5987)(-0.3)+(0.5050)(0.6911) \approx -0.1796+0.3490 = 0.1694 \] \[ \mathbf{C}_t \approx [0.6484,\ 0.1694] \]

Step 5 โ€” Output Gate

\[ z_o = 0.5(0.1)+0.1(0.2)-0.2(1.0)-0.1 = 0.05+0.02-0.2-0.1 = -0.23 \] \[ o_t = \sigma(-0.23) \approx 0.4427 \]

Step 6 โ€” Hidden State

\[ h_{t,1} = 0.4427 \times \tanh(0.6484) \approx 0.4427 \times 0.5707 \approx 0.2527 \] \[ h_{t,2} = 0.4427 \times \tanh(0.1694) \approx 0.4427 \times 0.1679 \approx 0.0743 \] \[ \mathbf{h}_t \approx [0.2527,\ 0.0743] \]

Code โ€” Verifying Every Value

import torch

h_prev = torch.tensor([0.1, 0.2])
C_prev = torch.tensor([0.5, -0.3])
x_t = torch.tensor([1.0])
combined = torch.cat([h_prev, x_t])

W_f, b_f = torch.tensor([0.4, -0.2, 0.3]), torch.tensor(0.1)
W_i, b_i = torch.tensor([0.2, 0.5, -0.1]), torch.tensor(0.0)
W_C, b_C = torch.tensor([-0.3, 0.4, 0.6]), torch.tensor(0.2)
W_o, b_o = torch.tensor([0.5, 0.1, -0.2]), torch.tensor(-0.1)

f_t = torch.sigmoid(torch.dot(W_f, combined) + b_f)
i_t = torch.sigmoid(torch.dot(W_i, combined) + b_i)
C_candidate = torch.tanh(torch.dot(W_C, combined) + b_C)
C_t = f_t * C_prev + i_t * C_candidate
o_t = torch.sigmoid(torch.dot(W_o, combined) + b_o)
h_t = o_t * torch.tanh(C_t)

print("f_t:", f_t.item())          # 0.5987
print("i_t:", i_t.item())          # 0.5050
print("C_candidate:", C_candidate.item())   # 0.6911
print("C_t:", C_t)                 # tensor([0.6484, 0.1694])
print("o_t:", o_t.item())          # 0.4427
print("h_t:", h_t)                 # tensor([0.2527, 0.0743]) -- matches every hand-computed value

Common Mistakes

  • Applying the forget and input gates as if they were single scalars rather than per-dimension vectors when the cell state has more than one dimension โ€” each element of \(\mathbf{C}_t\) is scaled independently by the corresponding element of \(\mathbf{f}_t\) and \(\mathbf{i}_t\).
  • Losing track of the order of operations โ€” the cell state must be fully updated (Step 4) before the output gate is applied to it (Steps 5โ€“6); the output gate never influences the cell-state update itself.

Interview Relevance

Q: "Walk through one complete LSTM time step with real numbers." This exact worked example โ€” computing the forget gate, input gate, candidate state, updated cell state, output gate, and finally the hidden state, in that specific order, with real numbers at each step โ€” is exactly the kind of exercise a strong candidate should be able to reproduce fluently for a sequence-modeling interview.

Practice Question

Using the same weights, compute one more time step, now with \(\mathbf{h}_t \approx [0.2527, 0.0743]\) and \(\mathbf{C}_t \approx [0.6484, 0.1694]\) as the new "previous" values, and a new input \(x_{t+1}=-0.5\). (You only need to compute the forget gate for this practice.)

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 Forward Pass โ€“ FAQs

Quick answers about learning LSTM Forward Pass in Deep Learning.

This free note from CodingNow 2.0 explains LSTM Forward Pass 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 Forward Pass, is 100% free with no signup required.
With focused practice, most students grasp LSTM Forward Pass 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