Semantic embeddings

Word Embeddings Lab

See how words become vectors, compute cosine similarity, try the analogy "king − man + woman", and plot words in a two-dimensional space.

What is this module?

A Word Embedding represents a word as a dense vector of real numbers, so that the vectors of words appearing in similar contexts lie close together (the distributional hypothesis of Harris and Firth). Word2Vec (Mikolov et al., 2013) learns vectors by predicting neighboring words (CBOW and Skip-gram), GloVe learns from co-occurrence statistics, and FastText adds subword pieces (n-grams), which helps morphologically rich Arabic. Contextual embeddings in BERT give a word a different vector depending on its sentence.

Tool: Semantic embeddings

Runs in your browser (JavaScript) — your text is not sent anywhere
Settings

How does the tool work?

  • The default vectors are educational and hand-designed with interpretable dimensions (royalty, gender, medical profession, place, capital/country, family...) so you can see how the operations work.
  • Cosine similarity = dot product ÷ (length of the first vector × length of the second).
  • Analogy: the vector (A − B + C), then the words closest to it.
  • Plot: Principal Component Analysis (PCA) via Power Iteration into two dimensions.

How to read the results?

Cosine values range from −1 to 1: the closer to 1, the more similar the directions. With the educational vectors the analogy works because the dimensions were designed for it, which does not mean a trained model will always succeed with the same precision.

Algorithms and methods

We always distinguish between traditional algorithms, resource-based methods and trained models. The tools on this page are traditional or lexicon-based, while trained models are explained here and run only through an external engine enabled by the site admin.

Machine learningFor explanation and comparison Word2Vec Word2Vec (CBOW / Skip-gram) A method for learning dense word vectors with a shallow network that predicts a word from its context (CBOW) or the context from the word (Skip-gram). Machine learningFor explanation and comparison FastText FastText Subword Embeddings An extension of Word2Vec that represents a word as the sum of its character n-gram vectors, so it gives vectors for unseen words and suits morphologically rich languages such as Arabic. Machine learningFor explanation and comparison GloVe GloVe (Global Vectors) A method that learns word vectors from the global co-occurrence matrix by approximating the logarithm of the co-occurrence counts with the dot product of the vectors. Traditional algorithm Implemented in the lab Cosine similarity Cosine Similarity A measure of how similar the directions of two vectors are, equal to their dot product divided by the product of their lengths, ranging from −1 to 1. Traditional algorithm Implemented in the lab Principal component analysis Principal Component Analysis (PCA) A dimensionality-reduction method that projects data onto the directions with the largest variance, used to plot high-dimensional vectors in two dimensions. Deep learningFor explanation and comparison Contextual embeddings Contextual Embeddings (BERT-style) Representations that give a word a different vector depending on its sentence, produced by the Transformer layers of models such as BERT, AraBERT and CAMeLBERT.

Limitations and accuracy limits

The default vectors are not the result of training on a corpus. The admin can import truly trained vectors in the word2vec text format (.vec) from the admin panel; they then appear separately, labeled with their source.

This tool's results are educational: they show how the algorithm works step by step, and they are not presented as confirmed scientific results.

From the knowledge base

Reading the results

How to read embedding results

The cosine score is between −1 and 1. In the teaching vectors the dimensions are named and designed by hand, so the results are deliberately "ideal" to explain the idea. With imported vectors the results are noisier and reflect the corpus they were trained on.

Lab documentation

Concept

Word2Vec

Proposed by Mikolov and colleagues in 2013 with two models: CBOW predicts the word from its context, and Skip-gram predicts the context from the word. It produces dense vectors that reflect the contextual distribution of words.

Mikolov et al. (2013)

Frequently asked questions

What are embeddings?

Representing a word as a vector of numbers so that the vectors of words similar in meaning or use end up close. They are learned from corpora with algorithms such as Word2Vec, GloVe and FastText.

Are the lab's vectors real?

