Module 0 · optional · Lesson 0.6
Write equations and read a paper
Translate a computational description into notation, then decode a new equation without a diagram. By the end, you should state axes, reductions and assumptions before checking a reference answer.
Prerequisite: the notation, index, shape and softmax tools from this optional toolkit.
0.6.1 Worked example: a loop becomes a sum
Read this pseudocode without running it. It uses one-based indices and five scalar components in vectors w and x; z is the scalar accumulator.
z = 0
for i in 1..5 inclusive:
z += w[i] * x[i]The accumulator removes the loop index from the result. A direct translation is:
z = sum from i=1 to 5 of w_i x_i
The same operation can be written as a dot product z = transposed w times x when both w and x are five-by-one column vectors. This vector-orientation assumption matters; the indexed sum makes the componentwise operation explicit.
Common mistake: leaving i as a free output index. That would describe five separate products rather than the one accumulated result.
0.6.2 Write before revealing a canonical answer
On paper or in your own editor, translate each description before opening the references. No answer needs to be typed into this page; equivalent mathematical notation is welcome.
- For a fixed output token i and feature j, initialize an accumulator; for each source row k from 1 through n, add A[i,k] times V[k,j].
- Find the maximum c of a finite score row u. For each position r, exponentiate u[r]−c, then divide by the sum of all those exponentials.
- Q is 3 by 7 and K is 5 by 7. Write a valid matrix product comparing every query with every key, and declare its output shape.
Check the reference explanation
O_ij = sum over k=1 through n of A_ik V_kj
c = max over s u_s; p_r = exp(u_r minus c) divided by sum over s exp(u_s minus c)
Q times transposed K has shape 3 by 5
A bound index may be renamed consistently, and exp(x) and e to the power x are equivalent notation. For the weighted sum, O=AV is equivalent when the full dimensions are compatible. An arbitrary free-text parser is not used: compare the operations and axes, not the spelling alone.
0.6.3 Test your translation
Make a prediction before opening hints. Try the task again without help after checking the reasoning.
0.6.4 Read an unfamiliar paper-style equation
The following invented equation uses the same kinds of operations as attention, with unfamiliar names and an explicit positive scale. Treat it as a short paper excerpt; do not use the previous diagrams.
C = row softmax of (U times transposed R divided by tau), multiplied by F
U is p by d; R is q by d; F is q by v; tau is positive
U contains p query-like rows, R contains q comparison rows, F contains q value-like rows, d is comparison width, v output width, and τ (tau) is a positive scalar scale. Without help: identify the reduction in the first product, state every intermediate shape, write one output entry as an indexed sum, and name an assumption you would check in an actual paper.
Check the reference explanation
Transposed R has shape d by q. Its product with U reduces d and yields p by q; scalar division and row softmax preserve p by q. Multiplication by F reduces q and produces C with shape p by v. If G names the normalized p-by-q weights, then:
C_ij = sum over k=1 through q of G_ik F_kj
i selects an output row, j an output feature, and k runs over the q comparison rows. Confirm which softmax axis is intended, whether any positions are masked, that each row has at least one legal entry, and that the declared vector orientation matches the multiplication. A paper may suppress batch/head axes or numerical stabilization details. Do not assume their absence means they do not exist.
The invented excerpt is an exercise, not a quotation. For a real paper, read the declarations and prose around an equation in Attention Is All You Need, Section 3.2, then compare its conventions with this course.
0.6.5 Choose a targeted return
Use this diagnostic without a score: if symbols remain ambiguous, revisit grammar; if the loop index survives accidentally, revisit reductions; if shapes disagree, revisit dimensions; if normalization or overflow is unclear, revisit softmax. If you can explain each operation unaided, return to the attention equation. You may continue at any point.