However, by just focusing on the current word, Markov models lose all context and structure of the preceding words in the sentence which can lead to incorrect predictions, limiting their applicability in many generative scenarios.
Neural networks are models that are inspired by the workings of a human brain, offering an alternate method for computing by modeling non-linear relationships between inputs and outputs — their use for language modeling is known as neural language modeling.
An RNN is a type of neural network that can exploit the sequential nature of the input. It passes each item of the sequence through a feedforward network and gives the output of the model as an input to the next item in the sequence, allowing for the storage of information from the previous steps. The “memory” possessed by RNNs makes them great for language generation, as they can remember the context of the conversation over time. RNNs differ from Markov chains, in that they also look at words previously seen (unlike Markov chains, which just look at the previous word) to make predictions.
The future of Language Generation
In this blog, we saw the evolution of language generation from using simple Markov chains for sentence generation to using self-attention models for generating longer range coherent text. However, we are just at the dawn of generative language modeling, and transformers are just one step in the direction towards truly autonomous text generation. Generative models are also being developed for other types of content such as images, videos, and audio. This opens the possibility to integrate these models with generative text models to develop advanced personal assistants with audio/visual interfaces.
However, we, as a society, need to be careful with the application of generative models as they open several possibilities for their exploitation in generating fake news, fake reviews and impersonating people online. OpenAI’s decision to
withhold release of their GPT-2 language model due to the potential for its misuse is a testament to the fact that we have now entered an age where language models are powerful enough to cause concern.
Generative models have the potential to transform our lives; however, they are a double-edged sword. By putting these models through appropriate levels of scrutiny, whether through the research community or government regulation, there is certainly going to be a lot more progress in this domain over the coming years. Regardless of the outcome, there should be exciting times ahead!
A Light Introduction to Transfer Learning for NLP
In this post, I will introduce transfer learning for natural language processing and key questions necessary to better understand this important area of study.
ElvisFollow
Jul 27, 2018
Picture this: a machine that can model and comprehend natural language and which is able to communicate in any language. Is this possible? Unfortunately, not yet. Because of the complexity of language, this only has credibility in the world of science fiction.
But recent progress in machine learning (ML) and natural language processing (NLP) indicates that we are moving closer to teaching machines remarkable capabilities as it regards to natural language. I admit that the idea of a machine possessing human-level capabilities is pure hype given how complex language is to understand. Nevertheless,
recent progress in NLP shows impressive results to be excited about.
In this post, I will briefly introduce some of the recent ideas that could enable generalizable NLP systems, specifically those that employ transfer learning through pretrained language models. By generalizable, I mean NLP systems that are able to perform many tasks at once. For instance, just imagine a scenario where an ML algorithm is able to recognize both sentiment and sarcasm from textual information. Why not? What kind of machine learning technology is needed to achieve this? Enter the world of transfer learning.
Transfer learning
In simple terms, transfer learning is the process of training a model on a large-scale dataset and then using that pretrained model to conduct learning for another downstream task (i.e., target task). Transfer learning was popularized in the field of computer vision thanks to the
ImageNet dataset. Here I won’t discuss computer vision. Instead, I will focus on how these concepts are applied to the field of natural language processing.
Pretraining
These are exciting times for NLP and ML researchers and enthusiasts. Very recently, pretrained language models were used to achieve state-of-the-art results on a wide range of NLP tasks (e.g., sequence labeling and sentence classification). Some of the recent works that employ pretrained language models include
ULMFit,
ELMo,
GLoMo, and
OpenAI transformer. These language modeling systems conduct deep pretraining of entire models with hierarchical representations or graph-based representations. This concept is a shift from the use of unary word embeddings, which have been effectively used to address many NLP tasks over the past few years, to the use of more sophisticated and abstract representations.
You may be wondering, “How does pretraining work for NLP?” Why is pretraining useful? Simply put it, it just so happens that pretraining allows a model to capture and learn a varity of linguistic phenomena, such as long-term dependencies and negation, from a large-scale corpus. Then this knowledge is used (transferred) to initialize and then train another model to perform well on a specific NLP task, such as
sentiment classification.
This works in language because it happens, for example, that negation is an important attribute to detect sentiment polarity from textual information. In addition, negation could also be useful for, say, something like
sarcasm detection, which is one of the most complex, unsolved tasks in NLP. We can say that negation is useful for many NLP tasks and thus the pretrained model possesses some universal properties.
A language model that possesses universal properties could be useful in cases where there is a lack of annotated datasets or language resources which is prevalent in NLP research. This idea is exciting because we are not only aiming to build a universal model, but also address some of the difficult challenges in ML research: availability of data and language resources.
The models
So far we know that the knowledge obtained from pretrained language models, such as in the form of word embeddings, perform significantly well for many NLP tasks. The problem here is that this knowledge, in the form of latent features, is not broad or general enough to perform well on the target or downstream task/s at hand. There are many explanations for this — some of it we know and some we don’t — but for now, let us briefly look at one of the recent methods that aim to address these limitations.
One recent, popular
method coined as ELMo can be described as “pretraining the entire model with deep contextualized representations through stacked neural layers” as opposed to just employing the word embeddings (i.e., unary representations) as initialization. Don’t worry if it’s not clear what the above concept describes. My goal in this series is to clarify what these models are doing in a very intuitive manner, and answer several key questions that arise when considering them:
What do we mean by modeling deep contextualized representations in the context of language? What is the model really learning?
How to build and train these pretrained language models?
What are the key components of the pretrained language models and how to improve them?
How do we use or apply them to solve different language-based problems?
What are the benefits of pretrained language models as compared to conventional transfer learning techniques for NLP?
What are the drawbacks?
What aspects of natural language do we need to keep in mind when pretraining language models?
What kinds of pretraining tasks are we considering to build and test these so-called generalizable NLP systems?
And more importantly, what kinds of datasets should we use that are representative enough to address the wide range of NLP tasks?
I will stop with the questions here but you can already start to observe how complex this line of research is. It needs to be broken down into parts. All you need to understand for now is that in order to build a universal language modelwe need to consider many things, some of which are still not very clear today. In the next post of this series, I will begin to address each of these important questions while exploring the state-of-the-art research on transfer learning for NLP.
Beyond Word Embeddings Part 2
Aaron (Ari) BornsteinFollow
Oct 17, 2018
Word Vectors and NLP Modeling from BoW to BERT

