Skip to content

Math Notation Is Just Code

12 exercises

If you haven’t seen mathematical equations for a few years (or decades!) this page is for you. We’ll use your experience as a software engineer to reintroduce just enough of the language of mathematical notation (“MN”)1 to start work on mechanistic interpretability.

Let’s start with a simple loop.

def compute(x):
s = 0
for j in range(3):
s += x[j]
return s
s=∑j=02xj{\htmlData{link=result}{s}} =\mathop{\htmlData{link=sum}{\sum}}\limits_{\htmlData{link=index}{j}=\htmlData{link=bounds}{0}}^{\htmlData{link=bounds}{2}} \htmlData{link=term}{x_{\htmlData{link=index}{j}}}
Predict the result

With x = [2, 4, 8, 16], what does compute(x) return?

As a Python programmer, you’re familiar with special keywords or builtins like for and range to express loops. MN is similar, except that it frequently uses Greek letters, subscripts, and superscripts. This is initially confusing, but some of it stems from the annoying convention that mathematicians omit the multiplication sign when multiplying numbers.2 So while we read ab as a two-character string, mathematicians read it as aa times bb.3

ML researchers also like to act fancy by using lots of Greek letters.4 Like variables in programs, these letters can mean anything, but certain conventions have developed. For example, you’re probably familiar with the lowercase letter π\pi to represent the ratio of a circle’s circumference to its diameter. But in other fields of math, π\pi is sometimes used to represent a probability distribution, and in reinforcement learning it’s used to represent a particular way of choosing an action (a “policy”). A large part of getting comfortable with mathematical notation is understanding and internalizing these conventions.

We’ll start with the simplest and most common convention: The uppercase letter Sigma (Σ\Sigma) represents a sum. It’s almost exactly equivalent to a for loop where we’re adding things up inside the loop. And just as we have to define the bounds of a for loop in code (e.g. for i in range(1, 5)) we also have to define the bounds when using Σ\Sigma. We do this by writing the lower bound as a subscript, and the upper bound as a superscript. We also use the subscript to name the variable that we’re looping over. So if we want to represent for i in range(1,5) we’ll write ∑i=14\sum_{i=1}^{4}. Notice that while our loop was over range(1,5), our summation is from i=1i=1 to 44. This is just because Python’s range function excludes the final value, while Σ\Sigma includes it.

In Python, we access elements of lists or arrays with indexing. For example, to get the jjth element of xx, we write x[j]. In MN, we’ll again use subscripts: we write the jjth element of xx as xjx_j.

Let’s put it together now. First, a simple Python loop:

s = 0
for i in range(1, 10):
s += x[i]

And now its MN equivalent:

s=∑i=19xi{\htmlData{link=result}{s}} = \mathop{\htmlData{link=sum}{\sum}}\limits_{\htmlData{link=index}{i}=\htmlData{link=bounds}{1}}^{\htmlData{link=bounds}{9}} \htmlData{link=term}{x_{\htmlData{link=index}{i}}}

x = [4, 1, 6, 2, 9, 3, 5, 8, 7, 2], summed from i = 1 to 9.

Before the loop, s = 0.

Step 1 of 10

Let’s do some simple exercises to lock in the idea. Please: it’s critical to do these without AI help. You’re here to learn, and if you’re having the AI do it for you, you’re not learning. On the other hand, if you don’t understand something, I fully encourage you to ask an AI for a better explanation.

Now on to the exercises!

Predict a sum

With x = [3, 5, 7, 11], evaluate the sum.

s=∑j=13xjs=\sum_{j=1}^{3}x_j
Implement a sum

Implement this sum in compute. Arrays have at least four elements.

s=∑j=13xjs=\sum_{j=1}^{3}x_j

To leave the editor using only the keyboard, press Esc, then Tab.

We’re now going to focus on a very important equation in AI5:

yi=bi+∑j=0n−1Wijxjy_i = b_i + \sum_{j=0}^{n-1} W_{ij} x_j

This may look a little intimidating at first, but it’s just more of what we’ve already learned about indexes and subscripts.

We’ll take it bit by bit6. First, let’s look at yiy_i. In this equation, ii is a “free index”, in that it doesn’t vary in the summation. So mentally, just think that we’ve defined an array y and we’re setting the iith element, y[i]. bib_i is similar: we have another array, b, and again we’re accessing the iith element.

What is ii and where did it come from? Think of it as an implicit index that we need to loop over to set all the elements of yy. We use it in a few different places: yiy_i, bib_i, and WijW_{ij}. Because these all share the same ii index, we can assume that the length of yy is the same as the length of bb, and both are the same as the size of the first dimension of WW.

