Notes on Word2Vec
Word2Vec is an iteration based
Natural Language Proceessing (NLP) framework for learning word vectors, developed by Thomas Mikolov. For each word position in a large corpus of text we define a fixed window size and the likelihood function :
We define the
objective function as the average negative log-likelihood:
Say a word is more likely to appear together with than , we want to define a probability measure such that:
We achieve this by creating two word vectors (center) and (outside) for each word 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 :
Looking at the derivative inside the double summation and use the fact that:
Computing the other derivative w.r.t.
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 .
Since can be expensive to calculate for the entire corpus of text, we use
stochastic gradient descent which updates with randomly selected samples of text.
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.
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.
data = raw_data.lower()
data = re.sub('[^a-zA-Z]', ' ', data)
data = re.sub(r'\s+', ' ', data)
words = [nltk.word_tokenize(sentence) for sentence in nltk.sent_tokenize(data)]
words = [[w for w in words[i] if w not in stopwords.words('english')] for i in range(len(words))]
Train Word2Vec model.
model = Word2Vec(words, min_count=2)
View the word vector for the word ‘python’:
array([ 0.00272543, 0.00044483, -0.002916 , -0.00091269, -0.0028646 ,
View words that are most similar to ‘python’:
- Deep Learning for Natural Language Processing, Standford University