Home / Natural Language Processing

What is a Sequence?

A sequence is data where order carries meaning. Shuffle a sentence and watch its meaning fall apart.

Overview

The Core Idea

A sequence is an ordered collection of items where the position of each item carries meaning. "Dog bites man" and "man bites dog" contain exactly the same three words, yet they describe opposite events. That difference lives entirely in the order — and order is precisely what ordinary tabular data doesn't have.

Input Sentence

Tip: Try "dog bites man" — a 3-word sentence where a single swap completely flips the meaning.

The Sequence (order preserved)

SEQUENCE INTACT

The Same Words as a Bag (order ignored)

A "bag of words" only knows which words appear and how often — never where. Notice this view never changes when you shuffle: to a bag, every ordering is identical.

Sequences Beyond Text

Stock prices
102 → 105 → 103 → 110

Yesterday's price helps predict today's.

DNA
A → T → G → C → A

Rearranging bases changes the protein.

Music
C → E → G → C

The same notes in a different order is a different melody.

What is a Sequence? Why Order is Information

The foundational idea behind all sequential models — from sliding windows to RNNs and Transformers.

Sequential vs. Tabular Data

In a classic spreadsheet-style dataset, each row is independent: shuffling the rows of a housing-price table changes nothing about what a model can learn. Sequential data breaks this assumption in two ways:

  • Order matters: the items form a meaningful progression — words in a sentence, prices over days, frames in a video.
  • Context matters: the meaning of an element depends on its neighbours. The word "bank" means something different after "river" than after "savings".

Because of this, sequence models need a way to remember or look at what came before — the motivation behind sliding windows, recurrent networks, and attention.

Order is part of the data

A sequence is data where the order carries meaning. Shuffle it and you have destroyed information, not merely rearranged it.

"The dog bit the man" and "The man bit the dog" contain identical words and describe different events. A stock price series read backwards describes a different market. A DNA sequence read out of order codes for nothing.

Compare that with a table of customers: shuffle the rows and nothing is lost, because each row is independent of the others. That independence is what most standard machine learning assumes, and sequences violate it.

DataSequential?Why
TextYesWord order is grammar
Audio waveformYesTime ordering is the signal
VideoYesFrame order is motion
Sensor readingsYesTime ordering carries trend
Customer recordsNoRows are independent
ImagesSpatially, not temporally2-D structure, no time axis

What makes sequences hard

Four properties, each requiring a specific architectural response.

Variable length. One sentence is 5 words, another 500. Fixed-size inputs cannot accommodate both, so models must either pad and mask, or process step by step.

Order matters. Any model treating the input as an unordered set is unusable, which is why attention needs positional encodings bolted on.

Long-range dependencies. A pronoun may refer to a noun fifty words earlier; a stock's behaviour may depend on last quarter. Architectures that decay information over distance cannot capture this.

Elements are not independent. Standard train/test splitting assumes independence. Shuffling a time series before splitting lets the model see the future, producing excellent scores and worthless models.

That last point is the one that catches people in practice. Sequence data needs chronological splits, and windows that overlap the boundary must be excluded.

The architectures, in order of history

ModelHow it handles orderLimitation
Bag of wordsIt does notOrder lost entirely
n-gramsLocal windows of nCannot reach beyond n
RNNA carried hidden stateGradients decay; no parallelism
LSTM / GRUGated state with an additive pathLong ranges still hard
1-D CNNLocal filters over timeFixed receptive field
TransformerAttention plus positional encodingQuadratic in length
State-space (Mamba)Recurrence with linear scalingNewer, less tooling

Each row fixes the previous row's main problem. The transformer's dominance comes from two structural wins — constant path length between any two positions, and full parallelism across the sequence — at the cost of quadratic attention.

What makes data sequential, and how to check

Order matters or it does not, and that single question decides your whole architecture. Here is a test that measures it rather than assuming, run on data where the answer is known both ways.

example_01.pyNumPy
Output

Experiments to try

  1. Shuffle the default sentence. Watch the top panel: the words get amber borders and the read-out sentence becomes gibberish, while the "bag" panel below is completely unchanged. The bag never noticed.
  2. Type "dog bites man" and click "Reverse the Order". Three words, one reversal, opposite meaning — the smallest possible demonstration that position is information.
  3. Restore the original. The status badge flips back to green the moment every word returns to its home index.

The short of it

A sequence is not just a set of values — it is values plus their order. Any model that throws the order away (like a bag of words) throws information away with it. Everything else in this NLP track — sliding windows, encodings, embeddings, recurrent cells — exists to let neural networks use that ordering information instead of losing it.

Preparing sequence data

Whatever the model, the same practical decisions arise.

Fix the length. Pad short sequences and truncate long ones, and supply an attention mask so the model ignores padding. Which end you truncate matters: for classification the beginning is often most informative; for time series the most recent values are.

Window it. For forecasting, slide a window along the series so each position becomes one training example: the previous k values as features, the next value as the target. This converts a sequence into an ordinary supervised table.

Split chronologically. Train on the past, validate on the middle, test on the most recent. Never shuffle. Leave a gap when windows would otherwise straddle the boundary.

Scale using training-period statistics only. Computing a mean over the whole series leaks the future into the past.

# windowing a series into (features, target) pairs
X, y = [], []
for i in range(len(series) - window):
    X.append(series[i:i + window])
    y.append(series[i + window])

Sequence-to-what?

The shape of the task determines the architecture's output arrangement:

ShapeExample
Sequence → one labelSentiment, intent classification
Sequence → label per elementNamed entities, part-of-speech tagging
Sequence → different-length sequenceTranslation, summarisation
Sequence → next elementForecasting, language modelling
One input → sequenceImage captioning, text-to-speech

For the first, pool over all positions rather than taking only the last — the final state is biased towards the end of the input.

For the third, an encoder-decoder or a decoder-only model with the input as a prefix. This is where the attention mechanism was originally invented, to remove the bottleneck of compressing a whole source sentence into one vector.

Questions people ask

Are images sequences? They have spatial structure but no time axis, so convolutions suit them better. Vision transformers do treat patches as a sequence, with 2-D positional encodings.

Can I use a standard neural network on sequences? Only by flattening a fixed-length window, which discards the notion of order and cannot generalise across positions. It is a baseline, not a solution.

How long can a sequence be? LSTMs handle hundreds of steps reliably; transformers reach hundreds of thousands of tokens with engineering effort and quadratic cost.

Why can I not shuffle time series before splitting? Because the model would train on the future and be tested on the past, which is not a capability it will have in production.

What about irregular timestamps? Resample to a regular frequency, or use a model that takes the time delta as an explicit input.

Is a transformer always better than an LSTM? Not always. For streaming with bounded memory, small datasets and on-device inference, recurrent models remain the practical choice.

Recap in one screen

  • A sequence is data where order carries meaning, so elements are not independent.
  • Variable length, order sensitivity, long-range dependence and non-independence are the four difficulties.
  • Padding with masks, or step-by-step processing, handles variable length.
  • Split chronologically and scale on training-period statistics — shuffling leaks the future.
  • Transformers win on path length and parallelism; recurrent models still win on streaming and small data.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “The Core Idea”?

  2. What does this module say about “Sequential vs. Tabular Data”?

  3. What does this module say about “Order is part of the data”?

Cheat sheet

What is a Sequence?

A sequence is an ordered collection of items where the position of each item carries meaning. "Dog bites man" and "man bites dog" contain exactly the same three words, yet they describe opposite events. That difference lives entirely in the order — and order is precisely what ordinary tabular data doesn't have.

NLP · vizlearn.in/natural_language_processing/what_is_a_sequence.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.