4. Text embeddings

Thomas W. Jones

2019-04-17

Text embeddings

Text embeddings are particularly hot right now. While textmineR doesn’t (yet) explicitly implement any embedding models like GloVe or word2vec, you can still get embeddings. Text embedding algorithms aren’t conceptually different from topic models. They are, however, operating on a different matrix. Instead of reducing the dimensions of a document term matrix, text embeddings are obtained by reducing the dimensions of a term co-occurrence matrix. In principle, one can use LDA or LSA in the same way. In this case, rows of theta are embedded words. A phi_prime may be obtained to project documents or new text into the embedding space.

Create a term co-occurrence matrix

The first step in fitting a text embedding model is to create a term co-occurrence matrix or TCM. In a TCM, both columns and rows index tokens. The \((i,j)\) entries of the matrix are a count of the number of times word \(i\) co-occurs with \(j\). However, there are several ways to count co-occurrence. textmineR gives you three.

The most useful way of counting co-occurrence for text embeddings is called the skip-gram model. Under the skip-gram model, the count would be the number of times word \(j\) appears within a certain window of \(i\). A skip-gram window of two, for example, would count the number of times word \(j\) occurred in the two words immediately before word \(i\) or the two words immediately after word \(i\). This helps capture the local context of words. In fact, you can think of a text embedding as being a topic model based on the local context of words. Whereas a traditional topic model is modeling words in their global context.

To read more about the skip-gram model, which was popularized in the embedding model word2vec, look here.

The other types of co-occurrence matrix textmineR provides are both global. One is a count of the number of documents in which words \(i\) and \(j\) co-occur. The other is the number of terms that co-occur between documents \(i\) and \(j\). See help(CreateTcm) for info on these.

Fitting a model

Once we have a TCM, we can use the same procedure to make an embedding model as we used to make a topic model. Note that it may take considerably longer (because of dimensionality of the matrix) or shorter (because of sparsity) to fit an embedding on the same corpus.

Interpretation of \(\Phi\) and \(\Theta\)

In the language of text embeddings, \(\Theta\) gives us our tokens embedded in a probability space (because we used LDA, Euclidean space if we used LSA). \(\Phi\) defines the dimensions of our embedding space. The rows of \(\Phi\) can still be interpreted as topics. But they are topics of local contexts, rather than within whole documents.

Evaluating the model

As it happens, the same evaluation metrics developed for topic modeling also apply here. There are subtle differences in interpretation because we are using a TCM not a DTM. i.e. occurrences relate words to each other, not to documents.

We will create a summary table as we did with a topic model before.

Here it is ordered by prevalence. (Here, we might say density of tokens along each embedding dimension.)

Summary of top 10 embedding dimensions
topic coherence prevalence top_terms
t_22 t_22 0.218 184.11 aim, specific, study, determine, studies
t_41 t_41 0.178 158.86 research, health, cancer, clinical, development
t_21 t_21 0.209 146.48 core, research, program, support, training
t_45 t_45 0.181 123.89 cells, cell, human, function, signaling
t_17 t_17 0.135 118.94 based, treatment, design, effect, development
t_14 t_14 0.136 113.22 factors, risk, early, social, sud
t_16 t_16 0.149 110.60 role, mechanisms, gene, critical, genetic
t_43 t_43 0.195 110.52 brain, cancer, tumor, cells, patients
t_49 t_49 0.144 109.38 response, tissue, responses, immune, host
t_19 t_19 0.062 107.02 high, imaging, metastases, vivo, modified

And here is the table ordered by coherence.

Summary of 10 most coherent embedding dimensions
topic coherence prevalence top_terms
t_31 t_31 0.322 95.70 provided, applicant, project, patients, description
t_46 t_46 0.283 101.39 proteins, fly, cutaneous, infection, response
t_34 t_34 0.278 98.03 microbiome, gut, composition, crc, bas
t_25 t_25 0.238 97.01 carbon, pathways, environment, intracellular, metabolism
t_5 t_5 0.221 101.74 fertility, ethnic, race, knowledge, force
t_22 t_22 0.218 184.11 aim, specific, study, determine, studies
t_38 t_38 0.210 99.11 sleep, dependent, memory, cognitive, reduced
t_21 t_21 0.209 146.48 core, research, program, support, training
t_15 t_15 0.203 100.98 lung, expression, ipf, patients, key
t_50 t_50 0.200 104.31 ri, ptc, fc, pathways, brafv

Embedding documents under the model

You can embed whole documents under your model. Doing so, effectively makes your embeddings a topic model that have topics of local contexts, instead of global ones. Why might you want to do this? The short answer is that you may have reason to believe that an embedding model may give you better topics, especially if you are trying to pick up on more subtle topics. In a later example, we’ll be doing that to build a document summarizer.

A note on the below: TCMs may be very sparse and cause us to run into computational underflow issues when using the “gibbs” prediction method. As a result, I’m choosing to use the “dot” method.

Once you’ve embedded your documents, you effectively have a new \(\Theta\). We can use that to evaluate how well the embedding topics fit the documents as a whole by re-calculatingR-squared and coherence.

Where to next?

Embedding research is only just beginning. I would encourage you to play with them and develop your own methods.