Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!

Publication

Transformers Well Explained: Masking
Latest   Machine Learning

Transformers Well Explained: Masking

Last Updated on March 13, 2024 by Editorial Team

Author(s): Ahmad Mustapha

Originally published on Towards AI.

This is the second part of a four-article series that explains transforms. Each article is associated with a hands-on notebook. In the previous article, we explained word embeddings in detail and then trained an embedding with the task of predicting the third word of a trigram given two previous words. In this article, we will do the same but with the task of figuring out masked words.

Photo by Mike Uderevsky on Unsplash

N.B.: Make sure to read part one first.

Masking

Masking is simply the act of hiding a word and asking the model to predict it. Like in the following “Being strong is <mask> what matters”. It is a different task that forces the model to generate an embedding with more contextual semantics in theory.

When we ask a model to predict a third word given the previous two words, we call this a one-directional language model. The model will be trying to predict the word given the previous context on the left. However, when we train on masking, the model makes predictions based on the context from the left and the right. We call this a bidirectional language model.

Sequence Size, <PAD>, <MASK>

Because we are predicting a masked word in a complete sentence, we will not be fed to the model one or two words but rather multiple words. One key characteristic of any transform is its “sequence size” or how many words it feeds on in the feedforward step. We can set the “sequence size” to be 20, 50, or 100 tokens. This means that every sentence should be of length equal to “sequence size”. What if the sentence size is less than the target size? That is why we introduced a new special token similar to the “<UNKNOWN>” token introduced in part one. The token is “<PAD>” and the role of this token is to pad sentences so that their size reaches the target “sequence size”. In the below example, we padded a sentence to reach the sequence size of 10 tokens.

Being strong is all what matters <PAD> <PAD> <PAD> <PAD>

We also added the “<MASK>” special token. For each complete sentence, we mask 10% or 15% of its tokens. To mask a token, we simply replace it with the “<MASK>” token. By doing this we prevent the network from using the masked token embedding to predict it. It should be predicted from the context of the left and the right. In the example below, we mask two words from the entire sentence.

Being strong <MASK> all <MASK> matters <PAD> <PAD> <PAD> <PAD>

Training Task

Many transformers' tutorials, when considering transformers, miss a very important detail on how the loss is computed for masked words. The input size for a transform (the embedding in our case, so far) is the sequence length. Every token is mapped to its index in the vocabulary list. The example sentence will be fed as follows.

Original:
Being U+007C strong U+007C is U+007C all U+007C what U+007C matters U+007C <PAD> U+007C <PAD> U+007C <PAD> U+007C <PAD>

Embedding:
22 U+007C 433 U+007C 190 U+007C 333 U+007C 355 U+007C 331 U+007C 2 U+007C 2 U+007C 2 U+007C 2

Masked:
Being U+007C strong U+007C <MASK>U+007C all U+007C <MASK> U+007C matters U+007C <PAD> U+007C <PAD> U+007C <PAD> U+007C <PAD>
22 U+007C 433 U+007C 1 U+007C 333 U+007C 1 U+007C 331 U+007C 1 U+007C 1 U+007C 1 U+007C 1

Assume that we have 60k words in the vocabulary and the sequence size is 10 (usually much more). The model will have to predict the class of each token in the sequence size. Then the model output or the logits of the model will be of size 10 x 60,000.

But do we compute the loss over all the tokens? If the majority of the tokens are not hidden why we should ask the model to train on them and to compute the loss considering them? The answer is no, we only train on the masked tokens. The trick is to have the target of a prediction as the input sequence; however, with all non-masked tokens mapped to “<PAD>” and then we ignore the “<PAD>” class or token when computing the loss. To do this we can use the argument “ignore-index” in Pytorch’s “Cross Entropy Loss”. So, the target will end up being like this:

Original:
Being U+007C strong U+007C is U+007C all U+007C what U+007C matters U+007C <PAD> U+007C <PAD> U+007C <PAD> U+007C <PAD>

Embedding:
22 U+007C 433 U+007C 190 U+007C 333 U+007C 355 U+007C 331 U+007C 2 U+007C 2 U+007C 2 U+007C 2

Masked:
Being U+007C strong U+007C <MASK>U+007C all U+007C <MASK> U+007C matters U+007C <PAD> U+007C <PAD> U+007C <PAD> U+007C <PAD>
22 U+007C 433 U+007C 1 U+007C 333 U+007C 1 U+007C 331 U+007C 1 U+007C 1 U+007C 1 U+007C 1

Target:
<PAD> U+007C <PAD> U+007C is U+007C <PAD> U+007C what U+007C <PAD> U+007C <PAD> U+007C <PAD> U+007C <PAD> U+007C <PAD>
2 U+007C 2 U+007C 190 U+007C 2 U+007C 355 U+007C 2 U+007C 2 U+007C 2 U+007C 2 U+007C 2

The full code of training an Arabic embedding using masking is available in the “Generate Embeddings — Mask .ipynb” notebook.

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI

Feedback ↓