This is not your average tutorial on GPT-2! I will not focus on the original purpose of GPT-2 - text generation.
In this tutorial we'll use GPT-2 in a less conventional way - for text classification.
Agenda
Intro
Understanding Natural Language Processing
Word embeddings
Text Classification
The Transformer
GPT-2
Architecture
Inner Workings
Prediction
Classification
Coding Session
Conclusions
Intro
Wikipedia
Natural language processing (NLP) is a subfield of linguistics, computer science, and artificial intelligence concerned with the interactions between computers and human language ...
... The result is a computer capable of "understanding" the contents of documents, including the contextual nuances of the language within them.
Generative Pre-trained Transformer 2 (GPT-2) is an open-source artificial intelligence created by OpenAI in February 2019. GPT-2 translates text, answers questions, summarizes passages, and generates text output on a level that, while sometimes indistinguishable from that of humans, can become repetitive or nonsensical when generating long passages.
Architecture
Architecture
We only have the decoder side of Transformer:
Architecture
There are multiple variations of GPT-2 depending on the number of decoder blocks:
GPT-2 translates text, answers questions, summarizes passages, and generates text output...
Let's see how it make predictions.
Prediction
We pass the text "a robot must" to GPT-2.
GPT-2 will output word embeddings for each of the words.
The last word embedding is used to predict the next word "obey".
Prediction
Our new text "a robot must obey" is fed to GPT-2.
Same process is repeated to predict the next word "orders".
Prediction
This is how GPT-2 is able to translates text, answers questions, summarizes passages, and generates text output.
How can we use it for simple text classification?
Classification
Now we'll use GPT-2 to perform text classification.
Classification
Now we'll use GPT-2 to perform text classification.
We'll classify a movie review as being either positive or negative sentiment.
Classification
Let's look at a positive movie review:
Classification
Now let's look at a negative movie review:
Coding Session
GPT-2 For Text Classification using Hugging Face Transformers
Complete tutorial on how to use GPT-2 for text classification.
Disclaimer:The format of this tutorial notebook is very similar to my other tutorial notebooks. This is done intentionally in order to keep readers familiar with my format.
This notebook is used to fine-tune GPT-2 model for text classification using Huggingfacetransformers library on a custom dataset.
Conclusions
I showed that GPT-2 can be used for text classification.
Depending on your data and classification task GPT-2 could outperform other transformers models.