Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

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 ↓