Sub topic 17
Evaluating Word Embeddings
Assume you know vectors, cosine similarity, basic probability, gradient descent, and text tokenization. You should also know how word2vec and GloVe produce embeddings. Evaluation asks whether those vectors capture useful linguistic relationships and whether they improve a real task.
Intrinsic evaluation: testing the vectors directly
An intrinsic evaluation measures an embedding property without training a complete application. Common tests include:
- Similarity judgments: Compare cosine similarity for word pairs with scores assigned by people. A useful embedding should rank pairs such as
carandtruckabove unrelated pairs such ascarandbanana. - Nearest neighbours: Inspect the words closest to a target vector. Neighbours can reveal whether the model captures topic, meaning, spelling, or undesirable artifacts.
- Analogy tests: Test relationships such as
king - man + woman ≈ queen. These tests are convenient, but their results depend heavily on vocabulary, wording, and cultural assumptions.
A high correlation with human similarity scores does not prove that an embedding will improve a classifier. It only measures one selected property.
import numpy as np
def cosine_similarity(a, b):
denominator = np.linalg.norm(a) * np.linalg.norm(b)
if denominator == 0:
return 0.0
return float(np.dot(a, b) / denominator)
vectors = {
"car": np.array([0.9, 0.1, 0.0]),
"truck": np.array([0.8, 0.2, 0.0]),
"banana": np.array([0.0, 0.1, 0.9]),
}
print(cosine_similarity(vectors["car"], vectors["truck"]))Extrinsic evaluation: testing a downstream task
An extrinsic evaluation puts embeddings into a task such as sentiment classification, named-entity recognition, search, or recommendation. Compare at least two systems:
- A baseline without pretrained embeddings, such as randomly initialized vectors.
- A system using the embeddings being evaluated.
Keep the model architecture, data split, preprocessing, and training budget the same. Report a metric that matches the task. Accuracy may work for balanced classification, while macro-F1, mean reciprocal rank, or perplexity may be better for other settings.
| Evaluation type | Measures | Main risk |
|---|---|---|
| Intrinsic | A narrow vector property | Weak connection to the application |
| Extrinsic | Effect on a real task | Results depend on the task and model |
| Human review | Quality and harmful associations | Expensive and subjective |
A reliable evaluation workflow
Prevent data leakage, which occurs when evaluation information influences training. For example, do not tune vectors on test examples or select a model after repeatedly inspecting final test scores. Use a validation set for decisions and reserve the test set for the final report.
Evaluate important subgroups separately. An embedding can achieve good average accuracy while performing poorly for names, dialects, or minority groups. Inspect nearest neighbours and analogy outputs for stereotypes, offensive associations, and unstable relationships.
Exercise
Why should an embedding be tested both intrinsically and extrinsically?
Show answer
Intrinsic tests measure properties of the vectors directly, while extrinsic tests show whether those properties improve the intended application.