Natural Language Processing (NLP) is a large field where we can find different tasks: Text Classification, Named Entities Recognition, Language Translation... These tasks have a common challenge: texts written with human languages (usually unstructured texts). The task that concerns us in this article is text generation using a Conditional Language Model and the novel Transformers architecture.
In order to understand Text Generation, it is necesary to define what is a Language Model (LM). From Wikipedia, "a Statistical Language Model is a probability distribution over sequences of words, such that, given a sequence of length m, it assigns a probability P(w 1 , . . . , w m ) to the whole sequence.". In consequence, we can use a Conditional LM to find the probability of the next word in a sequence: P(w m+1 |w 1 , . . . , w m ).
In this article, we assume that you have fundamental knowledge of deep learning, word vectors and embedding space. Nevertheless, here we describe some models and techniques which are relevant to understand Transformer-based models.
During a long time, Conditional Text Generation was based in Seq2Seq models [1] . The idea behind Seq2Seq consists of 2 Recurrent Neural Networks (RRN) that try to predict the next state sequence from the previous one (the two RNNs receive the names encoder and decoder respectively). Two of the most extended RNNs were LSTM, introduced in 1997 by Sepp Hochreiter and Jurgen Schmidhuber [2] , and GRU, introduced in 2014 by Junyoung Chung et al [3] . However, texts generated with RNNs are far from being perfect: they tend to be nonsense and sometimes they include spelling mistakes. Basically, one wrong prediction has the potential to make the entire sentence meaningless. Furthermore, it is not possible to apply parallelization since the RNNs need to process data as a sequence. Figure 1 : This is the architecture of a Seq2seq model: the first RNN is called encoder and the second one is decoder. In this case, the model receives an input sentence "ABC" and produces "XYZ" as the output sentence (the input and output may have different lengths).
Traditionally, a model received a sequence of tokens (usually words) and transformed them into static vectors. In short, they are simply vectors of numbers that represent the meaning of a word. One widely extended model is Word2vec (introduced by Mikolov et al. in 2013) which computes the static vector of each token (the vectors are called embeddings) [4] . Furthermore, vectors of Word2vec provided State-Of-The-Art (SOTA) performance in syntactic and semantic word similarities.
The power of using word vectors is that they lend themselves to mathematical operators. For example, we can add and subtract vectors:
The recent techniques consists of incorporating context into word embeddings: replacing static vectors with contextualized word representations has led to significant improvements on virtually every NLP task. ELMo introduced this kind of word embeddings in 2018 [5] : "vectors are learned functions of the internal states of a deep bidirectional language model (biLM)". This is one of the breakthroughs which will lead models to further understanding of words. For example, they may difference homonyms (e.g. rock can be a stone or a music genre) instead of having the same static vector for them.
Google's researches released a new model called Transformer in 2017 in the paper "Attention Is All You Need" [6] . Very briefly, this architecture consists of self-attention and point-wise, fully connected layers (see Figure  2) . Similarly to what we describe for Seq2seq, Transformers include an encoder, decoder and a final linear layer. These models are designed to handle sequences of data (especially useful in NLP). Note that, in contrast to Seq2seq they do not contain recurrence or convolution, so they do not require to process sequences in order. This fact allows us to parallelize (much more than RNNs) and reduces training time.
. BERT
Bidirectional Encoder Representations from Transformers (commonly known by its abbreviated form BERT), as the name suggests, "is designed to pretrain deep bidirectional representations from unlabeled text by jointly conditioning on both left and right context in all layers" [7] . The model architecure is a technical innovation that implements multiple layers of Transformer-encoders to language modelling.
The authors of BERT describe two steps in their framework: pre-training and fine-tuning (see Figure 3 ).
1. During pre-training, the model is trained on unlabeled data over different pre-training tasks: Masked LM and Next Sentence Prediction. The authors pretrained BERT on the BooksCorpus (800M words) and Wikipedia (2, 500M words).
During fine-tuning, the model is initialized with the pre-trained parameters (this is also known as transfer learning). Then, each downstream task is fine-tuned separately. This process is simple, so it is not described in this article.
Overall pre-training and fine-tuning procedures described in BERT's paper [7] .
. .
In order to make BERT handle a variety of downstream tasks, the authors defined three different inputs which allow us to unambiguously represent both a single sentence and a pair of sentences.
One of these inputs is the tokenized text using a technique called WordPiece tokens (i.e. sub-words units) [8] . Apart from the sub-words units, the authors introduced two new tokens that must be appended to the input sentences: [CLS] in the beginning of the input and [SEP] after each sentence.
The second and third inputs are sequences of 0s and 1s. One (called Token Type IDs or Segment IDs) indicates that a token belongs to the sentence A (a series of 0s) or the sentence B (a series of 1s). The other (called Mask IDs) is used when input texts are padded to same length (indicates whether the text is padded from a certain position). As you can see in Figure 4 , not all tokens are words, thus it is easier to handle unknown words. Word pieces are very powerful in the sense that tokens cover all the word (even the words that do not occur in the dictionary) and we do not lose information (since all subword units are in the input).
. .
Masked Language Model (MLM). In this task, authors masked 15% of WordPiece tokens (in each sequence of the dataset at random) and then predict those masked tokens. They only mask 15% of tokens since the token [MASK] is not used in fine-tuning and they may create a mismatch between pre-training and fine-tuning.
Next Sentence Prediction (NSP). In words of the authors, "tasks such as Question Answering (QA) and Natural Language Inference (NLI) are based on understanding the relationship between two sentences, which is not directly captured by language modeling". In order to train a model that understands sentence relationships, given a pair of sentences, the model should predict if the second sentence is the subsequent sentence in the original document. The authors built a dataset where 50% is the actual next sentence and 50% is a random sentence from a monolingual corpus.
.
GPT-2
Generative Pretrained Transformer 2, known by GPT-2, is a large unsupervised transformer-based language model and the successor to GPT [9] . GPT-2 was introduced in June 2018 by researchers from OpenAI in their paper "Language Models are Unsupervised Multitask Learners" [10] .
GPT-2 consists of solely stacked decoder blocks from the transformer architecture. In the vanilla transformer architecture, the decoder is fed a word embedding concatenated with a context vector, both generated by the encoder. In GPT-2 the context vector is zero-initialized for the first word embedding. Furthermore, in the vanilla transformer architecture self-attention is applied to the entire surrounding context (e.g. all of the other words in the sentence), but in GPT-2 masked self-attention is used instead: the decoder is only allowed (via obfuscation masking of the remaining word positions) to glean information from the previous words in the sentence (plus the word itself). Besides this, GPT-2 is a close copy of the vanilla transformer architecture and very similar to its predecessor GPT.
Authors of GPT-2 trained it with a simple objective: given some text, predict the next word. For this purpose, they used around 40GB of crawled data from internet. Also, in a similar way as BERT, they fine-tuned GPT-2 in downstream tasks to analyse the model performance in different situations. In GPT-2, authors moved the layer normalization to the input of each sub-block and also added another layer normalization after the final self-attention block.
. .
GPT-2 uses Byte Pair Encoding (BPE) as input representation [11] . This technique allows us to combine the empirical benefits of word-level LMs with the generality of byte-level approaches. BPE is a simple compression method in which the most common pair of consecutive bytes of data is replaced with a byte that does not occur within that data. For instance, given the sequence aaabdaaabac and Z = aa, then we obtain ZabdZabac.
It is important to highlight the fact that the authors do not apply any kind of pre-processing to the data (e.g. lowercasing, tokenization or out-of-vocabulary tokens). They believe that "a general language model (LM) should be able to compute the probability of (and also generate) any string".
.
As we have explained before, one of the pretraining tasks of BERT was MLM so, for this experiment, we only need a pretrained BERT model and HuggingFace 1 already provides a wide variety of pretrained and fine-tuned models in PyTorch or TensorFlow. In particular, we chose BERT multilingual base model which is a pretrained model on the top 104 languages with the largest Wikipedia using a MLM objective. Then, we apply the softmax function to normalize the output and pick the top words.
Since this experiment is really simple and does not require any further training, we proceeded to test it out.
Despite infered words are correct in many cases, there are other cases where the word suggestions are far to be good. For instance, the first word of "Today is [MASK] ." (written in English) is "demolished" (it is gramatically correct, but it is very uncommon). However, if we use a only-English pretrained model (we used BERT base model), the first output is "closed". Also, in non-English languages, sometimes it is unable to return a word but punctuation symbols.
We believe that we must use monolingual models to obtain good results. In other words, we must train one BERT model per language. There are a wide list of arguments for this which are described in the conclusions section.
You can find some infered masked tokens of our experiments in the appendix. There are a wide variety of tests in three languages: English, Spanish and Russian.
.
In Question-Answering tasks, the model receives a text (called context) and a question regarding to the context, and it should mark the inital and ending of the answer in the context (n)ote that the model does not generate text).
Similarly to the previous experiment, we import BERT multilingual base model from HuggingFace. Then, we add two fully-connected layers to obtain the initial and ending token positions from the context (see Figure 9 ). Based on these positions, we pick a portion of context and return it as the answer to the question. However, we cannot test it without previous training (fine-tuning BERT and adjust weights of fully-connected layers). In other words, we are using a technique called transfer learning: the model is pre-trained in a dataset A and, then, we use that pre-trained model to carry that knowledge into solving dataset B. ). The answer is good but it is not stricly accurate: it should be only "1804" since we did not ask for the period.
In order to train this QA model, we use XQuAD 2 . This dataset consists of a subset of 240 paragraphs and 1190 question-answer pairs from the development set of SQuAD v1.1 [12] together with their professional translations into ten languages, such as Spanish and Russian.
In our experiments, answers tend to be accurate when we ask simple questions and when we use similar words as the context (see Figure 8 ). We observe that it is particularly difficult to the model to understand some synonyms and homonyms. For example, we ask (in Spanish) "When was Napoleon crowned king of Italy?" and "When did he become monarch?", the first gives a good answer "March 18th, 1805" (strictly it was on May 26th) and the second returns a totally wrong answer "November 11th, 1799".
You can find some questions and answers of our experiments in the appendix. There are a wide variety of questions in three languages: English, Spanish and Russian. Overall, the model answers correctly when we write questions in English.
.
In this task, we experimented with GPT-2 due to BERT is not designed for text generation. GPT-2 was only trained to predict the next token in a sentence, but it surprisingly learned basic competence in some tasks like translating between languages and answering questions (see Figure 10) .
GPT-2 has the ability to generate conditional text samples of unprecedented quality. In the appendix, you can observe that the model is capable of generating synthetic texts close to human quality. We provided different kind of prompts to analyse the versatility of GPT-2 and, despite it shows coherence and good quality (in particular, when we write about topics highly represented in the training data), we found failures, such as sudden topic switching, repetitive text and LM failures. For example, sometimes the model negates what it just wrote in the previous sentences. In Figure  10 , even though text quality is good, you can observe that it also wrote the extravagant sentence "if the ship had not been in the water"
As we explain later, GPT-2 and BERT architectures are different (the first is a Transformer-encoder architecture and the second is a Transformer-decoder one). This fact lead that we cannot use BERT as-it-is for text generation. Nevertheless, there are some efforts to create new BERT-based algorithms for this purpose (the main idea consists of adding a decoder-transformer in the end). For example, the recent paper CG-BERT [13] looks very promising in this task, but we could not include it in this article due to lack of time and resources to train the model. The main idea of CG-BERT is to use half layers of BERT as encoders and half layers as decoders.
In addition to BERT and GPT-2, we tried to implement a simple Transformer model to be capable of generating a conversation. The result is poor since the training data is really small as well as the resources to train the model. You can read the details in the appendix.
.
Architecture and pre-training
One difference that we encounter between BERT and GPT-2 is in the architecture: despite both are transformers-based architectures, it uses encoders in the case of BERT and, in the case of GPT-2, decoders. This particularity makes BERT and GPT-2 to understand text information in a different way and, in consecuence, their performance variate depending on the dataset. We find another difference in the pre-training method: GPT-2 aims to predict the next word in a sentence, while BERT is trained in NSP and MLM. We should keep in mind these pre-training methods when we use transfer learning to fine-tune a model.
In order to evaluate BERT and GPT-2, authors prepared different versions of both models (with different number of parameters) and fine-tuned them in different downstream tasks. One version is BERT Base, which has the same number of parameters as GPT (the predecesor of GPT-2) for comparison purposes. Unfortunately, we could not find common benchmarks between BERT and GPT-2 in the same datasets, so we only can compare BERT and GPT in GLUE and SWAG tasks.
GLUE [14] , which consists of a wide list of NLP tasks (sentiment analysis, similarity, NLI, question-answering...), is one of the dataset where BERT was fine-tuned. In this dataset, BERT Base already outperformed GPT (and prior approaches such as BiLSTM+ELMo) and BERT Large got an even better result 3 , thus it achieved the status SOTA in this collection of tasks.
SWAG [15] consists of 113k multiple choice questions about grounded situations: given a question/situation, we have to pick the correct answer among four choices. Identically as GLUE, BERT outperformed GPT (BERT Large improved GPT score by 8.3%). It is also surprising that BERT Large got a better score than a human expert in a small subset of 100 questions.
The models described in this article, BERT and GPT-2, demonstrate the benefits of large scale language modeling. Both papers leverage advances in compute and available text corpora to significantly surpass state-of-the-art performance in Natural Language Understanding (NLU), modeling, and generation. Currently, there are new models (based on BERT and GPT-2 architectures) which get even better results. Some of them are RoBERTa (robustly optimized version of BERT with larger training data) [16] , ALBERT (lite version of BERT) [17] , DistilBERT (also a reduced version of BERT) [18] and StructBERT (incorporate language structures into BERT pre-training) [19] 4 5 6 .
Appart from the mentioned BERT-based models, Nvidia published Megatron-LM [20] in 2019. In a few words, it is a simple and efficient model parallel approach by modificating existing PyTorch transformer implementations. They released a 3.9 billion parameters version of BERT and 8.3 of GPT-2. Both Megatron models got better scores than both vanilla BERT and GPT-2 models in the same tasks. Additionally, OpenAI published recently a new model called GPT-3 (it uses the same architecture as GPT-2) [21] . The largest version of this novel model has 175 billion parameters which is 10 times more than any previous non-sparse language mode. However, despite the strong quantitative and qualitative improvements of GPT-3, particularly compared to its direct predecessor GPT-2, it still has notable weaknesses in text synthesis and several NLP tasks (they are extensively described in the paper). Unfortunately, we could not include text samples generated by GPT-3 in this article due to OpenAI has not released the model yet.
Transformers disrupted sequence-based deep learning significantly. The two variants of transformer models that we showed, BERT and GPT-2, outperform previous approaches with RNNs. In both cases, models take advantage of Attention layers, which selectively weight different elements in input data, and can achieve state-of-the-art performance on many language modelling benchmarks.
Although there are some situations where the output of Transformers-based models is close to the human quality, we find that the output quality depends on how familiar is the model with the topic. For instance, GPT-2 is not able to generate a high-quality text when we provide an uncommon context (such as the recent COVID-19 pandemic or a new popular person). In the case of BERT, we observe that the model tend to answer incorrectly (in question-answering tasks) when we use homonyms or synonyms instead of the same words as the context. These experiments show us that still computers are distant from fully understanding of unstructured texts written in human languages.
We observe a trend to build larger models in the NLP field, so they can capture more information from unstructured texts. Furthermore, unlikely humans, they need to be trained with tons of data. In terms of zero-shot or one-shot tasks, we need to improve the efficiency of NLP algorithms since they see much more text than a human sees in their lifetime.
In general, we observe that despite these models achieve better performance than us in specific tasks, the common sense of humans produces better results rather than any deep learning model. Also, we cannot ignore the fact that we learn from our daily life, but it is difficult to measure what type of data should we provide to a model (during pre-training) to get a similar knowledge. This is the most likely reason deep learning models fail to produce coherent texts.
In addition to the previous points, we observe a growing need to pre-train a model per language (BERT, GPT-2 or any other model). Apart from the grammar rules of each language (or, for example, the evident difference of RTL and LTR languages), there is knowledge which is intrinsic to the language: polite expressions, informal style... If we want to obtain human-quality in text generation, we will need large dataset not only in English but also in other languages (we cannot use translated data).
In summary, we will see more transformer-based models in the future. They have demonstrated superiority in parallel computation and modelling long-range dependencies, compared to RNNs (such as LSTM). Still, we do not know which is the best approach to pre-train them as well as how to reproduce the human common sense (this is mandatory in text generation). Thus it is very likely that we are going to see not only new pre-training methods but also larger models.
.
Despite some options could fit into philosophical or rare situations, we believe that they are uncommon options to replace the masked token. It also occurs that some options are grammatically correct, but they do not fit in the context of the sentence. In both cases, we note them as "incorrect".
Hello, I'm a [MASK] model.
All correct.
Today is [MASK] .
Only the third option, closed, seems correct.
[MASK] is a good idea. Spanish. Description about Sagrada Familia extracted from Wikipedia 9 .
El Tempo Expiatorio de la Sagrada Familia (en catalán, Temple Expiatori de la Sagrada Família), conocido simplemente como la Sagrada Familia, es una basílica católica de Barcelona (España), diseñada por el arquitecto Antoni Gaudí. Iniciada en 1882, todavía está en construcción. Es la obra maestra de Gaudí, y el máximo exponente de la arquitectura modernista catalana. Es uno de los monumentos más visitados de España, junto al Museo del Prado y la Alhambra de Granada, y es la iglesia más visitada de Europa tras la basílica de San Pedro del Vaticano. Cuando esté finalizada será la iglesia cristiana más alta del mundo.
Answer Notes .
The following synthetic texts were generated by GPT-2. We fed the model with an initial text and then we let GPT-2 to finish it. These text samples are written only in English due GPT-2 end up in nonsense outputs using other languages. Note that the model does not give the same output everytime we run it, so you might obtain a different output.
Last week, many people reported that they saw a unicorn at the park. It turned out to be a real unicorn, but it was just a fake. The unicorn was made of foam and plastic. The foam was made of polystyrene, which is a plastic that is commonly used in construction materials. The fake unicorn was made of foam, which has been known to be a very effective material for creating fake animals, such as this one. The foam was made of polystyrene, [...] In the last months, a new virus spread worldwide and changed our daily lifes. This new virus, called Ebola, appeared in three countries, and has killed more than 3,000 people in the four countries: Guinea, Liberia and Sierra Leone. The virus is spread through direct contact with the fluids of infected persons, which can include body fluids, such as blood, sweat and tears. . . It is also spread through contaminated food and water. When Ebola arrives in a country, a lot of people [...] Q: How would you describe to Maria? A: I'd say. . . I'd say that she 's like very strong and very resilient. Q: Thank you very much. A: Oh, thanks for the offer to talk. Q: What do you think about the death penalty? A: I am not an advocate for the death penalty. I am not someone who believes that this life should be taken and then taken over again, which of course is the case with every case when we see a murder. It is my belief that a fair penalty should be used, and that if the evidence was not sufficient to prove that the person committed the actual crime, then there should be a life sentence.
Teacher: Good morning everyone. Students: Good morning teacher. Teacher: It's my pleasure to welcome you all here today. Students: Yeah. Teacher: I want to talk about some things that you all do. Students: Yeah. Teacher: You guys always seem to have such great enthusiasm. Students: Yeah. Teacher: So, we'll start with our first topic. What's your favorite type of book to read? : 21 :
We implemented a simple model (see Figure 12 ) to figure out whether we are able of generating text based on training data. For this purpose, we took an English dialogue written by Shakespeare 10 . The model consists of 2 transformer-encoders and 2 transformer-decoders to capture the text information, and a fully-connected layer to obtain the prediction for the next token in the text. We kept the model simple due to two reasons:
• We do not have enough resources to train a large model like BERT or GPT-2.
• If the model was larger, we would also need a bigger dataset to train all its parameters. Unfortunately, the model predictions are really bad in the sense that it outputs nonsense tokens to continue a text. Here you can see the sample text that the model generated from the initial text "ROMEO:". It is very likely that we need to increase the model size to better capture the text information (in consecuence, we need to use more data to train it) and, therefore, generate a coherent text.
ROMEO: took perform still sets big,KATHARINA'd me my heart l belie to harpets'd upon me And sure darkness under'd statutes. deceive them or orderly and likely body will f roodFor Masteredon'd conclude else aged asTherefore The suffer hiswittedWould being royalties and to ouroan as which and than him The goddess wasYour things curst hag that they admirings that how of they twere to spirit: linger OF glory of follow ised greatestar