As we learned what a Transformer is and how we might train the Transformer model, we notice that it is a great tool to make a computer understand human language. However, the Transformer was originally designed as a model to translate one language to another. If we repurpose it for a different task, we would likely need to retrain the whole model from scratch. Given the time it takes to train a Transformer model is enormous, we would like to have a solution that enables us to readily reuse the trained Transformer for many different tasks. BERT is such a model. It is an extension of the encoder part of a Transformer.
In this tutorial, you will learn what BERT is and discover what it can do.
After completing this tutorial, you will know:
- What is a Bidirectional Encoder Representations from Transformer (BERT)
- How a BERT model can be reused for different purposes
- How you can use a pre-trained BERT model
Transformer Model to BERT
In the transformer model, the encoder and decoder are connected to make a seq2seq model so you can perform a translation, such as from English to German, as you saw before.
But each of the and above is an embedding vector transformed by a weight matrix in the transformer model. Training a transformer model means finding these weight matrices. Once the weight matrices are learned, the transformer becomes a language model, which means it represents a way to understand the language that you used to train it.
A transformer has an encoder and decoder parts. As the name implies, the encoder transforms sentences and paragraphs into an internal format (a numerical matrix) that understands the context, whereas the decoder does the reverse. Combining the encoder and decoder allows a transformer to perform seq2seq tasks, such as translation. If you take out the encoder part of the transformer, it can tell you something about the context, which can do something interesting.
The Bidirectional Encoder Representation from Transformer (BERT) leverages the attention model to get a deeper understanding of the language context. BERT is a stack of many encoder blocks. The input text is separated into tokens as in the transformer model, and each token will be transformed into a vector at the output of BERT.
What Can BERT Do?
A BERT model is trained using the masked language model (MLM) and next sentence prediction (NSP) simultaneously.
Each training sample for BERT is a pair of sentences from a document. The two sentences can be consecutive in the document or not. There will be a [CLS]
token prepended to the first sentence (to represent the class) and a [SEP]
token appended to each sentence (as a separator). Then, the two sentences will be concatenated as a sequence of tokens to become a training sample. A small percentage of the tokens in the training sample is masked with a special token [MASK]
or replaced with a random token.