Spark
Word2vec
Vector Mathematics
Machine Learning
NLP

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.

Practice ML system design

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:

  1. Continuous Bag of Words (CBOW): This approach predicts the target word from context words within a given window surrounding the target.
  2. 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

VV: Vocabulary Size • dd: Dimension of Word Vectors • XVXX_{VX}: Context matrix with size V×dV \times dYdVY_{dV}: Target matrix with size d×Vd \times V

Objective Function

For CBOW, given a context CC:

max,P(targetcontext)=_t=1TP(w_tw_tk,,w_t+k)\max , P(\text{target} \mid \text{context}) = \prod\_{t=1}^{T} P(w\_t \mid w\_{t-k}, \ldots, w\_{t+k})

For Skip-gram:

max,P(contexttarget)=_t=1T_kjk,j0P(w_t+jw_t)\max , P(\text{context} \mid \text{target}) = \prod\_{t=1}^{T} \prod\_{-k \leq j \leq k, j \neq 0} P(w\_{t+j} \mid w\_t)

Softmax Function

To solve these probabilities, we use the softmax function:

P(oc)=ev_ovcw=1Vev_wv_cP(o \mid c) = \frac{e^{\textbf{v}\_o^{\top} \cdot \textbf{v}*c}}{\sum*{w=1}^{V} e^{\textbf{v}\_w^{\top} \cdot \textbf{v}\_c}}

Where vo\textbf{v}_o and vc\textbf{v}_c 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:

logσ(vovc)+i=1kEw_iP_n(w)[logσ(v_w_iv_c)]\log \sigma(\textbf{v}*o^{\top} \textbf{v}c) + \sum{i=1}^{k} \mathbb{E}*{w\_i \sim P\_n(w)}[\log \sigma(-\textbf{v}\_{w\_i}^{\top} \textbf{v}\_c)]

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 vo\textbf{v}_o and vc\textbf{v}_c: • vovoηvoJ(θ)\textbf{v}_o \leftarrow \textbf{v}_o - \eta \cdot \nabla_{\textbf{v}_o} J(\theta)vcvcηvcJ(θ)\textbf{v}_c \leftarrow \textbf{v}_c - \eta \cdot \nabla_{\textbf{v}_c} J(\theta)

Where η\eta 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:

  1. Input Processing: Text data is tokenized into words.
  2. Word Pair Generation: For skip-gram, create pairs of target and context words.
  3. 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the 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.