TL;DR
Since the advent of
word2vec, neural word embeddings have become a go to method for encapsulating distributional semantics in text applications. This series will review the strengths and weaknesses of using pre-trained word embeddings and demonstrate how to incorporate more complex semantic representation schemes such as Semantic Role Labeling, Abstract Meaning Representation and Semantic Dependency Parsing into your applications.
Introduction
The
last post in this series reviewed some of the recent milestones in neural natural language processing. In this post we will review some of the advancements in text representation.
Computers are unable to understand the concepts of words. In order to process natural language a mechanism for representing text is required. The standard mechanism for text representation are word vectors where words or phrases from a given language vocabulary are mapped to
vectors of
real numbers.
Traditional Word Vectors
Before diving directly into Word2Vec it’s worth while to do a brief overview of some of the traditional methods that pre-date neural embeddings.
Bag of Words or BoW vector representations are the most common used traditional vector representation. Each word or n-gram is linked to a vector index and marked as 0 or 1 depending on whether it occurs in a given document.

An example of a one hot bag of words representation for documents with one word.
BoW representations are often used in methods of document classification where the frequency of each word, bi-word or tri-word is a useful feature for training classifiers. One challenge with bag of word representations is that they don’t encode any information with regards to the meaning of a given word.
In BoW word occurrences are evenly weighted independently of how frequently or what context they occur. However in most NLP tasks some words are more relevant than others.
TF-IDF, short for
term frequency–inverse document frequency, is a numerical statistic that is intended to reflect how important a word or n-gram is to a
document in a collection or
corpus. They provide some weighting to a given word based on the context it occurs.The tf–idf value increases
proportionally to the number of times a word appears in a document and is offset by the number of documents in the corpus that contain the word, which helps to adjust for the fact that some words appear more frequently than others.
https://skymind.ai/wiki/bagofwords-tf-idfHowever even though tf-idf BoW representations provide weights to different words they are unable to capture the word meaning.
As the famous linguist J. R. Firth said in 1935, “The complete meaning of a word is always contextual, and no study of meaning apart from context can be taken seriously.”
Distributional Embeddings enable word vectors to encapsulate contextual context. Each embedding vector is represented based on the mutual information it has with other words in a given corpus. Mutual information can be represented as a global co-occurrence frequency or restricted to a given window either sequentially or based on dependency edges.

