All courses
Courses › How LLMs really work · foundation

Reading formulas: indices and sums

Read subscripts like $x_i$ and $W_{ij}$, Σ sums and ∏ products, and turn a formula into a loop.

▶ Watch this lesson Start the whole course

What the animation says

Subscripts are positions

Formulas in LLM papers look dense, but most of them are just loops written in shorthand. Start with the tiny letters. A small letter below a name picks a position, just like an index in code. So x with a small i means item number i of the list x. On paper we count from one, so x two is five. Two small letters pick from a grid. The first one is the row, the second one is the column. Your turn. Tap the number that W three one points to.

Σ is a loop that adds

That big zigzag is a capital Greek S, for sum. It's a loop that adds things up. Below it you see the index name and where it starts. Above it, where it stops. To its right is what to compute for each value of the index. The running total starts at zero. Then i goes one, two, three: add two, add five, add three. Ten. Now the thing on the right is x i squared. What does the sum come to?

Each term first, then combine

Thirty-eight. The part on the right is worked out fresh for each i, and only then added. Squaring the total of ten would give a hundred, which is wrong. A capital Greek P, for product, is the same loop, but it multiplies. So its running total starts at one. Two times five times three is thirty. Start it at zero and every product would collapse to zero. And the index name doesn't matter. Call it i or j, the loop does exactly the same.

A free index gives one result each

Here's a formula inside every layer: y j equals the sum over i of x i times W i j. The i sits under the sum, so the loop uses it up. The j sits under no sum. It's free. A free index means one result for each of its values. W has two columns, so we get two results. For y one, walk down column one: two times one, five times zero, three times two. Eight. Your turn. Tap the column that y two walks down.

The same thing as code

Column two: two times four, five times one, three times one. Sixteen. Now as code. Each sum becomes a loop, with a total that starts at zero. The free index becomes an outer loop, giving one result per column. Careful: maths counts from one, Python counts from zero. So x one on paper is x zero in code. That's the whole skill. Subscripts are positions, sums and products are loops, free indices are outer loops.

What do the tiny letters and big Σ mean?

LLM formulas are full of symbols like $x_t$, $W_{ij}$ and a big $\sum$ with numbers above and below. They look dense, but almost every one is just a **loop written in shorthand**: go through some positions and add (or multiply) what you find. Once you can read that shorthand, a formula becomes a few lines of code you could run by hand.

Subscripts are positions, Σ and ∏ are loops

A **subscript** picks an item. $x_i$ is item number $i$ of the list $x$, like `x[i]` in code. Two subscripts pick from a grid of numbers: $W_{ij}$ is the number in **row** $i$, **column** $j$. $\sum$ (capital Greek S, for *sum*) means: let an **index** run over a range and add up the results. $\prod$ (capital Greek P, for *product*) means the same, but multiply.

The exact rules

$$\sum_{i=1}^{n} a_i = a_1 + a_2 + \dots + a_n \qquad \prod_{i=1}^{n} a_i = a_1 \cdot a_2 \cdots a_n$$ - Below: the index name and its start ($i=1$). - Above: the last value ($n$). - Right: what to compute for each $i$. The summed index is a **dummy index**: renaming $i$ to $j$ changes nothing. A sum starts its **running total** at 0, a product at 1. An index that appears in a formula but sits under no $\sum$ or $\prod$ is a **free index**: you get one result for each of its values. In $y_j = \sum_i x_i W_{ij}$, $i$ is summed away and $j$ is free. A **nested sum** like $\sum_i \sum_j W_{ij}$ holds the outer index fixed while the inner one runs through its whole range.

Working it by hand

Take $x = (2, 5, 3)$ and a grid $W$ with 3 rows, $(1, 4)$, $(0, 1)$ and $(2, 1)$, so 2 columns. $\sum_{j=1}^{3} x_j^2 = 4 + 25 + 9 = 38$ $\prod_{j=1}^{3} x_j = 2 \cdot 5 \cdot 3 = 30$ $y_j = \sum_{i=1}^{3} x_i\,W_{ij}$, one result per column $j$ (the free index): - $y_1 = 2\cdot1 + 5\cdot0 + 3\cdot2 = 8$ - $y_2 = 2\cdot4 + 5\cdot1 + 3\cdot1 = 16$

The same formulas as loops

Each $\sum$ becomes a loop with a running total starting at 0; each $\prod$ the same, starting at 1. A free index ($j$ in $y_j$) becomes an outer loop, one result per value. Watch out: maths counts from 1, Python counts from 0, so $x_1$ is `x[0]`.
x = [2, 5, 3]
W = [[1, 4], [0, 1], [2, 1]]   # 3 rows, 2 columns
s = 0
for i in range(3):          # i = 0,1,2 in Python = 1,2,3 on paper
    s += x[i] ** 2
print(s)                    # 38
p = 1                       # a product starts at 1
for i in range(3):
    p *= x[i]
print(p)                    # 30
y = []
for j in range(2):          # free index j: one result per column
    total = 0
    for i in range(3):      # the sum over i
        total += x[i] * W[i][j]
    y.append(total)
print(y)                    # [8, 16]

Remember

A subscript is a position: $x_i$ is item $i$, $W_{ij}$ is row $i$, column $j$. $\sum$ and $\prod$ are loops that add (starting at 0) or multiply (starting at 1) over the index range written below and above them. Any free index, one not consumed by a sum or product, becomes an outer loop with one result per value.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.