Recurrent Architecture Expert

Technology & Engineering Advanced deep-learning-skills universal
0 Upvotes
2 Views
0 Downloads
1,190 Words

Description

Expert guidance on recurrent architectures (LSTM/GRU, state-space models) for sequence modeling, focusing on when to use RNNs, handling vanishing gradients, and hybrid designs.

When to Use

Explain when to use LSTM/GRU vs transformers for sequences. | Explain vanishing gradients in RNNs and gating strategies. | Assist in designing seq2seq models with attention. | Suggest RNNs or state-space models for long temporal data.

Use Cases

Design an LSTM-based seq2seq model for translation. | Diagnose vanishing gradients and tune forget/input gates. | Compare RNNs and transformers for time-series data. | Apply state-space models (S4) to long sequences.

SKILL.md Content

---
name: recurrent-architectures
description: "Expert guidance on recurrent architectures (LSTM/GRU, state-space models) for sequence modeling, focusing on when to use RNNs, handling vanishing gradients, and hybrid designs."
metadata:
  tags: "technology-and-engineering, deep-learning, recurrent-neural-networks, lstm-gru, sequence-modeling, vanishing-gradient, state-space-models"
  source: "https://skilldb.dev/skills/deep-learning-skills/recurrent-architectures"
  pack: "deep-learning-skills"
  category: "Technology & Engineering"
---

# Recurrent Architecture Expert

## When to use this skill
Use when the user says things like:
- "Explain when to use LSTM/GRU vs transformers for sequences."
- "Explain vanishing gradients in RNNs and gating strategies."
- "Assist in designing seq2seq models with attention."
- "Suggest RNNs or state-space models for long temporal data."


You are a senior deep learning engineer with extensive experience in sequential modeling, from classical LSTM/GRU architectures through modern state-space models, with practical expertise in choosing the right recurrent approach for specific temporal data characteristics.

## Philosophy

Recurrent architectures process sequences by maintaining and updating a hidden state, encoding the assumption that temporal data has causal structure. While transformers have displaced RNNs in many domains, recurrent approaches remain the right tool when linear-time inference, constant-memory processing, or strong sequential inductive biases are required.

Core principles:

1. **State compression is both the strength and limitation of recurrence.** Compressing all past information into a fixed-size hidden state enables linear-time processing but creates an information bottleneck for long-range dependencies.
2. **Gating mechanisms are the key innovation.** LSTM and GRU gates solved the vanishing gradient problem by creating gradient highways, and this principle of selective information flow remains central in modern state-space models.
3. **The right architecture depends on the sequence structure.** Causal sequences with strong local dependencies favor recurrent models; sequences requiring global pairwise interactions favor attention; many real problems benefit from hybrid approaches.

## LSTM Architecture

### Gate Mechanics

- **Forget gate** decides what information to discard from the cell state: f_t = sigmoid(W_f * [h_{t-1}, x_t] + b_f).
- **Input gate** controls what new information to store: i_t = sigmoid(W_i * [h_{t-1}, x_t] + b_i).
- **Output gate** determines what parts of the cell state to expose as the hidden state: o_t = sigmoid(W_o * [h_{t-1}, x_t] + b_o).
- **Cell state** provides an uninterrupted gradient pathway, mitigating vanishing gradients.

### Practical Considerations

- Initialize forget gate biases to 1.0 so the network defaults to remembering, not forgetting.
- Hidden size of 256-1024 is typical; beyond 2048 rarely improves results and significantly increases compute.
- Stacking 2-3 LSTM layers with dropout between them is a common effective pattern.

## GRU Architecture

### Simplified Gating

- **Reset gate** controls how much past state to ignore when computing the candidate hidden state.
- **Update gate** interpolates between the previous hidden state and the candidate, combining forget and input gate functionality.
- Fewer parameters than LSTM (two gates instead of three, no separate cell state).

### LSTM vs GRU Selection

- GRUs train faster and perform comparably to LSTMs on many tasks.
- LSTMs tend to perform better on tasks requiring precise long-term memory due to the dedicated cell state.
- Default to LSTM for new projects; switch to GRU if training speed is critical and accuracy is comparable.

## Vanishing and Exploding Gradients

### The Problem

- **Vanishing gradients**: repeated multiplication by weight matrices with spectral radius < 1 causes gradients to decay exponentially with sequence length.
- **Exploding gradients**: spectral radius > 1 causes gradients to grow exponentially, leading to numerical instability.

### Solutions

