FastText › Visualizing the Learned Embedding Space
Language AI HandbookAll chapters FastText › Visualizing the Learned Embedding SpaceOne section · Sections in this chapter8 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
A PCA projection of the trained embeddings shows whether morphological families ended up near each other in the full embedding space. PCA finds the directions of maximum variance in the embedding matrix and projects each vector onto the first two principal components, producing a 2D view that preserves as much structure as possible from the original 30-dimensional space.
from sklearn.decomposition import PCA
viz_words = [
"run",
"running",
"runner",
"runing",
"walk",
"walking",
"walker",
"walked",
"swim",
"swimming",
"swimmer",
"swiming",
"jump",
"jumping",
"jumper",
"jumped",
]
# The fourth form in each family is absent from the training vocabulary.
oov_in_viz = {w for w in viz_words if w not in in_vocab_set}
emb_list, valid_words = [], []
for w in viz_words:
e = model.get_embedding(w)
if norm(e) > 0:
emb_list.append(e)
valid_words.append(w)
emb_arr = np.array(emb_list)
coords_2d = PCA(n_components=2).fit_transform(emb_arr)
family_colors = {
"run": theme_color("#1f77b4"),
"running": theme_color("#1f77b4"),
"runner": theme_color("#1f77b4"),
"runing": theme_color("#1f77b4"),
"walk": theme_color("#2ca02c"),
"walking": theme_color("#2ca02c"),
"walker": theme_color("#2ca02c"),
"walked": theme_color("#2ca02c"),
"swim": theme_color("#d62728"),
"swimming": theme_color("#d62728"),
"swimmer": theme_color("#d62728"),
"swiming": theme_color("#d62728"),
"jump": theme_color("#9467bd"),
"jumping": theme_color("#9467bd"),
"jumper": theme_color("#9467bd"),
"jumped": theme_color("#9467bd"),
}
OOV words land within or near the correct family regions despite never having been seen during training. The PCA projection confirms that the n-gram sharing mechanism is doing meaningful work: structural similarity in character sequences translates into proximity in the learned embedding space. When the model has never seen "runing" as a complete token, it can still place it near "run," "running," and "runner" because most of its n-grams are familiar from training on related words.
PCA preserves some structure and loses some. The first two principal components capture the directions of maximum variance, which in this case align with the between-family separation axis and within-family variation. The actual embedding space is 30-dimensional, and some structure that exists there is invisible in the 2D projection. The clusters you see are real, but the distances between clusters in 2D are not perfectly representative of distances in 30 dimensions.