Language models, retrieval, prompting, and language adaptation · Count-based and distributed representations · LNG007 · Model
Paragraph Vector / Doc2Vec
Learns a vector per document or paragraph by predicting words, providing fixed-width representations for similarity, clustering, or downstream prediction.
In CodeSplash
Documented in the Academy only. There's no Set up button.
- Availability
- Explained here
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's own run
- CodeSplash's run
- 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 a vector per document or paragraph by predicting words, providing fixed-width representations for similarity, clustering, or downstream prediction. Distributed-memory and distributed-bag-of-words variants are grouped into this single documented family.
Question / objective
Can a document identifier learn a vector that summarizes predictive context within a document?
Architecture / computational structure
Paragraph vectors combine document embeddings with a word prediction mechanism, or predict document words directly in the distributed-bag-of-words variant. Learned quantities include document and word vectors; dimension and context settings are hyperparameters.
Assumptions
New-document embedding requires optimization or a defined inference rule; it is not a simple deterministic lookup.
Formula / computational definition
Distributed memory predicts p(w_t|d,w_context), combining document vector d with context embeddings. Distributed bag of words predicts sampled document words from d alone. Training maximizes observed-word log probabilities or sampled approximations.
Parameters and notation
Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Vector width, variant, context window, negative samples, document tags, training epochs, and inference iterations for new documents.
Fitting, fine-tuning, and adaptation
Fit training-document vectors and model weights jointly. Infer a new document vector while freezing trained word/output weights, using iterative optimization. A downstream head can then be fitted on document vectors. Validate unseen documents, not only embeddings of training documents.
How to use and calibrate
Use held-out document retrieval and classification, repeat inference runs, and compare with averaged word vectors and modern sentence encoders. 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
Represent short reports for clustering or nearest-document search without a large contextual encoder.
Interpretation / pitfalls
Watch out
Short documents, unstable inference, and document leakage can undermine evaluations. 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: Gensim Doc2Vec supplies training and inference in Python; R can use reticulate with the same saved model. Native R reproduction requires implementing the variant-specific objective and new-document optimization.
Example scope: A distributed-memory paragraph-vector predictor component using concatenation.
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.
Component demo, not a trained model — A distributed-memory paragraph-vector predictor component using concatenation.
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)
doc, word = [0.2, 0.5], [0.4, -0.1]
h = doc + word
p = softmax([dot(u, h) for u in [[1,0,1,0], [0,1,0,1]]])
print(-math.log(p[0]))
Component demo, not a trained model — A distributed-memory paragraph-vector predictor component using concatenation.
Shared setup for this chapter
softmax <- function(x) {
shifted <- exp(x-max(x))
shifted/sum(shifted)
}
doc <- c(0.2,0.5); word <- c(0.4,-0.1)
h <- c(doc, word)
p <- softmax(drop(rbind(c(1,0,1,0), c(0,1,0,1)) %*% h))
print(-log(p[1]))
Sources
- Source 1arxiv.org
From the CodeSplash reference, entry LNG007.