Sequence Modeling
When the order of inputs is the signal
Sequence modeling predicts from ordered inputs — time series, text, audio, events. Order is information, so the model must respect it: what happened before shapes what happens now.
What you will learn
- Recognize which problems are sequential and why order matters
- Distinguish fixed-window models from stateful recurrent models
- Frame a sequence task as predict-next vs classify-sequence
What is this?
Sequence modeling handles data where inputs arrive in an order and that order carries meaning — a sentence, a heartbeat trace, a purchase history. Predictions depend on positions relative to each other, so a good sequence model consumes the whole ordered input (or a window of it) instead of treating records as independent. Classic approaches slide a fixed window into an MLP; recurrent models carry state across the entire sequence.
Why do you need it?
- Order matters: 'schedule the meeting' vs 'the meeting schedule' — shuffling destroys the problem.
- Context grows: one word's meaning depends on the sentence, the sentence on the conversation.
- Forecasting is inherently sequential — next value given all previous values.
- Choosing window-vs-recurrent is a real architecture decision with capacity and cost implications.
Mental model
Reading a book forward only, then deciding the next word
Sequential prediction with a recurrent model
You keep a running understanding (state) built from every page so far; you don't re-read the whole book each page. That state — not just the last page — informs 'what happens next'.
How it works
- Represent the sequence as tokens or numeric features in order: x1, x2, ..., xT.
- A fixed-window model takes the last k values as one vector — simple, bounded context.
- A recurrent model processes one token at a time, updating a hidden state h_t from h_(t-1) and x_t.
- The state compresses all past context ('what I've seen so far') into a fixed-size vector.
- Outputs may be per-step (predict next token) or after the whole sequence (classify the sequence).
Fixed window vs recurrent state.
In production
- Time-series forecasting: stock sims, demand, energy — predict the next step from history.
- Speech/audio tagging: classify segments or phonemes from ordered frames.
- Text as sequence: sentiment, translation, language modeling — every token depends on its context.
Best practices
- Encode order explicitly: positional features, lags, or careful tokenization.
- Choose the model on the needed context length: window models for short, bounded context.
- Normalize per-sequence or per-batch rather than globally when scale varies.
- Report metrics at the granularity of your decision (per-step accuracy vs whole-sequence)
Common mistakes
- Shuffling training batches before deciding order is irrelevant — destroys sequential structure.
- Sliding windows too small to capture the actual dependencies.
- Ignoring event-time vs processing-time when the sequence comes from real-world logs.
- Assuming 'sequence model' must be recurrent — modern transformers often win on long context.
Common issues
Model ignores later context — long pauses all look the sameWindow/state too short to carry the relevant history
Widen window, add lag features, or switch to a recurrent/attention model.
Accuracy great on train, poor on online dataOnline stream arrives with different cadence/order — event time unused
Use event time + sequence ids and validate on a real-time replay.
Every prediction is the global mean/latest valueImbalanced sequence lengths or batch normalization across sequences
Mask padded regions and use per-sequence normalization.
Debugging tips
- Plot the sequence and label distribution; most 'model errors' are mislabeled/truncated sequences.
- Check context length: if accuracy jumps when you widen a window, the dependency was longer than you thought.
- Compare windowed-MLP vs recurrent baselines before blaming the architecture.
- Verify ordering survived preprocessing (sort/merge shuffles are a classic silent bug).
Interview checkpoint
Quick quiz
What is the key signal a sequence model must respect?
A fixed sliding-window MLP can never see context before its window.