Now on to the sum. Let’s skip the summation itself for now and take a look at WijW_{ij}. The two indexes, ii and jj, tell us that this is a 2D array.7 In Python, we would write this simply as W[i][j] (or W[i,j] if you’re using NumPy).

Now back to the summation. Notice that we’re summing from j=0j=0 to n−1n-1. Because we’re “declaring” jj in the summation, we call it a “bound” index. We don’t specify ii in the Σ\Sigma subscript, so we’re holding it constant. Think of this as a scoping rule: ii has equation-level scope, and jj is scoped to the summation. We’re doing that MN thing of not explicitly saying what nn is, and trusting the reader to figure out from context that it’s probably the size of the second dimension of WW, or the number of elements in xx, and that these two sizes are probably the same. Because there’s no operator between WijW_{ij} and xjx_j, we use the MN default of multiplication.

We’ve spent a lot of time breaking down the equation in MN, so now let’s see how it might look in Python. Recall that we have two loops: The implicit loop over values of i, and the summation loop over values of j. We also have three variables: x, W, and b.

yi=bi+∑j=0n−1Wijxj{\htmlData{link=out}{y_{\htmlData{link=i}{i}}}} = {\htmlData{link=bias}{b_{\htmlData{link=i}{i}}}} + \sum_{\htmlData{link=j}{j}=\htmlData{link=bounds}{0}}^{\htmlData{link=bounds}{n-1}} W_{\htmlData{link=i}{i}\htmlData{link=j}{j}} x_{\htmlData{link=j}{j}}
def compute(x, W, b):
i_range = len(W) # Could also have used len(b)
y = [0] * i_range
for i in range(i_range): # Our implicit outer loop
y[i] = b[i] # First the b_i term, to which we'll add the summation
j_range = len(x) # Could also have used len(W[0])
for j in range(j_range):
pass # You get to fill in this part in the exercise below.
return y
Identify the bound index

When computing a fixed output element y_i, which index varies within the summation?

yi=bi+∑j=0n−1Wijxjy_i=b_i+\sum_{j=0}^{n-1}W_{ij}x_j
Identify the bound index
Complete an indexed computation

Supply the missing expression.

yi=bi+∑j=0n−1Wijxjy_i=b_i+\sum_{j=0}^{n-1}W_{ij}x_j
def compute(x, W, b):
    y = [0] * len(W)
    for i in range(len(W)):
        y[i] = b[i]
        for j in range(len(x)):
            y[i] += ______
    return y

W = [[2, 0, 1], [1, 3, -1]], x = [3, 1, 2], b = [1, -2].

i = 0: start y₀ at b₀ = 1.

Step 1 of 8

In programming, we rely on types to ensure that the parameters we use make sense. Even in Python, with its loose “duck typing”, the interpreter throws an exception at runtime if we do things like None + 1. Or we can use programs like mypy to do type checking before we run our program.

Similarly, in MN, we have something that is kind of like types. Specifically, our expressions have shapes. Shapes are exactly the same as the dimensions of a multidimensional array. For example, a 2D array with m rows and n columns has shape (m, n).

There are a couple of other conventions when using shapes:

  • A normal value that is not an array is a scalar, and has no shape (or we might say it has an empty shape or zero dimensions).
  • You might see the shape of a 1D array with len n written as (n,). This is because writing e.g. (5,) in Python is the expression for a tuple with one element (the element 5), and numpy’s shape property gives the shape as a tuple. MN refers to 1D arrays as vectors.
  • A 2D array is called a matrix, and an array of more than two dimensions is called a tensor.8 In fact, a tensor can have any number of dimensions - 0, 1, 2, or more. We’ll be working with tensors a lot.
  • In MN, you might see something like Rm×n\mathbb{R}^{m \times n}. This tells you not just the shape of the matrix (m rows, n columns), but also the type of elements in the matrix - ℝ is the MN notation for real numbers. You might also see integers (ℤ), natural numbers (ℕ), rational numbers (ℚ), and rarely complex numbers (ℂ).

In general, to do things like addition and subtraction, your operands have to be the same shape. Strictly speaking, in MN you can’t add a scalar ss and a vector vv. Many ML papers do it anyway (“with abuse of notation”), because it’s easier than writing the notation for adding [s,s,s,...][s, s, s, ...]. Most of the time though you’ll be working in PyTorch or numpy, and both of these support broadcasting. Broadcasting is somewhat like type coercion, in that it applies a set of rules to get otherwise incompatible shapes to work together. We won’t get into the rules here9, but as one example torch.tensor([2, 3, 4]) + 1 gives tensor([3, 4, 5]).

