Zhanbo Chen
Back to home

CS546 Learning Notes: From Co-occurrence Matrix to Word2Vec, Connecting Negative Sampling and Language Models

Learning Notes/Algorithm
2026-09-27
21 min read
0 reads
AIMachine Learning

In brief

Reviewing Lesson 4 on Language Models & Word Embeddings, I found that some concepts seem familiar at first glance, but when asked questions like "what is one dimension of this matrix" or "what probability does this actually predict", I still get stuck. This post organizes problems I encountered during review: first understanding concepts in my own words, then matching them with small examples and formulas to see if I've confused two things together. The numerical examples in this article are meant to help explain concepts, not experimental results.

1. Distributional meaning: Is it just about word frequency?

My initial question was: does distributional meaning simply refer to frequency of occurrence?

My current understanding is that it focuses on the contexts in which a word appears. Word frequency can be used to count these patterns, but looking only at how many times a word appears in total does not reveal its meaning.

Suppose cat appears 100 times and computer also appears 100 times—this does not mean the two words are similar. Even though cat and dog may have different total frequencies, they could both appear near feed, pet, fur. This distribution across contexts is what determines whether they are related.

The phrase "the company it keeps" in the lecture notes refers to observing which words frequently co-occur with a given word.

How to read a word-word co-occurrence matrix?

Target word / Context worddigitaldatasugar
computer210
information160
apricot001

Each row represents a target word, each column represents a context word, and each cell contains the number of co-occurrences within a specified window.

So when a question asks "a dimension of a row vector represents what," it is asking what corresponds to a position. The answer is context word. A dimension corresponds to digital; the value 2 in that dimension represents the co-occurrence count.

It is important to clarify: two words having similar contexts does not require the two words themselves to appear next to each other frequently.

2. TF-IDF: count(t,d), df, and the final value

I initially found these symbols confusing, but they actually represent statistics over two different scopes:

SymbolMeaning
ttterm, a word
dddocument
count⁡(t,d)\operatorname{count}(t,d)number of times word tt appears in document dd
df⁡t\operatorname{df}_tnumber of documents containing word tt
NNtotal number of documents

For example, if apple appears 100 times in an article, it contributes 100 to count but only 1 to df.

The TF used in the lecture notes is log-transformed:

