The Word2Vec is an iteration based Natural Language Proceessing (NLP) framework for learning word vectors, developed by Thomas Mikolov. For each word position $t$ in a large corpus of text $T$ we define a fixed window size $m$ and the likelihood function $L(\theta)$:

We define the objective function $J(\theta)$ as the average negative log-likelihood:

Say a word $w_1$ is more likely to appear together with $w_2$ than $w_3$, we want to define a probability measure $p$ such that:

We achieve this by creating two word vectors $\textbf{v}_w$ (center) and $\textbf{u}_w$ (outside) for each word $w$ and using the dot product to represent the similarity of two words. The we use the softmax function to transform the dot products into a probability measure. Note that a softmax function amplifies probabilities for the largest values while still assign some probabilities to smaller values.

We can now derive the gradient of the objective function. First look at $\partial J(\theta)/\partial \textbf{v}_c$:

Looking at the derivative inside the double summation and use the fact that:

Therefore,

Computing the other derivative w.r.t. $\textbf{u}_o$

We used two vector for each word such that the optimization is easier. Eventually we average the two vectors for a given word. We can now use gradient descent to update the model parameters $\theta$.

Since $\nabla_\theta J(\theta)$ can be expensive to calculate for the entire corpus of text, we use stochastic gradient descent which updates $\theta$ with randomly selected samples of text.

# Example

requests Requests is a library for making HTTP requests in Python. HTTP functions as a request–response protocol in the client–server computing model. A web browser, for example, may be the client and an application running on a computer hosting a website may be the server.

• The client submits an HTTP request message to the server.
• The server, which provides resources such as HTML files and other content, or performs other functions on behalf of the client, returns a response message to the client. The response contains completion status information about the request and may also contain requested content in its message body.

A Uniform Resource Locator (URL) is a reference to a web resource, typically includes a protocol (http), a hostname (www.website.com), and a file name (index.html).

bs4 Beautiful Soup is a Python library for pulling data out of HTML and XML files, and it can work with a parser to provide functionality of navigating, searching, and modifying the parse tree. Both HTML and XML are markup languages designed for storing and transporting data.

re Provides regular expression matching operations.

nltk The Natural Language Toolkit is a Python platform for working with human language data. It provides interfaces to lexical resources as well as a suite of text processing libraries.

gensim Gensim is an open-source library for unsupervised topic modeling and natural language processing, using modern statistical machine learning.

Perform data cleaning and retain only key words from the parsed data.

Train Word2Vec model.

View the word vector for the word ‘python’:

View words that are most similar to ‘python’:

Some other resources: stackabuse, skymind and google code

Reference: