Learn machine learning from scratch

Start at chapter 1 and build up: what a model is, how it measures its own mistakes, and how it learns. Everything is worked out in plain Python, with the maths shown rather than assumed. Two shorter tracks hold reference notes on the maths and on modern architectures.

Track Progress: 0 Completed
0%
Maths for MLLEVEL 1 Β· BEGINNERModule 1 Β· Vectors

The dot product and the angle between vectors

Multiply matching entries and add: one number that is big when two vectors point the same way, zero at right angles, and the heart of every neuron and every attention score.

10 min Prerequisites: Lesson 6 (vectors as arrows)

1. The idea

The dot product multiplies matching entries and adds them up: πšΒ·π› = a₁b₁ + aβ‚‚bβ‚‚ + … + a_d b_d. One number comes out. You have met it already: it is the weighted sum inside every neuron, 𝐰ᡀ𝐱.

The same number has a meaning you can see: πšΒ·π› = β€–πšβ€– ‖𝐛‖ cos ΞΈ, where ΞΈ is the angle between the arrows. Pointing the same way, cos ΞΈ = 1 and the dot product is as large as it can be for those lengths. At right angles, cos ΞΈ = 0 and the dot product is 0. Pointing apart, it is negative.

Divide out the lengths and you get cosine similarity, cos ΞΈ = πšΒ·π› / (β€–πšβ€– ‖𝐛‖): a number between βˆ’1 and 1 that ignores how long the vectors are and keeps only their direction. It is how a search engine finds the sentence embeddings closest to a query, and the score attention gives each word is a dot product between a query vector and a key vector.

2. Try it: turn two arrows

Drag the two tips. Watch the sign of πšΒ·π› as the angle passes 90Β°. The purple bar is the shadow of 𝐛 on 𝐚: its length times β€–πšβ€– is the dot product.

Two draggable arrows a and b from the origin with an arc marking the angle between them. A dashed line drops from the tip of b onto the line through a, and the part of a's line up to that point is highlighted as the projection. Below: aΒ·b worked out, both lengths, cos ΞΈ and the angle in degrees.

3. The math

Multiply and add
aβ‹…b=βˆ‘i=1daibi=a⊀b\mathbf{a}\cdot\mathbf{b} = \sum_{i=1}^{d} a_i b_i = \mathbf{a}^\top\mathbf{b}
Same length vectors in, one number out.
Lengths and the angle
aβ‹…b=βˆ₯aβˆ₯ βˆ₯bβˆ₯cos⁑θ\mathbf{a}\cdot\mathbf{b} = \|\mathbf{a}\|\,\|\mathbf{b}\|\cos\theta
β€–πšβ€– is the length of 𝐚 (next lesson).
Cosine similarity
cos⁑θ=aβ‹…bβˆ₯aβˆ₯ βˆ₯bβˆ₯\cos\theta = \frac{\mathbf{a}\cdot\mathbf{b}}{\|\mathbf{a}\|\,\|\mathbf{b}\|}
1: same direction. 0: unrelated (at right angles). βˆ’1: opposite.
Right angles
aβŠ₯bβ€…β€ŠβŸΊβ€…β€Šaβ‹…b=0\mathbf{a} \perp \mathbf{b} \iff \mathbf{a}\cdot\mathbf{b} = 0
Perpendicular, or orthogonal: the dot product is zero.
Projection: the shadow of b on a
proj⁑ab=aβ‹…baβ‹…a a\operatorname{proj}_{\mathbf{a}}\mathbf{b} = \frac{\mathbf{a}\cdot\mathbf{b}}{\mathbf{a}\cdot\mathbf{a}}\,\mathbf{a}
The part of 𝐛 that points along 𝐚.

4. The code (python)

mm1_dot.py
import numpy as np

a = np.array([3.0, 1.0])
b = np.array([1.0, 2.5])
print(a @ b)                       # 5.5

cos = (a @ b) / (np.linalg.norm(a) * np.linalg.norm(b))
print(round(cos, 3), round(np.degrees(np.arccos(cos)), 1))   # 0.646 49.8

def cosine(u, v):
    return (u @ v) / (np.linalg.norm(u) * np.linalg.norm(v))

# three made-up word embeddings
cat = np.array([0.9, 0.1, 0.3])
kitten = np.array([0.8, 0.2, 0.35])
car = np.array([0.1, 0.9, -0.2])
print(round(cosine(cat, kitten), 3), round(cosine(cat, car), 3))   # 0.989 0.136

5. You’ll see this in

6. Check yourself

Answer all 4 questions correctly to complete the chapter Β· 0 / 4 done
Q1/4 What is (1, 2, 3) Β· (4, 0, βˆ’1)?
Q2/4 Two non-zero vectors have a dot product of 0. What does that tell you?
Q3/4 What is the cosine similarity between 𝐚 and 3𝐚?
Q4/4 Which part of Ε· = 𝐰ᡀ𝐱 + b is a dot product?

Finished this chapter?

Weekly newsletter

What changed in ML architectures this week

New models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.