Reading formulas: indices and sums
Read subscripts like $x_i$ and $W_{ij}$, Σ sums and ∏ products, and turn a formula into a loop.
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.
Subscripts are positions, Σ and ∏ are loops
The exact rules
Working it by hand
The same formulas as loops
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
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.
Bytewise
All courses