πŸ”¬ LSTM & GRU Detailed Analysis

434 words
2 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# πŸ”¬ LSTM & GRU Detailed Analysis ## 1. 🎯 Learning Objectives - Trace LSTM forward pass with numerical example - Explain gradient flow through cell state - Compare LSTM vs GRU: parameter count, performance - Implement LSTM from scratch ## 2.

πŸ”¬ LSTM & GRU Detailed Analysis

1. 🎯 Learning Objectives

  • Trace LSTM forward pass with numerical example
  • Explain gradient flow through cell state
  • Compare LSTM vs GRU: parameter count, performance
  • Implement LSTM from scratch

2. πŸ“– Core Content

3.1 LSTM Forward Pass Numerical Example

Let's trace a single LSTM cell with input x_t = [0.5, -0.3] and previous hidden h_{t-1} = [0.2, 0.1], cell state C_{t-1} = [0.0, 0.0]. Step 1: Forget gate f_t = Οƒ(W_f Β· [h_{t-1}, x_t] + b_f) Assuming W_f Β· [h_{t-1}, x_t] = [0.5, -0.2], b_f = [0.1, 0.0] f_t = Οƒ([0.6, -0.2]) = [0.646, 0.450] Step 2: Input gate i_t = Οƒ(W_i Β· [h_{t-1}, x_t] + b_i) = Οƒ([0.3, 0.4]) = [0.574, 0.599] Step 3: Candidate cell state CΜƒ_t = tanh(W_C Β· [h_{t-1}, x_t] + b_C) = tanh([0.8, -0.5]) = [0.664, -0.462] Step 4: Update cell state C_t = f_t βŠ™ C_{t-1} + i_t βŠ™ CΜƒ_t C_t = [0.646Γ—0 + 0.574Γ—0.664, 0.450Γ—0 + 0.599Γ—(-0.462)] C_t = [0.381, -0.277] Step 5: Output gate o_t = Οƒ(W_o Β· [h_{t-1}, x_t] + b_o) = Οƒ([0.2, 0.6]) = [0.550, 0.646] Step 6: Hidden state h_t = o_t βŠ™ tanh(C_t) = [0.550Γ—tanh(0.381), 0.646Γ—tanh(-0.277)] h_t = [0.550Γ—0.364, 0.646Γ—(-0.270)] h_t = [0.200, -0.174]

3.2 Gradient Flow

The cell state recurrence: C_t = f_t βŠ™ C_{t-1} + i_t βŠ™ CΜƒ_t The gradient of C_t w.r.t. C_{t-1} is f_t (not a matrix multiplication). If f_t β‰ˆ 1, gradient flows back almost unchanged β€” this is why LSTM avoids vanishing gradients.

3.3 LSTM vs GRU Comparison

AspectLSTMGRU
Gates4 (forget, input, candidate, output)2 (reset, update)
Cell stateYes (C_t)No (only hidden h_t)
ParametersMore (4Γ— W matrices)Fewer (3Γ— W matrices)
PerformanceComparableComparable (often slightly worse)
Training speedSlower (more params)Faster (fewer params)
When to useComplex sequence tasksSimpler tasks, less data

3.4 PyTorch Implementation

python
class LSTMCell(nn.Module):
    def __init__(self, input_size, hidden_size):
        super().__init__()
        self.W_f = nn.Linear(input_size + hidden_size, hidden_size)
        self.W_i = nn.Linear(input_size + hidden_size, hidden_size)
        self.W_C = nn.Linear(input_size + hidden_size, hidden_size)
        self.W_o = nn.Linear(input_size + hidden_size, hidden_size)
    def forward(self, x, h, C):
        combined = torch.cat([h, x], dim=1)
        f = torch.sigmoid(self.W_f(combined))
        i = torch.sigmoid(self.W_i(combined))
        C_tilde = torch.tanh(self.W_C(combined))
        o = torch.sigmoid(self.W_o(combined))
        C_next = f * C + i * C_tilde
        h_next = o * torch.tanh(C_next)
        return h_next, C_next

4. πŸ“ Practice Questions

Q1: If the forget gate outputs are all close to 1, what happens to gradient flow?
Answer: The gradient flows almost unchanged through the cell state. This is how LSTM captures long-term dependencies. The forget gate learns which past information to keep, and when f_t β‰ˆ 1, that information persists. Join Discord PreviousRNN VariantsNextTransformers & Attention
Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.