Index Ai Academy
Foundation · Foundation mathVectors and matrices

The dot product

About 43 minLooking ahead: some prerequisites aren't done yet

Why this matters

The dot product turns two lists of numbers into one number: multiply them pair by pair and add. It's a weighted total, like the bill for a shopping basket, amounts times prices. It's also the single most important operation in machine learning. One neuron computes exactly one dot product, a layer computes many, and a transformer spends most of its time doing them. This lesson builds it by hand, three ways in Python, and as a neuron in NumPy and PyTorch. It also meets two facts that the rest of the module leans on: a vector dotted with itself gives its squared length, and two vectors whose dot product is 0 meet at a right angle.

  • Compute a dot product: multiply matching components and add
  • Read it as a weighted total, like a shopping bill, and as a shadow whose sign says whether two arrows lean together; know that a·a is the squared length
  • Compute it three ways in Python, a loop, sum(a * b) and a @ b, and check the lengths first
  • See one neuron's calculation, z = w·x + b, as a dot product plus a bias, and get the same z from PyTorch's nn.Linear
Quick check
Have a guess before you start. You buy 3 apples at $1.50, 2 loaves at $4 and 5 lemons at $0.80. What's the bill?

The math: multiply in pairs, then add

Purpose. One number that says how much two lists agree, weighted.

The dot product of two vectors of the same length multiplies matching components and adds the results. For a = (2, 3, 1) and b = (4, 1, 5):

a⋅b=2×4+3×1+1×5=16\mathbf{a} \cdot \mathbf{b} = 2 \times 4 + 3 \times 1 + 1 \times 5 = 16

In plain words: pair them up, multiply each pair, add the products.

The shopping bill is the everyday case: amounts · prices. The answer is a single number, a scalar, not a vector. The two vectors must be the same length, or some numbers have no partner.