An example distributional embedding matrix each row encodes distributional context based on the count of the words it co-occurs with
Distributional vectors predate neural methods for word embeddings and the techniques surrounding them are still relevant as they provide insight into better interpreting what neural embeddings learn. For more information one should read the work of
Goldberg and Levy.
Neural Embeddings
Word2VecPredictive models learn their vectors in order to improve their predictive ability of a loss such as the loss of predicting the vector for a target word from the vectors of the surrounding context words.
Word2Vec is a predictive embedding model. There are two main Word2Vec architectures that are used to produce a
distributed representation of words:
Continuous bag-of-words (CBOW) — The order of context words does not influence prediction (
bag-of-words assumption). In the continuous skip-gram architecture, the model uses the current word to predict the surrounding window of context words.
Continuous skip-gram weighs nearby context words more heavily than more distant context words. While order still is not captured each of the context vectors are weighed and compared independently vs CBOW which weighs against the average context.

CBOW and Skip-Gram Architectures
CBOW is faster while skip-gram is slower but does a better job for infrequent words.
GloVeBoth CBOW and Skip-Grams are “predictive” models, in that they only take local contexts into account. Word2Vec does not take advantage of global context.
GloVe embeddings by contrast leverage the same intuition behind the co-occuring matrix used distributional embeddings, but uses neural methods to decompose the co-occurrence matrix into more expressive and dense word vectors. While GloVe vectors are faster to train, neither GloVe or Word2Vec has been shown to provide definitively better results rather they should both be evaluated for a given dataset.
FastTextFastText, builds on Word2Vec by learning vector representations for each word and the n-grams found within each word. The values of the representations are then averaged into one vector at each training step. While this adds a lot of additional computation to training it enables word embeddings to encode sub-word information. FastText vectors have been shown to be more accurate than Word2Vec vectors by a number of different measures
A 10,000 foot overview of Neural NLP Architectures
In addition to better word vector representation the advent of neural has led to advances in machine learning architectures that have enabled the advances listed in the
previous post.
This section will highlight some of the key developments in neural architecture that enabled some of the NLP advances seen thus far. This not meant to be an exhaustive review of deep learning and machine learning NLP architecture, rather the goal is to demonstrate the changes that are driving NLP forward.
Deep Feed Forward Networks

The advent of linear deep feed forward networks also known as
multi layer perceptrons (MLP) in NLP introduced the potential for non linear modeling. This development helps with NLP because there are cases where the embedding space may be non linear. Take the following example of a documents whose embedding space is non linear meaning there is no way to linear divide the two document groups.

It doesn’t matter how you fit a line there is no linear way to split the spam and ham documents
A non linear MLP network provides the ability to properly model such non linearities.

This development by itself however did not bring about a significant revolution in NLP, since MLPs are unable to model word ordering. While MLPs open the door for marginal improvements in tasks such as language classification, where decisions can be made by modeling independent character frequencies, for more complex or ambiguous tasks standalone MLPs fall short.
1D CNNs

Kim, Y. (2014). Convolutional Neural Networks for Sentence Classification
Prior to their application in NLP
Convolutional Neural Networks (CNNs)provided groundbreaking results computer vision with the advent of
AlexNetIn NLP instead of convolving over pixels convulsion filters are applied and pooled sequentially over individual or groups of word vectors
In NLP CNNs are able to model local ordering by acting as n-gram feature extractors for embeddings. CNN models have contributed to state of the art results in classification and a variety of other NLP tasks.
More recently the work of
Jacovi and Golberg et al, has contributed to deeper understanding of what convolutional filters learn by demonstrating that filters are able to model rich semantic classes of n-grams by using different activation patterns, and that global max-pooling induces behavior which filters out less relevant n-grams from model decision process.
A good primer on getting started with 1D CNNs can be found in the embedded link below.
Deep Learning for Natural Language Processing — Part III
It’s been a month since I wrote the first part of this series. There, I shared the bit I know about word vector…medium.comRNNs (LSTM/GRU)
Building on the local ordering provide by CNNs Recurrent Neural Networks (RNNs) and their gated cell variants such as Long Short Term Memory Cells (LSTMs) and Gated Recurrent Units (GRUs) provide mechanisms for modeling sequential ordering and mid range dependencies in text such as the affect of a word in the beginning of a sentence on the end of a sentence.
Illustrated Guide to LSTM’s and GRU’s: A step by step explanation
Hi and welcome to an Illustrated Guide to Long Short-Term Memory (LSTM) and Gated Recurrent Units (GRU). I’m Michael…towardsdatascience.comAdditional variations of RNNs such as
Bidirectional-RNNs which process text in both left to right and right to left and
character level RNNs for enhancing underrepresented or out of vocabulary word embeddings led to many state of the art neural
NLP breakthroughs.