The default vectors are teaching vectors designed by hand with ten interpretable dimensions (such as royalty, masculine/feminine, medicine, place) to illustrate the idea. They are not trained, and this is stated next to every result. An administrator can import trained vectors in ‎.vec‎ format.

How does cosine similarity work?

It computes the cosine of the angle between two vectors: the dot product divided by the product of their lengths. It ranges from -1 to 1, and the closer it is to 1, the more similar the vectors are in direction.

What is the "king − man + woman ≈ queen" example?

A famous example showing that some semantic relations appear as consistent directions in embedding space. With trained vectors it does not always hold exactly, and later research showed that its results depend on how the nearest neighbour is searched.

What is the difference between Word2Vec and FastText?

Word2Vec gives a vector for each whole word, so it cannot represent a word it has not seen. FastText builds a word's vector from its character n-grams, so it represents new and derived words, which matters for morphologically rich Arabic.

What are contextual embeddings?

Representations that vary with the sentence, produced by models such as BERT. The word «عين» gets a different vector in «عين الماء» (a spring) and «عين الإنسان» (an eye). Static embeddings give it a single vector.

Why reduce dimensions with PCA?

Because vectors have tens or hundreds of dimensions and cannot be plotted. PCA projects them onto two or three dimensions that keep the most variance. Distances in the plot are approximate and can mislead.

How do I import trained vectors?

From the admin panel: "Import embeddings", with a text file in word2vec text format (‎.vec‎). Only a limited number of the most frequent words are read to avoid weighing down the site, and the file's source is shown next to the results.

What are the sources of trained Arabic vectors?

They include the official FastText vectors for Arabic from Facebook Research (CC BY-SA 3.0 licence) and AraVec. Check the licence and size before importing, since the full files are very large.

Why are the dimensions of the teaching vectors interpretable while trained ones are not?

Because trained vectors learn dimensions that improve the training objective, not dimensions that humans name. We designed the teaching vectors with named dimensions only to make them easier to understand.

What is a sentence vector?

Representing a whole sentence with a single vector. The simplest way is the average of its word vectors, and stronger ways are models such as Sentence-BERT. The lab uses the average for demonstration.

What is bias in embeddings?

Trained embeddings learn the biases present in texts, such as associating occupations with a particular gender. This matters ethically when they are used in real systems.

Exercises and challenges

Beginner Computing by hand

Cosine

Compute the cosine between [1, 1] and [1, 0].

Hint

cos = (a·b)/(‖a‖ ‖b‖).

Show solution

(1×1+1×0)/(√2 × 1) = 1/√2 ≈ 0.707.

Beginner Hands-on in the lab

Analogies

Run "king − man + woman". What is the first word? What is the similarity score?

Hint

The vectors are for teaching.

Show solution

«ملكة» ("queen") with a high score, because the dimensions were designed by hand for this relation.

Intermediate Hands-on in the lab

Interpretable dimensions

Open the table of teaching vectors. Which dimension separates «طبيب» from «طبيبة»?

Hint

Read the dimension names.

Show solution

Only the "masculine/feminine" dimension; the "medicine" dimension is almost identical.

Intermediate Conceptual question

FastText and Arabic

Why does FastText suit Arabic better than Word2Vec?

Hint

Character n-grams.

Show solution

Because it builds a word's vector from its character n-grams, so it represents derived forms and words unseen in training, which are many in Arabic because of morphology and prefixes.

Advanced Conceptual question

The PCA trap

Why might two words appear close in a two-dimensional PCA plot even though their cosine is low?

Hint

Projection loses information.

Show solution

Because projecting onto two dimensions discards the variance in the other dimensions, so two points that differ in the discarded dimensions may coincide.

Advanced Research challenge

Evaluating embeddings

How would you evaluate trained Arabic vectors? Name two kinds of evaluation.

Hint

Intrinsic and extrinsic.

Show solution

Intrinsic evaluation: word similarity sets or analogy sets translated or built for Arabic. Extrinsic evaluation: the performance of a downstream task such as classification or NER using the vectors.

Related sections of the site