Fig. 1 in words
Four readings of one sky · 400 000 words × 50 numbers · same data as code 05
1 Angle from “cat”
- cat at 0° · dog 22.8° · kitten 50.3° · car 68.7° · democracy 87.9°
- a tick at 30°
- one-hot: every pair at 90°
2 Country → capital
- 12 capitals · 21–45°
- random 77–109°
- mean ↑ dashed
- cos 0.61 against 0.02
3 King - man + woman
- b − a + c, nearest by cosine
- king 0.885 (an input word) · queen 0.852 · throne 0.766
- man : doctor :: woman : ?
- doctor 0.905 (an input word) · nurse 0.840
- scale 0.70 · 0.80 · 0.90
- hatched = an input word
- skip the inputs → queen, nurse
4 One point per word
- hot at the centre · around it 1 cool · 2 cold · 3 mix · 4 dry · 5 soft · 6 warm
- rings at 30° and 40°
- bank → river: no. 2 353
- cos 0.414 · every sense, one point
01 One-Hot to Dense Represent
- One-hot: one 1 in 400 000 slots; every pair at 90°, so dog is no closer to cat than democracy
- Dense: 50 learned numbers; cat–dog 22.8°, cat–democracy 87.9°
- Learned inside a language model against “the curse of dimensionality” (Bengio et al. 2003)
Normalise first: cosine reads direction, not length.
02 The Company It Keeps Use
- Harris (1954): “difference of meaning correlates with difference of distribution”
- Firth (1957): “You shall know a word by the company it keeps!”
- Count neighbours, reweight by PMI, compress by SVD (Deerwester et al. 1990)
Meaning here is a pattern of use; the vectors never see the world.
03 Predict or Count W2V · GloVe
- Skip-gram: vectors “good at predicting the nearby words” (Mikolov et al. 2013)
- GloVe: dot products fitted to log co-occurrence counts (Pennington et al. 2014)
- With negative sampling it implicitly factorises shifted PMI (Levy & Goldberg 2014)
Tuning explains much of the gap (Levy et al. 2015).
04 Reading Analogies 3CosAdd
- king − man + woman: the nearest word is king (0.885); queen (0.852) is second
- Queen wins only if the inputs are skipped: “we discard the input words” (Mikolov et al.)
- man : doctor :: woman : ? gives doctor (0.905); nurse needs the skip (Nissim et al. 2020)
Inputs allowed: 3CosAdd falls 0.71 → 0.21 (Nissim et al.); Linzen (2016) got an input back 98% of the time.
05 Try It Yourself PYTHON 3.13.16
sprint33_meaning_as_vectors_word_embeddings.py
import numpy as np, gensim.downloader as api
wv = api.load("glove-wiki-gigaword-50") # 66 MB, once
V, w, ix = wv.vectors, wv.index_to_key, wv.key_to_index
E = V / np.linalg.norm(V, axis=1)[:, None] # unit rows
u = lambda t: E[ix[t]]
print(len(w), "words x", E.shape[1], "numbers")
for t in ["dog", "kitten", "car", "democracy"]: # 1
c = u("cat") @ u(t) # cosine as an angle
print(f"cat-{t}: cos {c:.3f} = "
f"{np.degrees(np.arccos(c)):.1f} deg")
P = """france paris italy rome spain madrid germany
berlin japan tokyo russia moscow greece athens egypt
cairo morocco rabat turkey ankara china beijing
poland warsaw""".split() # 2 country -> capital
def mcos(D): # mean cosine between the offsets
D = D / np.linalg.norm(D, axis=1)[:, None]
return (D @ D.T)[np.triu_indices(len(D), 1)].mean()
C = np.array([u(t) for t in P])
D = C[1::2] - C[::2] # 12 country -> capital offsets
r = np.random.default_rng(33).integers(1000, 20000,
(12, 2))
R = E[r[:, 1]] - E[r[:, 0]] # 12 random word pairs
print(f"offsets: capitals {mcos(D):.2f}, "
f"random pairs {mcos(R):.2f}")
def top(q, x=(), k=2): # 3 analogy by 3CosAdd
s = E @ q / np.linalg.norm(q)
o = [i for i in np.argsort(-s)[:9] if w[i] not in x]
return " ".join(f"{w[i]} {s[i]:.3f}" for i in o[:k])
for a, b, c in [("man", "king", "woman"),
("man", "doctor", "woman")]:
q = u(b) - u(a) + u(c) # x = skip the 3 inputs
print(f"{b}: {top(q)} | skip: {top(q, {a,b,c}, 1)}")
o = np.argsort(-(E @ u("hot"))) # 4 one point per word
print("hot ->", " ".join(w[i] for i in o[1:7]))
n = list(np.argsort(-(E @ u("bank")))).index(ix["river"])
print("bank -> river is neighbour no.", n)
Output, as printed on the image
Output printed on the image:
400000 words x 50 numbers
cat-dog: cos 0.922 = 22.8 deg
cat-kitten: cos 0.639 = 50.3 deg
cat-car: cos 0.364 = 68.7 deg
cat-democracy: cos 0.037 = 87.9 deg
offsets: capitals 0.61, random pairs 0.02
king: king 0.885 queen 0.852 | skip: queen 0.852
doctor: doctor 0.905 nurse 0.840 | skip: nurse 0.840
hot -> cool cold mix dry soft warm
bank -> river is neighbour no. 2353
Run it. sprint33_meaning_as_vectors_word_embeddings.py · versions, data and steps
Check: arccos 0.922 = 22.8° · arccos 0.037 = 87.9° · 0.885 − 0.852 = 0.033 · gensim 4.4.0 · numpy 2.5.3
06 General Notes Practice
- Close is not same: cold is hot’s 2nd neighbour; SimLex-999 rates similarity, not association (Hill et al. 2015)
- One point per word: river is bank’s 2 353rd neighbour
- Vectors learn their corpus, stereotypes included (Caliskan et al. 2017)
Next: attention shapes each word’s vector by its sentence.
07 Mini Project Hands-on
- Build PPMI + SVD vectors from one book; compare cat’s neighbours with GloVe’s
- Run 10 analogies with and without the skip; count the input answers
- Find antonyms closer than synonyms; list the neighbours of “spring”
Check: a one-hot pair always gives 90.0 deg.
R Trusted Sources
- Harris (1954) Distributional Structure, Word 10(2–3), 146–162
- Firth (1957) Studies in Linguistic Analysis, Blackwell, 1–32
- Deerwester et al. (1990) JASIS 41(6), 391–407
- Bengio et al. (2003) JMLR 3, 1137–1155
- Mikolov, Sutskever, Chen, Corrado & Dean (2013) NeurIPS 26
- Pennington, Socher & Manning (2014) GloVe, EMNLP, 1532–1543
- Levy & Goldberg (2014) NeurIPS 27
- Levy, Goldberg & Dagan (2015) TACL 3, 211–225
- Linzen (2016) RepEval, 13–18
- Nissim, van Noord & van der Goot (2020) CL 46(2), 487–497
- Hill, Reichart & Korhonen (2015) SimLex-999, CL 41(4), 665–695
- Caliskan, Bryson & Narayanan (2017) Science 356(6334), 183–186
