Algorithm Search⌘K

Definitionintro

Word-context matrix

Also called co-occurrence matrix, term-context matrix.

The matrix whose entry (w, c) counts how often word w occurs near context c in a corpus, the raw material of count-based word vectors.

A word-context matrix has one row per word of a vocabulary and one column per context, and its entry (w,c)(w, c) counts how often ww occurs with cc in a corpus. A context is usually a word within a window of kk tokens, but it can also be a document or a syntactic relation to another word. The rows are the simplest word vectors of the distributional hypothesis: two words with similar rows occur in similar contexts. The counts are almost never used as they are. A weighting such as PPMI comes first, and often a low-rank reduction such as latent semantic analysis. A page or paper that uses such a matrix should say what a context is, the window size, whether the window is weighted, and whether it crosses sentence boundaries, because each of these changes the similarities.

Definition

Let a corpus be a list of sentences, each a list of tokens t1,…,tnt_1, \ldots, t_n, and let WW be the vocabulary of target words and CC the set of contexts. For window contexts with C=WC = W and a window size k≥1k \ge 1, the entry is

M[w,c]=∑sentences#{(i,j):ti=w, tj=c, 1≤∣i−j∣≤k}.M[w, c] = \sum_{\text{sentences}} \#\{ (i, j) : t_i = w,\ t_j = c,\ 1 \le |i - j| \le k \}.

A weighted window adds g(∣i−j∣)g(|i - j|) for each pair instead of 1, with a weight gg that falls with the distance dd, for example g(d)=1/dg(d) = 1/d, or g(d)=(k−d+1)/kg(d) = (k - d + 1)/k, which is the average weight of the dynamic window of word2vec (Levy, Goldberg and Dagan 2015). An asymmetric window counts only j>ij > i or only j<ij < i, and a positional context keeps the offset, so that "the" at offset −1-1 and "the" at offset +1+1 are different columns. For document contexts, CC is the set of documents and M[w,d]M[w, d] is the number of occurrences of ww in dd, which gives the term-document matrix of information retrieval. For syntactic contexts, a column is a pair (relation, word) from a dependency parse, such as (object of, drink) for "milk".

The total is N=∑w,cM[w,c]N = \sum_{w, c} M[w, c], the number of word and context pairs, and the row and column sums are M[w,∗]M[w, *] and M[∗,c]M[*, c]. For an unweighted symmetric window, MM is symmetric, and a sentence of nn tokens adds 2∑d=1kmax⁡(n−d,0)2 \sum_{d=1}^{k} \max(n - d, 0) to NN. The empirical probabilities P(w,c)=M[w,c]/NP(w, c) = M[w, c] / N, P(w)=M[w,∗]/NP(w) = M[w, *] / N and P(c)=M[∗,c]/NP(c) = M[*, c] / N are the input of pointwise mutual information.

Examples and non-examples

A window of 1. Take the five sentences "the cat drinks milk", "the dog drinks water", "a cat eats fish", "a dog eats meat" and "the child drinks milk", with k=1k = 1 and no context across sentences. Each sentence of 4 tokens gives 6 pairs, so N=30N = 30. Some rows of the 11×1111 \times 11 matrix, with the columns that are not zero in any of them:

word the a drinks eats cat dog child milk water fish meat
cat 1 1 1 1 0 0 0 0 0 0 0
dog 1 1 1 1 0 0 0 0 0 0 0
child 1 0 1 0 0 0 0 0 0 0 0
drinks 0 0 0 0 1 1 1 2 1 0 0
milk 0 0 2 0 0 0 0 0 0 0 0

The rows of "cat" and "dog" are equal, so their cosine is 1. The column of "cat" is the row of "cat", since the window is symmetric. The column sums are the counts of each word as a context: "drinks" has 6, because it occurs three times with a neighbor on each side.

A window of 2. With k=2k = 2 the same corpus has 2⋅(3+2)=102 \cdot (3 + 2) = 10 pairs per sentence and N=50N = 50, and "cat" now also counts "milk" and "fish". A larger window adds pairs that are further apart and more about the topic than about the grammar.

A term-document matrix. With the five sentences as documents, the row of "cat" is (1,0,1,0,0)(1, 0, 1, 0, 0) and the row of "dog" is (0,1,0,1,0)(0, 1, 0, 1, 0). Their cosine is 0: as documents, the two words never meet, although they have the same neighbors. The choice of context changes which words are similar.

Not the same matrix: a window across sentences. If the window runs across sentence boundaries, "milk" at the end of one sentence counts "the" at the start of the next. The result is the word-context matrix of the corpus read as one long sentence, which is a different matrix from the one above, so the definition of a matrix must say which one it is.

Not: a similarity matrix. The V×VV \times V matrix of cosines between the rows is computed from a word-context matrix, but its entries are not counts, and it is not one.

Not: an embedding matrix. The dense vectors of the skip-gram model or GloVe are learned from the same counts, but the matrix of these vectors has a few hundred learned columns that are not contexts.

  • Weightings. Pointwise mutual information replaces each count by log⁡P(w,c)P(w)P(c)\log \frac{P(w, c)}{P(w) P(c)}, and PPMI keeps its positive part. tf-idf is the usual weighting of a term-document matrix.
  • Sparsity. By Zipf's law, most words are rare, and most entries of the matrix are 0. It is stored as a sparse matrix or a hash map of the pairs that occur, and its size is the number of distinct pairs, not ∣W∣⋅∣C∣|W| \cdot |C|.
  • Reduction. A truncated singular value decomposition of the weighted matrix gives dense vectors of a few hundred dimensions. Applied to a term-document matrix this is latent semantic analysis.
  • Similarity. Rows are compared with cosine similarity, which does not depend on the length of a row and so not directly on the frequency of the word.
  • Class bigram counts. Brown clustering uses the matrix of an ordered window of size 1, the bigram counts n(u,w)n(u, w), and sums its rows and columns over classes.

References

  1. Turney and Pantel 2010, the term-document, word-context and pair-pattern matrices, and the steps of building, weighting and comparing them
  2. Levy, Goldberg and Dagan 2015, the window size and the dynamic context windows that weight a context at distance d by 1/d or by (L - d + 1)/L
  3. Daniel Jurafsky and James H. Martin, Speech and Language Processing, third edition draft, the chapter on vector semantics and embeddings, the sections on term-document and term-context matrices

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/HJM1EQ/text.yaml