MN does however allow you to do multiplication and division with a scalar and a vector, for example svsv or v/sv / s.

Let’s go back to our old friend:

yi=bi+∑j=0n−1Wijxjy_i = b_i + \sum_{j=0}^{n-1} W_{ij} x_j

Here we can see that yy is a 1D vector (or tensor). If yy has mm elements, then its shape is (m,). We know from the summation that WW has nn columns, and we know it has mm rows because it shares the ii index with yy. This means that WW is a matrix with shape (m, n). Also from the summation, we know that xx is a vector with nn elements. Finally, we know that bb is a vector with mm elements because it also shares the ii index with yy.

Predict a shape

W.shape == (4, 3) and x.shape == (3,). What is the shape of y?

yi=∑j=0n−1Wijxjy_i=\sum_{j=0}^{n-1}W_{ij}x_j
Repair an allocation

This code assumes W is square. Repair it for any W with shape (m, n).

yi=∑j=0n−1Wijxjy_i=\sum_{j=0}^{n-1}W_{ij}x_j

To leave the editor using only the keyboard, press Esc, then Tab.

We’ve seen how to interpret subscripts as indexes. Now we’re going to move on to superscripts. You’re already familiar with exponents, like 232^3 or x2x^2. They’re the normal kind of repeated multiplication that you learned in school. But in MN, we often use superscripts as indexes as well. And just as with subscripts, knowing conventions is extremely helpful when trying to read ML papers and blog posts.

Let’s look at one example where we use both superscripts and subscripts as indexes:

xi(ℓ+1)=xi(ℓ)+ai(ℓ)x_i^{(ℓ+1)} = x_i^{(ℓ)} + a_i^{(ℓ)}

This is the formula for the update to a neural network’s residual stream10. The residual stream is the “backbone” of a transformer network, and helps carry information between layers. The residual stream is a matrix, but it’s better to think of it as a 2D nested array: One array for each token position in our input. As usual here we’re using ii to indicate which element of the array we’re working with, and as we just mentioned, each element of the array is itself an array.

The residual stream changes after each layer of the network, so we need a way to say which layer we’re talking about. By convention, you’ll usually see ℓ\ell used to represent the layer index11, like we do here. And usually, you’ll see the ℓ\ell in parentheses to help indicate that it’s an index rather than an exponent, but some authors don’t follow this convention.

We can also see that the formula is recursive: We define the xx vector for the next layer (layer ℓ+1\ell+1) as the sum of the output of layer ℓ\ell (a(ℓ)a^{(\ell)}) and the current xx vector (x(ℓ)x^{(\ell)}).

I want to mention one more thing that might trip you up if you haven’t seen it for a while: a T that looks like an exponent, e.g. ATA^{\mathsf{T}} or A⊤A^\top. This isn’t an exponent or an index, but rather indicates the transpose of a matrix. We’ll get into transposition later when we cover matrices in their own lesson.

Interpret a superscript

x holds the residual stream at every layer: x[0] holds the vector for every token position at layer 0, x[1] at layer 1, and so on. Which Python expression gives this value?

xi(ℓ)x_i^{(\ell)}
Interpret a superscript
Implement a residual update

x is the residual stream and a is each layer's output. Both are indexed as x[layer][i], where x[layer][i] is the vector for token position i. Return the residual stream after layer + 1, as a list with one vector for each i.

xi(ℓ+1)=xi(ℓ)+ai(ℓ)x_i^{(\ell+1)}=x_i^{(\ell)}+a_i^{(\ell)}

To leave the editor using only the keyboard, press Esc, then Tab.

Now it’s time to put everything we’ve learned together: summation, indexes, shapes, and superscripts!

We’re going to add one small new piece: just as in Python, MN has some functions that are ‘built-in’ in the sense that the convention around these functions is so strong that we can regard them as fully defined just from the function name. One of these functions is max⁡(a,b)\max(a, b) and it works exactly like Python’s max function: its value is the larger of the two arguments. When one of the arguments to max⁡\max is 0, it becomes max⁡(0,x)\max(0, x). ML gives this one-argument function a special name: a Rectified Linear Unit (ReLU)12.

Now, let’s take a look at the equations we’ll be using, and their arguments’ types (shapes). First, the arguments:

x∈Rn,W∈Rm×n,b∈Rm,V∈Rp×m,c∈Rpx \in \mathbb{R}^{n}, \quad W \in \mathbb{R}^{m \times n}, \quad b \in \mathbb{R}^{m}, \quad V \in \mathbb{R}^{p \times m}, \quad c \in \mathbb{R}^{p}

And now let’s look at the two equations we’ll use:

hi=max⁡(0,  bi+∑j=0n−1Wijxj)zk=ck+∑i=0m−1Vkihi\begin{align} h_i &= \max\left(0,\; b_i + \sum_{j=0}^{n-1} W_{ij} x_j\right) \tag{1} \\ z_k &= c_k + \sum_{i=0}^{m-1} V_{ki} h_i \tag{2} \end{align}

Equation 1, which defines hih_i, should look familiar. It’s just the yiy_i equation from Section 2, wrapped in a ReLU.

Equation 2, which defines zkz_k, is also very similar to yiy_i, with one key difference: instead of xjx_j in the summation, we use hih_i, which is the result of Equation 1.

Looking back at the arguments, we see three numbers that define the shapes of all the arguments: nn, mm, and pp. The three things that aren’t mentioned there are ii, jj, and kk. But by examining the equations, we can see that because b∈Rmb \in \mathbb{R}^{m} and bib_i is indexed by ii, then ii must range from 00 to m−1m-1. Looking at the summation in Equation 2 confirms this. Equation 1’s summation gives us the bounds for jj, which we can see ranges from 00 to n−1n-1. And finally, because ckc_k is indexed by kk and c∈Rpc \in \mathbb{R}^{p}, then kk must range from 00 to p−1p-1.

Let’s look at Equation 1 in a little more detail. Compare the equation from Section 2 and this new equation:

yi=bi+∑j=0n−1Wijxjhi=max⁡(0,  bi+∑j=0n−1Wijxj)\begin{align} y_i &= b_i + \sum_{j=0}^{n-1} W_{ij} x_j \tag{Section 2} \\ h_i &= \max\left(0,\; b_i + \sum_{j=0}^{n-1} W_{ij} x_j\right) \tag{1} \end{align}

You can see that the two equations are exactly the same, except for the ReLU (the max⁡(0,...)\max(0, ...)). Just as with Section 2’s output yy, Equation 1’s output hh is a vector. And we know that hh has mm elements, since it’s indexed by ii.

Now that we understand Equation 1, let’s take a look at Equation 2. It’s almost exactly the same as the Section 2 equation - the only difference is that we have to take the hh from Equation 1 and plug it into the summation. Since the output of Equation 2, zz, is indexed by k we know that zz will have pp elements.

We can read this pair of equations in order like lines of a program. Because zkz_k sums over all elements of hh, we need to calculate Equation 2 after Equation 1. Doing Equation 1 and then Equation 2 is just like writing two functions, where the second function takes as a parameter the output of the first function.

x∈Rn  ⟶  h∈Rm  ⟶  z∈Rpx \in \mathbb{R}^{n} \;\longrightarrow\; h \in \mathbb{R}^{m} \;\longrightarrow\; z \in \mathbb{R}^{p}

We’ve said before that shapes kind of work like types. Let’s do our own “type checks” on the two equations.

First, let’s look at Equation 1. We know its output is going to be hh with one dimension of size mm based on our previous analysis of the range of ii. bb is also the same size, for the same reason. In the summation, we know that WW has shape m×nm \times n, and we’re doing two loops: one walking over each row ii of WW, and one walking over each column jj of that row. xx is also indexed by jj, so we know xx must have the same number of elements as WW has columns.

The sum gives us one number for each ii, and bb also has one value for each ii, so adding them is valid. Applying max⁡\max doesn’t change the shape, so Equation 1 passes the type check!

Equation 2 is the same, just with different subscript letters. Once again we’re doing two loops: one walking over each row kk of VV, and one walking over each column ii of that row. In the summation, hh is also indexed by ii, so we know hh must have the same number of elements as VV has columns. The sum gives us one number for each kk, and cc also has one value for each kk, so adding them is valid. Equation 2 passes the type check!

So far, we’ve been thinking of MN as a programming language, but another way to think about it is as a specification. That is, the equations tell us what to compute, but not how to compute it. We’ve been talking about nested loops, but you could also do the exact same calculation using NumPy functions or a GPU kernel written in CUDA. In Lesson 2, we’ll see that each equation can be simplified using matrix multiplications, and the two equations shrink to:

h=max⁡(0,  Wx+b),z=Vh+ch = \max(0,\; Wx + b), \quad z = Vh + c

