Skip to content

Language models, retrieval, prompting, and language adaptation · Count-based and distributed representations · LNG005 · Model

GloVe

Learns static word vectors from global co-occurrence counts. It makes the relationship between distributional statistics and embedding geometry explicit, and is useful when reusable word-level features or interpretable…

Runs in CodeSplash Component demo, not a trained model CodeSplash's own run

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
Verified in Python and R through discover.word_vectors. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Learns static word vectors from global co-occurrence counts. It makes the relationship between distributional statistics and embedding geometry explicit, and is useful when reusable word-level features or interpretable count diagnostics are desired.

Question / objective

Can global word co-occurrence ratios be represented by low-dimensional vector differences?

Architecture / computational structure

A sparse co-occurrence matrix supervises two embedding tables and bias vectors. Learned vectors and biases minimize weighted squared error; context window, dimension, weighting exponent, and count cutoff are hyperparameters.

Assumptions

Co-occurrence processing choices materially affect results.

Formula / computational definition

L=∑i,jf(Xij)(wi⊤w~j+bi+b~j−log⁡Xij)2.\mathcal L=\sum_{i,j}f(X_{ij})\left(w_i^\top\widetilde w_j+b_i+\widetilde b_j-\log X_{ij}\right)^2.

Minimize sum_{i,j} f(X_ij)[v_i^T u_j+b_i+c_j-log X_ij]^2 over observed co-occurrences X_ij. The weighting function f limits the influence of extreme counts; v,u are embedding vectors and b,c biases.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Context-window definition, vocabulary thresholds, vector dimension, weighting cutoff, weighting exponent, and optimization passes.

Fitting, fine-tuning, and adaptation

Construct co-occurrences from training text and optimize the published weighted objective. Adapt by rebuilding or augmenting counts and refitting, or fine-tune exported vectors in a supervised downstream model. Track vocabulary coverage and semantic drift on held-out domain tasks.

How to use and calibrate

Use downstream performance, reconstruction diagnostics, frequency-stratified neighbors, and sensitivity to window choice; two-dimensional projections are illustrations only. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.

Example application

Build word embeddings when a complete corpus-wide co-occurrence matrix is available.

Interpretation / pitfalls

Watch out

A single representation mixes meanings, and associations can reflect corpus bias rather than semantic truth. Zeros require special treatment rather than taking log zero. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.

Python and R code examples

Full implementation route: The original implementation is a reproducible external route. Both languages can implement the weighted least-squares objective for a small co-occurrence matrix; Python/R wrappers should be version-checked rather than assumed interchangeable.

Example scope: One weighted GloVe co-occurrence residual, with explicit weighting parameters.

Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.

Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — One weighted GloVe co-occurrence residual, with explicit weighting parameters.

Shared setup for this chapter
import math

def softmax(xs):
    m = max(xs)
    es = [math.exp(x-m) for x in xs]
    return [e/sum(es) for e in es]

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

def matvec(matrix, vector):
    return [dot(row, vector) for row in matrix]

def meanvec(rows):
    return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]

def sigmoid(x):
    return 1/(1+math.exp(-x))

def nll(probabilities):
    return -sum(math.log(p) for p in probabilities)
x, xmax, exponent = 8.0, 100.0, 0.75
prediction = dot([0.3, 0.2], [0.4, -0.1]) + 0.1 + 0.2
weight = min((x / xmax) ** exponent, 1.0)
print(weight * (prediction - math.log(x)) ** 2)
R

Component demo, not a trained model — One weighted GloVe co-occurrence residual, with explicit weighting parameters.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
x <- 8; xmax <- 100; exponent <- 0.75
prediction <- sum(c(0.3,0.2) * c(0.4,-0.1)) + 0.1 + 0.2
weight <- min((x / xmax)^exponent, 1)
print(weight * (prediction - log(x))^2)

Sources

From the CodeSplash reference, entry LNG005.