# Word embeddings

After Tomas Mikolov et al. released the word2vec tool, there was a boom of articles about word vector representations. One of the best of these articles is Stanford’s GloVe: Global Vectors for Word Representation, which explained why such algorithms work and reformulated word2vec optimizations as a special kind of factoriazation for word co-occurence matrices.

Here I will briefly introduce the GloVe algorithm and show how to use its text2vec implementation.

# GloVe algorithm

THe GloVe algorithm consists of following steps:

1. Collect word co-occurence statistics in a form of word co-ocurrence matrix $$X$$. Each element $$X_{ij}$$ of such matrix represents how often word i appears in context of word j. Usually we scan our corpus in the following manner: for each term we look for context terms within some area defined by a window_size before the term and a window_size after the term. Also we give less weight for more distant words, usually using this formula: $decay = 1/offset$

2. Define soft constraints for each word pair: $w_i^Tw_j + b_i + b_j = log(X_{ij})$ Here $$w_i$$ - vector for the main word, $$w_j$$ - vector for the context word, $$b_i$$, $$b_j$$ are scalar biases for the main and context words.

3. Define a cost function $J = \sum_{i=1}^V \sum_{j=1}^V \; f(X_{ij}) ( w_i^T w_j + b_i + b_j - \log X_{ij})^2$ Here $$f$$ is a weighting function which help us to prevent learning only from extremely common word pairs. The GloVe authors choose the following function:

$f(X_{ij}) = \begin{cases} (\frac{X_{ij}}{x_{max}})^\alpha & \text{if } X_{ij} < XMAX \\ 1 & \text{otherwise} \end{cases}$

# Linguistic regularities

Now let’s examine how GloVe embeddings works. As commonly known, word2vec word vectors capture many linguistic regularities. To give the canonical example, if we take word vectors for the words “paris,” “france,” and “germany” and perform the following operation:

$vector("paris") - vector("france") + vector("germany")$

the resulting vector will be close to the vector for “berlin”

Let’s download the same Wikipedia data used as a demo by word2vec:

library(text2vec)
text8_file = "~/text8"
if (!file.exists(text8_file)) {
unzip ("~/text8.zip", files = "text8", exdir = "~/")
}
wiki = readLines(text8_file, n = 1, warn = FALSE)

In the next step we will create a vocabulary, a set of words for which we want to learn word vectors. Note, that all of text2vec’s functions which operate on raw text data (create_vocabulary, create_corpus, create_dtm, create_tcm) have a streaming API and you should iterate over tokens as the first argument for these functions.

# Create iterator over tokens
tokens = space_tokenizer(wiki)
# Create vocabulary. Terms will be unigrams (simple words).
it = itoken(tokens, progressbar = FALSE)
vocab = create_vocabulary(it)

These words should not be too uncommon. Fot example we cannot calculate a meaningful word vector for a word which we saw only once in the entire corpus. Here we will take only words which appear at least five times. text2vec provides additional options to filter vocabulary (see ?prune_vocabulary).

vocab = prune_vocabulary(vocab, term_count_min = 5L)

Now we have 71,290 terms in the vocabulary and are ready to construct term-co-occurence matrix (TCM).

# Use our filtered vocabulary
vectorizer = vocab_vectorizer(vocab)
# use window of 5 for context words
tcm = create_tcm(it, vectorizer, skip_grams_window = 5L)

Now we have a TCM matrix and can factorize it via the GloVe algorithm.
text2vec uses a parallel stochastic gradient descent algorithm. By default it will use all cores on your machine, but you can specify the number of cores if you wish.

Let’s fit our model. (It can take several minutes to fit!)

glove = GlobalVectors$new(rank = 50, x_max = 10) wv_main = glove$fit_transform(tcm, n_iter = 10, convergence_tol = 0.01, n_threads = 8)
## INFO  [09:39:07.657] epoch 1, loss 0.1755
## INFO  [09:39:15.795] epoch 2, loss 0.1228
## INFO  [09:39:24.507] epoch 3, loss 0.1085
## INFO  [09:39:33.514] epoch 4, loss 0.1005
## INFO  [09:39:42.651] epoch 5, loss 0.0954
## INFO  [09:39:51.221] epoch 6, loss 0.0918
## INFO  [09:40:00.094] epoch 7, loss 0.0890
## INFO  [09:40:08.177] epoch 8, loss 0.0869
## INFO  [09:40:16.672] epoch 9, loss 0.0851
## INFO  [09:40:24.678] epoch 10, loss 0.0837
dim(wv_main)
## [1] 71290    50

Note that model learns two sets of word vectors - main and context. Essentially they are the same since model is symmetric. From our experience learning two sets of word vectors leads to higher quality embeddings. GloVe model is “decomposition” model (inherits from mlapiDecomposition - generic class of models which decompose input matrix into two low-rank matrices). So on par with any other mlapiDecomposition model second low-rank matrix (context word vectors) is available in components field:

wv_context = glove\$components
dim(wv_context)
## [1]    50 71290

Note that as in all models which inherit from mlapiDecomposition transformed matrix will has nrow = nrow(input), ncol = rank and second component matrix will has nrow = rank, ncol = ncol(input).

While both of word-vectors matrices can be used as result it usually better (idea from GloVe paper) to average or take a sum of main and context vector:

word_vectors = wv_main + t(wv_context)

We can find the closest word vectors for our paris - france + germany example:

berlin = word_vectors["paris", , drop = FALSE] -
word_vectors["france", , drop = FALSE] +
word_vectors["germany", , drop = FALSE]
cos_sim = sim2(x = word_vectors, y = berlin, method = "cosine", norm = "l2")
head(sort(cos_sim[,1], decreasing = TRUE), 5)
##      paris     berlin     munich    germany versailles
##  0.7724678  0.7156243  0.6962157  0.6381500  0.6170311

You can achieve much better results by experimenting with skip_grams_window and the parameters of the GloVe class (including word vectors size and the number of iterations). For more details and large-scale experiments on wikipedia data see this old post on my blog.