Now try it yourself. First, work out hh by hand for a small input. Then write the code for both equations.

Predict the hidden values

With x = [1, 2], W = [[1, -1], [0, 2], [-1, 1]], and b = [0, -1, 0], what is h?

hi=max⁡(0,bi+∑j=0n−1Wijxj)h_i=\max\left(0,b_i+\sum_{j=0}^{n-1}W_{ij}x_j\right)
Implement the forward pass

Implement both equations and return z. Don't assume any fixed dimensions.

hi=max⁡(0,bi+∑j=0n−1Wijxj)zk=ck+∑i=0m−1Vkihi\begin{aligned}h_i&=\max\left(0,b_i+\sum_{j=0}^{n-1}W_{ij}x_j\right)\\z_k&=c_k+\sum_{i=0}^{m-1}V_{ki}h_i\end{aligned}

To leave the editor using only the keyboard, press Esc, then Tab.

We’ve completed a refresher of basic mathematical notation, and started to get a feel for how to read MN and turn it into a program.

Now here’s a challenge problem to check your understanding! It combines the residual stream ideas from section 4 with the kinds of weighted sums we’ve been working on for most of this lesson.

If you have trouble, refer back to the earlier exercises you completed and see how you can use them to solve this problem.

Final check

Implement this equation in the compute function. x has shape (n,), and W has shape (L, n, n). W[layer] is the weight matrix for that layer. Start with x(0)=xx^{(0)} = x, apply the update for each layer ℓ from 0 to L - 1, and return x(L)x^{(L)}, with shape (n,).

xi(ℓ+1)=xi(ℓ)+∑j=0n−1Wij(ℓ)xj(ℓ)x_i^{(\ell+1)}=x_i^{(\ell)}+\sum_{j=0}^{n-1}W_{ij}^{(\ell)}x_j^{(\ell)}

To leave the editor using only the keyboard, press Esc, then Tab.

Now for some debugging practice! In the Formula Forensics game, each problem gives you a mathematical formula and code that implements it incorrectly. As a programmer, you know what to do: Find the bug and fix it!

Companion game

Formula Forensics

Use each specification to find and repair the bug in a numerical function.

Play Formula Forensics

So far we’ve treated vectors like arrays or lists: just a series of numbers. But you’re here to learn mechanistic interpretability!13 In the next lesson, we’ll take an interpretability view of a vector. We’ll see that we can treat a vector as a direction, and how that direction relates to features that a model might have learned.

  1. I hope you’re okay with me calling mathematical notation “MN.” We programmers have a long tradition of naming languages things like C, C++, C#, D, F, so MN fits right in. ↩

  2. Not using multi-letter names also means they tend to dip into Greek when they run out of Latin characters. We’ll talk more about that in a later lesson. ↩

  3. This fact causes mathematicians no end of self-inflicted pain, as expressed in common mathematical laments like “with abuse of notation” and “subscripts omitted for simplicity.” ↩

  4. For example, see the discussion of “mathiness” in Troubling Trends in Machine Learning Scholarship. ↩

  5. Each yiy_i is the output of a neuron in a neural network, before any nonlinearity. Later, we’ll see how to represent this equation more concisely with matrix math. ↩

  6. Get it? Bit! Because we’re programmers! So funny! ↩

  7. Mathematicians really like single letters, and they like to concatenate them and trust that you’ll figure out the meaning from context. When you see multi-character things like ijij, it’s almost always a bunch of single-character variables rather than a multi-character variable. ↩

  8. This is different than what mathematicians mean by a tensor. You may run into mathematical tensors occasionally when reading ML papers, but it’s pretty rare, and the “n-dimensional array” meaning is more common in ML. ↩

  9. You can read the full rules here. If you think you’ve really mastered the broadcasting rules, you might want to try these puzzles. Or you might decide not to worry about it; this is the kind of thing coding agents are really good at. ↩

  10. We’ll get into the details of what a residual stream is much later, after we’ve built up some more preliminaries. Suffice it to say, it’s an extremely important piece of modern deep learning and mechanistic interpretability. ↩

  11. We use ℓℓ instead of ll to avoid confusion with the number 11. ↩

  12. ReLUs are used in many older LLMs, though most recent LLM architectures replace ReLU with other similarly-shaped functions instead. We’ll get to why ReLUs are useful in a later lesson. ↩

  13. At least I hope so. This whole thing has been rather awkward for us otherwise. ↩

Your progress is saved in this browser, on this device only. It is never uploaded, and it will be lost if you clear your browser's storage.