3.5.12 · D1Sequence Models

Foundations — Handling variable-length sequences

2,440 words11 min readBack to topic

Before you can read the parent note, you must be able to read its alphabet. Below is every symbol and idea the parent uses, built from nothing, each one leaning on the ones before it.


1. What is a "sequence"?

A sequence is just an ordered list of things, one after another, where the order carries meaning.

  • The picture: beads on a string. The first bead, the second bead, and so on. Swap two beads and you get a different string — order matters.
  • Why the topic needs it: language, audio, and time series are all sequences. If order didn't matter we could just throw everything in a bag and use an ordinary network.
Figure — Handling variable-length sequences

Look at the blue beads on the top row: they are labelled . That subscript number is the position .


2. The index and the length

  • The picture: is your finger pointing at one bead; is how many beads there are in total.
  • Why needed: the parent writes things like — those are the lengths of the 1st, 2nd, ... sequence. Different sequences have different , and that difference is the whole problem.

3. Subscripts and the parenthesis notation

The parent mixes two ways of pointing at one element. Both mean the same thing.

  • The picture: a grid. Rows are different sequences; columns are positions. is the cell in row , column .
  • Why needed: we process many sequences at once, so we always need two addresses — which sentence, and which word in it.

4. A "batch" and the letter

  • The picture: a stack of index cards, each card one sentence. = number of cards in the stack.
  • Why needed: hardware (GPUs) is fast when it does the same operation to many items at once. The parent's just says "average the loss over all cards in the stack". (See Batch Processing.)

5. The tensor and why it must be rectangular

  • The picture: a brick wall where every row must be the same width — no jagged edges allowed.
  • Why needed: this is the villain of the whole topic. Sequences have different lengths (jagged), but a tensor demands equal lengths (rectangular). That clash is exactly what padding fixes.
Figure — Handling variable-length sequences

The left picture (jagged) is what real data looks like. The right picture (rectangular) is what the computer demands. The grey cells are the fix — see the next section.


6. Padding and the <PAD> token

  • The picture: the grey cells on the right of figure s02 — extra beads glued on to make all strings equal length.
  • Why needed: it turns the jagged wall into a rectangular one. Now the batch is a legal tensor.
  • The picture: the width of the widest row; every other row stretches out to match it.

7. The mask — the star of the show

  • The picture: a stencil laid over the batch. Holes (1s) let real data through; blocked spots (0s) hide the fake padding.
  • Why needed: whenever we compute something (a loss, an attention score), we multiply by the mask so padding contributes zero. It's the memory of "which parts were fake".
Figure — Handling variable-length sequences

Read from the picture: row is all 1s up to its true length , then all 0s. The green region is real, the dark region is padding.


8. The Greek letter (sum) and

  • The picture: a conveyor belt. starts at 1, steps to 2, 3, ... up to ; at each stop you drop the value into a total.
  • Why needed: the parent's masked loss sums over all positions. Multiplying by inside the sum means the padding positions add , so only real positions count.

9. Loss and

  • Two fonts, two meanings (the parent uses both):
    • — the per-position loss: how wrong the prediction was at one single position .
    • (fancy script L) — the whole-sequence loss for sequence : an average of the per-position losses over its real tokens.
  • The picture: is one student's score on one question; is that student's average over all real questions (skipping the blank padding questions).

10. The hidden state and the RNN

  • The picture: reading a book and keeping a mental summary that you update after each word. = your summary after word ; = your summary just before it.
  • Why needed: a sequence model must remember earlier elements to understand later ones. The parent's update says "new memory = blend of (old memory) and (new input)". See Recurrent Neural Networks and LSTM and GRU.

11. Vectors, dot product, and

The attention section leans on these.

  • The picture: an arrow pointing somewhere in space; is how many directions (axes) that space has. means " is an arrow with numbers".
  • The picture: two flashlight beams. Pointing the same way → strong overlap (large dot product); at right angles → no overlap (zero).
  • Why needed: attention uses to ask "how well does what I'm looking for (query) match what's here (key)?" See Attention Mechanism and Transformers.
Figure — Handling variable-length sequences

12. Softmax and

  • means : it makes everything positive and blows up big scores more than small ones (winner emphasised).
  • (Greek alpha) is the resulting weight for position — "how much attention position gets", between 0 and 1.
  • Why needed: attention's output is , a weighted blend of values. Softmax guarantees the weights are a fair share adding to 100%.

How it all fits together

Sequence: ordered list x_t

Index t and length L

Batch of B sequences, lengths differ

Tensor must be rectangular

Padding to L_max

Mask m_i of t marks real vs fake

Masked loss: sum and average over real tokens

Hidden state h_t and shared weights W

RNN handles any length

Vector and dot product

Attention score q dot k

Softmax gives weights alpha

Length-agnostic attention output

Handling variable-length sequences

Everything on the left builds the padding + mask machinery; the middle builds the RNN; the right builds attention. All three converge on the parent topic: Handling variable-length sequences.


Equipment checklist

Test yourself — reveal only after you've answered.

What does the index point at, and what does measure?
points at one position (which element); is the total number of elements in the sequence.
What does mean?
The element at position of sequence number (row , column of the batch grid).
Why must a batch tensor be rectangular?
Hardware processes equal-shaped blocks fast; every row needs the same number of columns, so lengths must match.
What is ?
The length of the longest sequence in the batch — the length everyone is padded up to.
What does a mask value of 1 vs 0 mean?
1 = real element (count it); 0 = padding (ignore it).
Why does equal ?
The mask is 1 for each real token and 0 for padding, so adding them counts exactly the real tokens = the true length.
What does tell you to do?
Let run from 1 to and add up all the terms .
Difference between and ?
is the loss at one single position; is the averaged loss over all real positions of sequence .
What is the hidden state ?
The network's running memory — a vector summarising everything seen up to position .
Read the subscripts: what do and do?
transforms the input into hidden space; transforms the previous hidden state .
What does the dot product measure?
Alignment between two vectors — large when they point the same way, near zero when perpendicular.
What does softmax produce?
A list of positive weights that sum to 1 (a probability distribution) from raw scores.
Why set padding scores to before softmax instead of multiplying by 0?
Because would still leak attention; gives padding exactly zero weight.