word2vec
TensorFlow
machine learning
neural networks
natural language processing

Tensorflow implementation of word2vec

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Word2Vec learns dense vector representations where words that appear in similar contexts end up close together in embedding space. In TensorFlow, the usual implementation path is to build skip-gram training pairs, learn an embedding table, and train with a sampled softmax or negative-sampling style objective.

Understand the Skip-Gram Setup

Word2Vec has two classic variants: CBOW and skip-gram. Skip-gram is easier to explain in code because each training example starts with one center word and tries to predict a nearby context word.

For a sentence such as the cat sat on the mat, a skip-gram dataset with a small window might contain pairs like:

  • center cat, context the
  • center cat, context sat
  • center sat, context cat

The model does not predict a full sentence. It only learns embeddings that make nearby words score highly together.

Build Training Pairs

A small preprocessing function can turn token ids into skip-gram pairs:

python
1def make_skipgram_pairs(tokens, window_size=2):
2    pairs = []
3    for i, center in enumerate(tokens):
4        left = max(0, i - window_size)
5        right = min(len(tokens), i + window_size + 1)
6        for j in range(left, right):
7            if i != j:
8                pairs.append((center, tokens[j]))
9    return pairs
10
11
12tokens = [1, 2, 3, 4, 5]
13print(make_skipgram_pairs(tokens, window_size=1))

In real projects, those token ids come from a vocabulary mapping built from the corpus. Very rare words are often dropped or replaced with an unknown token to keep the vocabulary manageable.

Train Embeddings With TensorFlow

The core model can be quite small. An embedding layer stores the word vectors, and a dense output layer scores vocabulary items.

python
1import tensorflow as tf
2
3vocab_size = 1000
4embedding_dim = 64
5
6model = tf.keras.Sequential([
7    tf.keras.layers.Embedding(input_dim=vocab_size, output_dim=embedding_dim),
8    tf.keras.layers.Flatten(),
9    tf.keras.layers.Dense(vocab_size)
10])
11
12model.compile(
13    optimizer="adam",
14    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
15)

For a toy dataset, you can train directly on center-word ids and context-word ids:

python
1centers = tf.constant([1, 2, 3, 4], dtype=tf.int32)
2contexts = tf.constant([2, 3, 4, 5], dtype=tf.int32)
3
4model.fit(centers, contexts, epochs=5, verbose=0)

This dense softmax setup is simple and runnable, although large vocabularies usually switch to more efficient sampling-based objectives.

Extract and Use the Learned Vectors

After training, the embedding weights are the part you care about:

python
1embeddings = model.layers[0].get_weights()[0]
2print(embeddings.shape)  # (vocab_size, embedding_dim)
3
4word_id = 10
5print(embeddings[word_id][:5])

You can compare words with cosine similarity, use the vectors as initialization for downstream NLP models, or visualize them with dimensionality reduction.

The actual quality of the vectors depends far more on corpus quality, vocabulary handling, and training pair generation than on fancy model architecture.

Common Pitfalls

One common mistake is confusing the embedding matrix with the final dense layer. The embedding layer contains the learned word vectors you usually want to keep.

Another issue is training on a tiny corpus and expecting meaningful semantic structure. Word2Vec works because it sees many context co-occurrences; very small datasets produce noisy vectors.

It is also easy to build an extremely large full-softmax output layer and run into memory problems. For realistic vocabularies, sampled losses or specialized data pipelines are often necessary.

Summary

  • Word2Vec learns embeddings by predicting nearby words from local context.
  • In TensorFlow, a simple skip-gram implementation can be built with an embedding layer and a classifier over context words.
  • The training data is a set of center-context pairs generated from tokenized text.
  • The learned vectors live in the embedding matrix, not in the final output layer.
  • Corpus quality, vocabulary size, and sampling strategy matter more than model size for useful embeddings.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.