An sample of some different RNN architectures and coupled with example use cases.
Attention and Copy Mechanisms
While standard RNN architectures have led to incredible
breakthroughs in NLP they suffer from a variety of challenges. While in theory they can capture long term dependencies they tend to struggle modeling longer sequences, this is still an open problem.
One cause for sub-optimal performance standard RNN encoder-decoder models for sequence to sequence tasks such as
NER or translation is that they weight the impact each input vector evenly on each output vector when in reality specific words in the input sequence may carry more importance at different time steps.
Attention mechanisms provide a means of weighting the contextual impact of each input vector on each output prediction of the RNN. These mechanisms are responsible for much of the current or near current state of the art in Natural language processing.

An example of an attention mechanism applied to the task of neural translation in Microsoft Translator
Additionally in
Machine Reading Comprehension and
Summarization systems RNNs often tend to generate results, that while on first glance look structurally correct are in reality hallucinated or incorrect. One mechanism that helps mitigate some of these issues is the
Copy Mechanism.

Copy Mechanism from Get To The Point: Summarization with Pointer-Generator Networks Abigail See, et all
The copy mechanism is an additional layer applied during decoding that decides whether it is better to generate the next word from the source sentence or from the general embedding vocabulary.
Putting it all together with ELMo and BERT
ELMo is a model generates embeddings for a word based on the context it appears thus generating slightly different embeddings for each of its occurrence.

For example, the word “play” in the sentence above using standard word embeddings encodes multiple meanings such as the verb to play or in the case of the sentence a theatre production. In standard word embeddings such as Glove, Fast Text or Word2Vec each instance of the word play would have the same representation.
ELMo enables NLP models to better disambiguate between the correct sense of a given word. On in it’s release it enabled near instant state of the art results in many downstream tasks, including tasks such as co-reference were previously not as viable for practical usage.

ELMo also provides promising implications for preforming transfer learning on out of domain datasets. Some such as Sebastien Ruder have even hailed the coming ELMo as the
ImageNet moment of NLP and while ELMo is a very promising development with practical real world applications, and has spawned recent related techniques such as
BERT, that use attention transformers instead of bi-directonal RNNs to encode context, we will see in our upcoming post that there are still many obstacles in the world of Neural NLP.

Comparsion of
BERT and ELMo architectures from Devlin et. all
Call To Action: Getting Started
Below are some resources to get started with the the different word embeddings above.
Documentation
Getting started with pyTorch and Docker on the Azure DLVMCharacter RNN classification with pyTorchFast Text TutorialKeras NLP introTools
GensimWord2VecSci-Kit Learn Bag of WordsSpaCy ExamplesPyTorch TextAllen NLP ELMoHugging Face BERT PyTorchAdditional ResourcesOpen Dataset
Dual Word Embeddings Trained on Bing QueriesNext PostNow that we have a solid understanding of some of the milestones in neural NLP, as well as the models and representations in the
next post will review some of the pitfalls of current state of the art NLP systems.
If you have any questions, comments, or topics you would like me to discuss feel free to follow me on
Twitter.
About the Author
Aaron (Ari) Bornstein is an avid AI enthusiast with a passion for history, engaging with new technologies and computational medicine. As an Open Source Engineer at Microsoft’s Cloud Developer Advocacy team, he collaborates with Israeli Hi-Tech Community, to solve real world problems with game changing technologies that are then documented, open sourced, and shared with the rest of the world.