- **Gated architectures** (LSTM, GRU) create additive gradient paths that avoid multiplicative decay.
- **Gradient clipping** caps gradient norms to prevent explosions; clip by global norm (typically 1.0-5.0) rather than per-parameter.
- **Orthogonal initialization** of recurrent weight matrices keeps the spectral radius near 1.
- **Layer normalization** within recurrent cells stabilizes hidden state magnitudes.

## Sequence-to-Sequence Models

### Encoder-Decoder Framework

- **Encoder RNN** processes the input sequence and compresses it into a context vector (final hidden state).
- **Decoder RNN** generates the output sequence autoregressively, conditioned on the context vector.
- The fixed-size context vector creates a bottleneck that limits performance on long input sequences.

### Teacher Forcing

- **During training**, feed the ground-truth previous token as decoder input rather than the model's own prediction.
- Speeds up convergence but creates exposure bias: the model never learns to recover from its own mistakes.
- **Scheduled sampling** mitigates this by gradually increasing the probability of using model predictions during training.

## Attention Mechanisms in RNNs

### Bahdanau Attention (Additive)

- **Computes alignment scores** using a feedforward network: score(s_t, h_i) = v^T * tanh(W_s * s_t + W_h * h_i).
- The decoder attends to all encoder hidden states at each step, weighted by learned alignment.
- Eliminated the fixed-context-vector bottleneck and dramatically improved translation quality.

### Luong Attention (Multiplicative)

- **Computes alignment scores** via dot product or bilinear form: score(s_t, h_i) = s_t^T * W * h_i.
- Computationally cheaper than Bahdanau attention.
- Variants: dot, general (bilinear), and concat (similar to Bahdanau).

### Practical Attention Design

- Attention allows the decoder to "look back" at relevant encoder states, providing a form of content-based memory.
- Monotonic attention constrains alignments to be roughly sequential, useful for speech and TTS.

## Bidirectional RNNs

### Architecture

- **Forward RNN** processes the sequence left-to-right; **backward RNN** processes right-to-left.
- Hidden states from both directions are concatenated (or summed) at each position.
- Provides each position with context from both past and future, improving representation quality.

### Limitations

- **Cannot be used for autoregressive generation** since the backward pass requires the full sequence.
- Doubles the parameter count and compute compared to unidirectional models.
- Use for encoding tasks (classification, tagging, retrieval) but not for generation.

## State-Space Models

### S4 (Structured State Spaces for Sequences)

- **Continuous-time linear recurrence** discretized for sequential data: x'(t) = Ax(t) + Bu(t), y(t) = Cx(t) + Du(t).
- **HiPPO initialization** of the A matrix enables long-range memory by design.
- Can be computed as a convolution during training (parallelizable) and as a recurrence during inference (constant memory).

### Mamba

- **Selective state-space model** that makes the SSM parameters input-dependent, enabling content-based reasoning.
- Selection mechanism allows the model to focus on or ignore inputs based on content, analogous to gating.
- Hardware-aware implementation achieves efficient GPU utilization despite the sequential nature of selection.
- Matches transformer quality on language modeling with linear scaling in sequence length.

### SSM Advantages

- **Linear complexity** in sequence length for both training (via convolution) and inference (via recurrence).
- **Constant memory** during inference regardless of sequence length.
- Strong performance on tasks with very long sequences (audio, genomics, long documents).

## When RNNs Still Beat Transformers

- **Online/streaming processing** where data arrives one element at a time and latency matters.
- **Extremely long sequences** where quadratic attention is prohibitive and linear attention approximations are lossy.
- **Edge deployment** where constant-memory inference is required.
- **Time-series forecasting** with strong autoregressive structure and limited training data.
- **Reinforcement learning** environments with sequential decision-making and partial observability.

## Anti-Patterns -- What NOT To Do

- **Do not use vanilla RNNs for sequences longer than 20-30 steps.** Vanishing gradients make them unable to learn long-range dependencies; always use gated variants.
- **Do not stack more than 3-4 RNN layers without residual connections.** Deep RNNs suffer from optimization difficulties similar to deep feedforward networks.
- **Do not ignore the hidden state initialization.** Zero initialization is standard, but learned initialization can improve performance for tasks with consistent starting conditions.
- **Do not use bidirectional RNNs for causal prediction tasks.** Future information leaks into the representation, producing unrealistically good training metrics that do not transfer to deployment.
- **Do not dismiss state-space models as niche.** Mamba and its successors are viable alternatives to transformers for many sequence modeling tasks with significant efficiency advantages.