tf⁡t,d={1+log⁡10count⁡(t,d),count⁡(t,d)>00,count⁡(t,d)=0\operatorname{tf}_{t,d}= \begin{cases} 1+\log_{10}\operatorname{count}(t,d), & \operatorname{count}(t,d)>0\\ 0, & \operatorname{count}(t,d)=0 \end{cases}

Higher occurrence frequency yields higher TF, but the logarithm prevents growth from being too fast. When a word does not appear, TF is set to 0, avoiding log(0).

idf⁡t=log⁡Ndf⁡t\operatorname{idf}_t=\log\frac{N}{\operatorname{df}_t}

IDF is exactly as I confirmed before: "log(total number of documents / number of documents containing this word)". A word appearing in all documents would have IDF of 0 under this unsmoothed definition.

Finally, multiply the two:

wt,d=tf⁡t,didf⁡tw_{t,d}=\operatorname{tf}_{t,d}\operatorname{idf}_t

This gives the TF-IDF weight of the word in this document. A word that is frequent in the current article but rare in others typically does a better job of distinguishing that article.

The TF-IDF matrix is commonly arranged as "documents × terms," but can also be transposed to "terms × documents." Each cell stores a weight; what the rows and columns represent depends on the labels.

3. PPMI, cosine, and SVD: What does the matrix become?

A PPMI matrix can still be "target word × context word," just replacing co-occurrence counts with association scores:

PMI⁡(w,c)=log⁡2P(w,c)P(w)P(c)\operatorname{PMI}(w,c)=\log_2\frac{P(w,c)}{P(w)P(c)} PPMI⁡(w,c)=max⁡(PMI⁡(w,c),0)\operatorname{PPMI}(w,c)=\max(\operatorname{PMI}(w,c),0)

The denominator P(w)P(c)P(w)P(c) is the joint probability assuming independence. A ratio greater than 1 means actual co-occurrence is more frequent than under the independence assumption, so PMI is positive. Negative values are clipped to 0; word pairs without co-occurrence are typically also recorded as 0 in the PPMI table.

Target word / Context wordeatfruitcomputer
apple2.13.00
banana1.82.70
software003.2

This is an example table. PPMI weighting itself does not reduce the number of rows or columns, nor does it necessarily solve the problem of unreliable statistics for low-frequency words.

Cosine similarity: Is closer to 1 better?

cos⁡(p,q)=pTq∥p∥∥q∥\cos(p,q)=\frac{p^Tq}{\|p\|\|q\|}

It compares the direction of two non-zero vectors, with values in [−1,1][-1,1]: 1 for same direction, 0 for perpendicular, −1 for opposite direction. For non-negative co-occurrence counts or PPMI vectors, similarity lies in [0,1][0,1]; cosine is undefined for zero vectors.

When I asked if "closer to 1 is better," the more accurate understanding is: closer to 1 means the vectors are more similar. If the task is to find similar words, this is the signal we care about, but it does not mean we should push all pairwise similarities toward 1. Antonyms can also share many contexts and have high similarity; high similarity does not guarantee synonymy.

What does SVD compress into what?

Suppose a PPMI matrix has 10,000 target words and 10,000 context dimensions:

M∈R10000×10000M\in\mathbb R^{10000\times10000}

Truncated SVD retains the largest k=100k=100 singular values and their corresponding directions:

M≈UkΣkVkTM\approx U_k\Sigma_kV_k^T

We can take UkΣkU_k\Sigma_k as the word vector matrix, which becomes 10000×10010000\times100. The number of words remains unchanged; each word goes from 10,000 dimensions to 100 dimensions, yielding a typically dense low-dimensional representation.

Originally each column corresponded to a context word; after compression, the columns are combined features, typically not directly nameable as eat or fruit. This is a low-rank approximation that discards some information and is not lossless compression.

Additionally, the vocabulary symbol ∣V∣|V| denotes the size of a set, not an absolute value. For the corpus apple banana apple orange, ∣V∣=3|V|=3, with repeated words counted only once. The VkV_k in the SVD formula is a decomposition matrix and is not the same as the vocabulary VV.

4. Why dense representations might be better: car and automobile

The lecture notes say car and automobile are synonyms, but in sparse vectors they are two independent dimensions. I needed to work through this example concretely.

Suppose we only look at two context dimensions:

Target wordcarautomobile
repair100
fix010

The cosine of the two vectors is 0. Although both contexts are discussing cars, the counts fall in different positions, and a direct comparison fails to recognize the association.

Dense representations can learn the relationships between these contexts, allowing similar usage patterns to provide similar information. The point is not that sparse representations cannot handle synonyms, but that independent count dimensions may underestimate such similarity.

5. Word2Vec has no manual labels—how does it train?

"No manual labels" and "no targets during training" are different things. Word2Vec constructs prediction tasks automatically from text, so it can be called self-supervised learning, and is often classified as unsupervised learning in a broad sense. When the lecture notes say "in a supervised fashion," it refers to having prediction targets, loss functions, and parameter updates.

TaskInputPrediction target
CBOWsurrounding wordscenter word
Skip-gramcenter wordsurrounding words
Autoregressive language modelingpreceding wordsnext word

For example, in we enjoy learning language models, with learning as center and window radius 1, positive samples are:

  • (learning, enjoy)
  • (learning, language)

I once selected (enjoy, language), but that actually pairs two context words together. In Skip-gram, each positive sample is a center word with one context word within the window. When training through the entire sentence, the center position moves; not every position is centered on learning.

6. The two sets of vectors are not one-hot and embedding

This is what I repeatedly clarified this time. Following the arrangement in the lecture notes:

ObjectShapeFunction
one-hot xx∥V∥×1\|V\|\times1specifies which word; not a learned semantic vector
input weight WW∥V∥×d\|V\|\times deach row is an input vector for a word
output weight W′W'd×∥V∥d\times\|V\|each column is an output/context vector for a word
h=xTW=Wk,:h=x^TW=W_{k,:}

It clicked for me here: Wk,:W_{k,:} is row kk, all columns. The colon means take all; W:,kW_{:,k} is column kk.

Multiplying one-hot by WW is equivalent to table lookup, extracting the center word's vector. Programs typically index directly without actually performing one-hot matrix multiplication.

"Two sets of vectors" refers to the roles of center word and context word, both dd-dimensional and both learned. For example, (eat, apples) uses the input vector of eat and the output vector of apples. In reverse, (apples, eat) uses the other two vectors.

For notational consistency later, I write individual word vectors as column vectors vt,vc′∈Rdv_t,v'_c\in\mathbb R^d; the row retrieved from WW becomes vtv_t after transposition.

What does the retrieved vector represent?

It is a set of features learned to predict context. You cannot directly name each dimension as "fruitiness" or "sweetness"; meanings are usually distributed across multiple dimensions.

It is not a probability, allows negative values, and does not require the sum to be 1. Training typically starts with random values and only forms useful structure after training.

7. Skip-gram Three Formulas, and the Vanishing Labels

First Calculate Probability for a Single Candidate Word

s(t,c)=vtTvc′s(t,c)=v_t^Tv'_c

This is a dot product: multiply corresponding dimensions and sum them. It is the matching score adopted by the model, not cosine, and not a probability.

Why can dot product represent matching? Because training adjusts vectors so that the relative score of true word pairs increases. At random initialization, this score does not yet have reliable semantic meaning.

Complete softmax is:

p(c∣t)=exp⁡(s(t,c))∑w∈Vexp⁡(s(t,w))p(c\mid t)=\frac{\exp(s(t,c))}{\sum_{w\in V}\exp(s(t,w))}

The numerator is the current candidate word, and the denominator is the entire vocabulary, not just words in the window.

Then Combine the Targets in the Window

p(c1,…,cC∣t)=∏r=1Cp(cr∣t)p(c_1,\ldots,c_C\mid t)=\prod_{r=1}^{C}p(c_r\mid t)

Here we assume that given the center word, each context target is conditionally independent. For example, I [eat] apples, this is p(I∣eat)p(apples∣eat)p(\text{I}\mid\text{eat})p(\text{apples}\mid\text{eat}).

Window radius is something we set to select training samples; the model does not predict the radius. This is also not the probability of "apples appears at least once in the window." Regular Skip-gram does not explicitly distinguish left and right positions.

Finally Convert to Loss

For a true word pair:

L(t,c)=−log⁡p(c∣t)L(t,c)=-\log p(c\mid t)

I asked at the time: "Didn't you say cross-entropy was used? Why don't I see yjy_j?"

Complete cross-entropy is:

L=−∑j=1∣V∣yjlog⁡p(wj∣t)L=-\sum_{j=1}^{|V|}y_j\log p(w_j\mid t)

The label for the true word is 1, and for other candidate words is 0. All terms multiplied by 0 vanish, leaving only the negative log probability of the true word. So ground truth is already embodied in the selected cc.

For example, if the predicted probability of the true word apples is 0.2, the loss is −ln⁡0.2≈1.61-\ln0.2\approx1.61; after increasing to 0.8, the loss is −ln⁡0.8≈0.22-\ln0.8\approx0.22.

For each center position and each actual context in the training text, sum the losses together, which gives the two layers of summation in the slide. The log converts products to sums, and the negative sign converts maximizing probability into minimizing loss.

I also corrected something I said: it is not "reduce the gradient," but rather use the gradient to update parameters and reduce the loss:

θ←θ−η∇θL\theta\leftarrow\theta-\eta\nabla_\theta L

8. Negative Sampling: Not Shrinking the Softmax Denominator

This is where I most easily make mistakes. Initially I understood it as "only sample a few words, so softmax doesn't need to sum from 1 to the entire vocabulary." It is true that computation decreases, but the objective function also changes.

Complete softmax predicts context words across the entire vocabulary; negative sampling distinguishes between real data word pairs and random noise word pairs.

For example, a positive sample (eat, apples), paired with two negative samples (eat, computer), (eat, airplane). The positive sample has label 1, and negative samples have label 0.

Complete SoftmaxNegative Sampling
Which words to calculateEntire vocabularyOne positive and kk negatives
Output transformationsoftmaxIndependent sigmoid per pair
lossMulti-class cross-entropySum of multiple binary cross-entropies
Do probabilities sum to 1 across vocabulary?YesNot required

Put together, for a positive sample (t,c)(t,c):

Lsoftmax=−log⁡es(t,c)∑w∈Ves(t,w)L_{\text{softmax}}=-\log\frac{e^{s(t,c)}}{\sum_{w\in V}e^{s(t,w)}} LNS=−log⁡σ(s(t,c))−∑i=1klog⁡[1−σ(s(t,ni))]L_{\text{NS}}=-\log\sigma(s(t,c)) -\sum_{i=1}^{k}\log\left[1-\sigma(s(t,n_i))\right]

Because 1−σ(s)=σ(−s)1-\sigma(s)=\sigma(-s), the second term can also be written as −∑ilog⁡σ(−s(t,ni))-\sum_i\log\sigma(-s(t,n_i)).

Positive samples want sigmoid output close to 1; negative samples want it close to 0. Negative samples do not mean "these two words can never co-occur"; they are just training instances from noise sampling this time.

If you only use positive samples for this binary classification, the model can judge all word pairs as true. Negative samples give it grounds to distinguish. Complete softmax, on the other hand, already makes candidates compete through the denominator, without needing these extra negative samples.

k Negative Samples per Positive Sample: What Exactly Are We Counting?

In the slide, apricot has four context words, so there are 4 positive samples. The figure shows 8 negative samples, corresponding to 2 per positive sample.

The exercise changes kk to 3, so the number of negative samples is 4×3=124\times3=12, for a total of 16 positive and negative combined. We count training word pair instances, not different center words, and do not require the sampled noise words to be distinct.

9. Negative Sampling's P and Alpha: Which Words to Sample?

Pα(w)=count⁡(w)α∑u∈Vcount⁡(u)αP_\alpha(w)=\frac{\operatorname{count}(w)^\alpha} {\sum_{u\in V}\operatorname{count}(u)^\alpha}

Here PP only describes when sampling negative samples, what probability each word has of being selected, not the model's p(w∣t)p(w\mid t), nor the classification output of sigmoid.

The denominator sums the "count raised to a power" for the entire vocabulary, normalized into a probability distribution. This distribution can be pre-calculated from the corpus without needing to recalculate for each center word.

Taking α=3/4\alpha=3/4 makes the distribution smoother. For example, if the original frequency ratio is 100:1100:1, the sampling probability ratio becomes:

1003/4:1≈31.6:1100^{3/4}:1\approx31.6:1

I understand this smoothing as follows: the gap between high and low frequency narrows, low-frequency words get higher relative sampling probability than before, but high-frequency words are still more likely to be sampled.

It is not add-1. When count is 0, raising to a power still gives 0, and does not assign new probability to unseen words.

10. After Learning Word Vectors Well, How Do We Evaluate?

Intrinsic evaluation directly tests representations, such as calculating cosine for word pairs and comparing with human similarity ratings for ranking or correlation; you can also do synonym selection questions.

WordSim-353, SimLex-999, and SCWS in the slide are relevant evaluation datasets. The TOEFL example asks which word is closest to levied, with the correct answer being imposed; the model can select based on vector similarity and calculate accuracy.

Extrinsic evaluation puts vectors into downstream models to see actual task performance, such as machine translation, sentiment classification, or named entity recognition.

POS is Part of Speech. POS tagging assigns labels like NOUN, VERB to each word and can be a downstream task. In a book, book is a noun; in book a flight, book is a verb; context must be considered.

Good intrinsic evaluation does not guarantee good performance on every downstream task; final task performance also depends on the model and training method.

11. N-gram: Given the First N−1 Words, Calculate the Conditional Probability of the Next Word

P(wi∣w1,…,wi−1)≈P(wi∣wi−n+1,…,wi−1)P(w_i\mid w_1,\ldots,w_{i-1}) \approx P(w_i\mid w_{i-n+1},\ldots,w_{i-1})

Unigram does not look at context, bigram looks at the previous 1 word, trigram looks at the previous 2 words. This is not just predicting the nn-th word of the entire sentence; the window slides backward.

For example, trigram can successively calculate P(apples∣I,eat)P(\text{apples}\mid\text{I,eat}), P(every∣eat,apples)P(\text{every}\mid\text{eat,apples}). Multiplying conditional probabilities at each position gives sequence probability, with boundary handling conventions needed at sentence start and end.

Traditional count-based model estimation:

P(beach∣nice)=C(nice beach)C(nice)P(\text{beach}\mid\text{nice})= \frac{C(\text{nice beach})}{C(\text{nice})}

Here we assume consistent counting scope, where nice's count corresponds to occurrences of contexts with subsequent tokens. The problem is: if a combination has not been seen, unsmoothed estimation gives 0, and the product of sequence probabilities becomes 0.

12. Add-1: Why Add 1 to the Numerator and Add the Entire Vocabulary Size to the Denominator?

P^(wi∣c)=C(c,wi)+1∑w∈V[C(c,w)+1]=C(c,wi)+1∑w∈VC(c,w)+∣V∣\hat P(w_i\mid c)= \frac{C(c,w_i)+1}{\sum_{w\in V}[C(c,w)+1]} = \frac{C(c,w_i)+1}{\sum_{w\in V}C(c,w)+|V|}

cc is the preceding context. Each candidate word gets +1, so total count increases by ∣V∣|V|, not just 1.

Suppose the vocabulary has only apples, rice, bread, with original counts after eat as 3, 2, 0. After smoothing they become 4, 3, 1, and total count changes from 5 to 8, so bread's probability changes from 0 to 1/81/8.

The total continuation count in the question is here the sum of occurrences of all follow-up words, not the number of distinct follow-up words. For example, 12 times eat apples and 8 times eat rice total 20.

Therefore if total count is 20, vocabulary size is 5, and target word count is 0:

P^(w∣c)=0+120+5=125\hat P(w\mid c)=\frac{0+1}{20+5}=\frac1{25}

Smoothing does not make all word probabilities larger. The sum is still 1; we allocate mass to unseen combinations while adjusting probabilities of seen combinations. The "unseen" here refers to combinations in the fixed vocabulary that haven't been seen, not automatically handling all out-of-vocabulary words.

13. Why Does Neural Language Model Automatically Do Smoothing?

In traditional count-based models, dog jump and cat jump are counted separately. If dog jump appears frequently, the model does not automatically increase cat jump's count just because dog and cat have similar meanings.

In neural language models, dog and cat have different but potentially similar vectors, processed through the same network. If the model learns a pattern from dog jump, it has a chance to transfer this pattern to cat jump, even if this combination never appeared in training data.

So the key is similar representations and parameter sharing in the network. This does not mean dog and cat share a single vector, nor does using embedding guarantee superiority over all n-grams.

This automatic smoothing here refers to this kind of generalization of statistical information, not secretly doing add-1. Embeddings can be pre-trained or learned together with the language model.

Also, n-gram describes context restriction, and neural models can also look at only the previous n−1n-1 words. When comparing, we should say "count-based models" and "neural network-based models."

14. That Feed-Forward Network Diagram: Where Is the Fixed Window, and What Are W1/W2/W3?

The slide input is wt−n+1,…,wt−1w_{t-n+1},\ldots,w_{t-1}, each word looks up a vector from matrix CC, concatenated into xx. If word vectors have dd dimensions, input is fixed at (n−1)d(n-1)d dimensions. The window moves with position, but size remains fixed. Feed-forward is reflected in the computation flowing from input to hidden layer, then to output, with no recurrent hidden state passed across time steps.

h=tanh⁡(W(1)x+b(1))h=\tanh(W^{(1)}x+b^{(1)}) s=W(2)h+W(3)x+b(3)s=W^{(2)}h+W^{(3)}x+b^{(3)} y^=softmax⁡(s)\hat y=\operatorname{softmax}(s)
ParameterRole
CCOriginal word embedding lookup table
W(1)W^{(1)}Input to hidden layer, combining contextual features
W(2)W^{(2)}Hidden layer to output, scoring the entire vocabulary
W(3)W^{(3)}Direct input to output, providing a parallel linear scoring path

W(3)W^{(3)} is not a third layer stacked after W(2)W^{(2)}. It bypasses the hidden layer; its scores are added to those from the other path, and both are softmax'd together.

I even specifically asked "has such a network actually existed in history?" Yes, the slides correspond to Bengio et al.'s A Neural Probabilistic Language Model (2003). It jointly trains word representations and language models, so word embeddings didn't originate with Word2Vec.

15. Contextual embedding: Not just one extra context matrix

When I saw this page, my first thought was: is there an additional context matrix alongside the original embedding matrix?

The actual difference is that the final representation is computed dynamically based on the current input.

In static lookup, the representation of "bank" is fixed. A contextualized model first retrieves the initial token representation, then processes the visible context with a network to obtain the output vector at this position:

hi=fθ(input sequence)ih_i=f_\theta(\text{input sequence})_i

In "deposited money at the bank" and "beside the river bank," the initial token embedding for "bank" can be the same, yet the final representations will differ due to context.

So "each word still has only one vector" needs layered understanding: at the lookup stage, usually each token has a fixed row; at the network output stage, each occurrence can have its own representation. It's not that every possible meaning is pre-stored separately.

The slides list ELMo using BiLSTM, BERT using bidirectional Transformer encoder, and GPT using causal Transformer. Causal means only information from the current position and earlier can be used; future unseen words cannot modify the current representation.

Also don't confuse this with Word2Vec's output/context vector: those are static parameters too. "A vector as context words" and "a vector that changes with context" mean different things.

16. What category does GloVe belong to?

I ultimately mistook GloVe for a Skip-gram example. After organizing it's like this:

MethodLearning Basis
PPMIDirectly convert co-occurrence statistics to association weights
Word2Vec Skip-gramCenter word predicts context
Word2Vec CBOWContext predicts center word
GloVeFit global word co-occurrence statistics with weighted least squares objective

An intuitive GloVe example: ice and steam. "Solid" leans toward ice's context, "gas" toward steam's, while "water" relates to both. The ratio of co-occurrence probabilities helps capture this difference.

It's a different method from Word2Vec, but both fall under distributional semantics: learning meaning from the contextual distribution in language.

17. Key sentences I most need to remember this time

  • Distributional meaning looks at context distribution, not just total word frequency.
  • One dimension of co-occurrence vectors corresponds to context words; TF-IDF's count and df respectively count word occurrences and number of documents containing it.
  • ∣V∣|V| is vocabulary size; Wk,:W_{k,:} is the kk-th row.
  • Word2Vec's two sets of vectors are learned, not one-hot plus embedding.
  • Dot product is a score; only after softmax do we get a probability distribution across the vocabulary.
  • One-hot cross-entropy simplifies to only the negative log probability of the true target.
  • Negative sampling switches to a binary classification objective, not shrinking the softmax denominator; PαP_\alpha only controls how noise words are sampled.
  • Add-1 adds one to each candidate word, thus adding ∣V∣|V| to the denominator.
  • Neural language models help unseen combinations generalize through similar representations and parameter sharing, but don't guarantee better results in all cases.
  • Contextual embedding computes representations by context, not by adding another static table.

After reviewing to this point, I can better connect "lookup → score → probability → loss → update parameters." When seeing another formula next time, I'll first ask: is it doing representation, scoring, prediction, or sampling? Separating these things makes symbols less confusing.

Materials and corresponding slides

  • Distributional semantics, sparse representations, and SVD: PDF pages 3–20.
  • Word2Vec, two sets of vectors, negative sampling: pages 21–32.
  • Word embedding evaluation: pages 42–43.
  • N-gram, Add-1, neural language models: pages 45–53.
  • Contextual embeddings: page 63; GloVe: pages 67–72.
  • Historical sources: Bengio et al., A Neural Probabilistic Language Model, JMLR 2003.
ZB

Zhanbo Chen

Java Backend & AI Agent Developer

Back to home
Comments