Contents Projects Code Course home

Chapter 30Project 3: Embedding Forensics

What you will build

The geometry toolkit that a practitioner needs: cosine similarity, nearest neighbours and analogy solving. Then you use it to identify unknown vectors, audit analogies on a small space, and measure an association bias with a simplified WEAT score. The space is small on purpose, so that failures are visible.

Allow eight to ten hours. Read Chapters 6, 7 and 9 first.

What you need

A word space in a plain text file, one word per line followed by its numbers. Build your own with the pipeline of Chapter 7. Count co-occurrences in a window of five words on each side, over a corpus of a few thousand documents. Weight by PPMI. Reduce to fifty dimensions with a truncated SVD, and keep the three thousand most frequent words. Use only the Python standard library for the toolkit.

The conventions

Match these exactly, because they decide the answers. A different tie rule or a different exclusion rule gives different answers.

  • cos⁡(u,v)=u⋅v/(‖u‖‖v‖)\cos(u, v) = u \cdot v / (\lVert u \rVert\,\lVert v \rVert). If either norm is zero, return 0.00.0.

  • nearest(word, space, k) returns the kk words with the highest cosine to the word, excluding the word itself, with the highest cosine first and ties broken alphabetically.

  • analogy(a, b, c, space, k) returns the kk nearest words to vb−va+vcv_b - v_a + v_c, excluding aa, bb and cc, with the same ordering.

  • For target words TT and attribute sets AA and BB the simplified WEAT score is s=1|T|∑t∈T[meana∈Acos⁡(t,a)−meanb∈Bcos⁡(t,b)].s = \frac{1}{|T|}\sum_{t \in T}\Big[\operatorname*{mean}_{a\in A}\cos(t,a) - \operatorname*{mean}_{b\in B}\cos(t,b)\Big].

Stages

  1. Build the toolkit. Write cosine, nearest and analogy for a space stored as a dictionary from word to list of floats. Handle the zero vector and ties deliberately.

    Check. In the space with a=(1,0)a = (1, 0), b=(0,1)b = (0, 1), c=(1,1)c = (1, 1) and d=(1,0)d = (1, 0), the neighbours of aa are dd with cosine 11, then cc with 0.70710.7071, then bb with 00. For analogy, take man (1,0)(1, 0), woman (0,1)(0, 1), king (3,0)(3, 0), queen (3,1)(3, 1) and prince (1,2)(1, 2). The analogy “man is to king as woman is to what” asks for the word nearest (2,1)(2, 1), and the answer is queen at cosine 0.98990.9899, ahead of prince at 0.80.8. Check that none of the three query words is returned.

  2. Mystery vectors. Have a friend, or a script, take five words from your space and add a little noise to each vector. Identify them using only your toolkit. The nearest neighbour gives candidates, and the neighbourhood of each candidate says which is right. Write down your procedure for the word you were least sure of.

  3. Audit the analogies. Choose five queries of the form (a,b,c)(a, b, c) and compute the top result of each. Some will look sensible and some absurd. The space was built from a few thousand documents and not billions of tokens, so say what you notice about which relations survive.

  4. A bias probe. Choose target words and two attribute sets, for example institutional words against narrative words, and compute the simplified WEAT score to four decimals. Say what its sign means for your targets, and how a corpus can produce it although no single word is biased on its own.

Extensions

  • Rebuild the space with a window of one word and compare which analogies survive.

  • Add the exclusion trap on purpose, by allowing the query words in the answer, and measure how much the score of your analogies rises.

  • Compute the full WEAT effect size with a permutation test and compare it with the simplified score.

From Natural Language Processing: From Corpus Statistics to Grounded LLMs, by Ramaseshan Ramachandran.