Spark Word2vec vector mathematics
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Word2Vec is a group of related models that are used to produce word embeddings, or vector representations of words. Developed by Mikolov et al. at Google in 2013, Word2Vec has become a foundational piece in the field of natural language processing (NLP). Apache Spark's machine learning library is equipped with an implementation of Word2Vec, leveraging distributed computing capabilities for scalability and efficiency.
In this article, we'll explore the mathematics behind Spark's Word2Vec, technical explanations, and examples to illustrate its functionalities.
Fundamentals of Word2Vec
Word2Vec operates in two primary modes:
- Continuous Bag of Words (CBOW): This approach predicts the target word from context words within a given window surrounding the target.
- Skip-gram: In this method, the model tries to predict the context words based on the target word.
Both methods learn to map words into a continuous vector space where semantically similar words are mapped to nearby points.
Vector Mathematics in Word2Vec
Notations and Definitions
• : Vocabulary Size • : Dimension of Word Vectors • : Context matrix with size • : Target matrix with size
Objective Function
For CBOW, given a context :
For Skip-gram:
Softmax Function
To solve these probabilities, we use the softmax function:
Where and are the vector representations.
Negative Sampling
Negative sampling refines the calculation by approximating the softmax function, focusing on observing observed word pairs and a small sample of random word pairs:
Optimizing the Objective
The optimization of the objective functions is performed through stochastic gradient descent (SGD). Gradients are computed and used to update vector representations:
• Modify vectors and : • •
Where represents the learning rate.
Technical Implementation in Spark
Spark's implementation of Word2Vec allows for scaling across large datasets by using its distributed processing features. Here is an outline:
- Input Processing: Text data is tokenized into words.
- Word Pair Generation: For skip-gram, create pairs of target and context words.
- Model Training: Using SGD and negative sampling to iterate and optimize word vectors.
Example Code
Below is an example of Word2Vec initialization and training in Spark:
Related reading
- Speech to text using TensorFlow
- Split a string by spaces -- preserving quoted substrings -- in Python
- Split a string into pieces of max length X - split only at spaces
- Split on regex more than a character, maybe variable width and keep the separator like GNU awk
- sparse autoencoder cost function in tensorflow
- Sparse Tensor matrix from a dense Tensor Tensorflow
- Spark write Dataset in kafka, enable KryoSerializer
- Spark/k8s How to run spark submit on Kubernetes with client mode

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.