Masked language modeling
Last updated
Last updated
Masked language modeling predicts a masked token in a sequence, and the model can attend to tokens bidirectionally. This means the model has full access to the tokens on the left and right. Masked language modeling is great for tasks that require a good contextual understanding of an entire sequence. BERT is an example of a masked language model.
This guide will show you how to:
Finetune on the subset of the dataset.
Use your finetuned model for inference.
You can finetune other architectures for masked language modeling following the same steps in this guide. Choose one of the following architectures:
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
Before you begin, make sure you have all the necessary libraries installed:
Copied
We encourage you to log in to your BOINC AI account so you can upload and share your model with the community. When prompted, enter your token to log in:
Copied
Start by loading a smaller subset of the r/askscience subset of the ELI5 dataset from the ๐Datasets library. Thisโll give you a chance to experiment and make sure everything works before spending more time training on the full dataset.
Copied
Copied
Then take a look at an example:
Copied
While this may look like a lot, youโre only really interested in the text
field. Whatโs cool about language modeling tasks is you donโt need labels (also known as an unsupervised task) because the next word is the label.
For masked language modeling, the next step is to load a DistilRoBERTa tokenizer to process the text
subfield:
Copied
Copied
Each subfield is now a separate column as indicated by the answers
prefix, and the text
field is a list now. Instead of tokenizing each sentence separately, convert the list to a string so you can jointly tokenize them.
Here is a first preprocessing function to join the list of strings for each example and tokenize the result:
Copied
Copied
This dataset contains the token sequences, but some of these are longer than the maximum input length for the model.
You can now use a second preprocessing function to
concatenate all the sequences
split the concatenated sequences into shorter chunks defined by block_size
, which should be both shorter than the maximum input length and short enough for your GPU RAM.
Copied
Apply the group_texts
function over the entire dataset:
Copied
PytorchHide Pytorch content
Use the end-of-sequence token as the padding token and specify mlm_probability
to randomly mask tokens each time you iterate over the data:
Copied
TensorFlowHide TensorFlow content
Use the end-of-sequence token as the padding token and specify mlm_probability
to randomly mask tokens each time you iterate over the data:
Copied
PytorchHide Pytorch content
Copied
At this point, only three steps remain:
Copied
Copied
Copied
TensorFlowHide TensorFlow content
To finetune a model in TensorFlow, start by setting up an optimizer function, learning rate schedule, and some training hyperparameters:Copied
Copied
Copied
Copied
Copied
Copied
Once training is completed, your model is automatically uploaded to the Hub so everyone can use it!
Great, now that youโve finetuned a model, you can use it for inference!
Come up with some text youโd like the model to fill in the blank with, and use the special <mask>
token to indicate the blank:
Copied
Copied
PytorchHide Pytorch content
Tokenize the text and return the input_ids
as PyTorch tensors. Youโll also need to specify the position of the <mask>
token:
Copied
Pass your inputs to the model and return the logits
of the masked token:
Copied
Then return the three masked tokens with the highest probability and print them out:
Copied
TensorFlowHide TensorFlow content
Tokenize the text and return the input_ids
as TensorFlow tensors. Youโll also need to specify the position of the <mask>
token:
Copied
Pass your inputs to the model and return the logits
of the masked token:
Copied
Then return the three masked tokens with the highest probability and print them out:
Copied
Split the datasetโs train_asks
split into a train and test set with the method:
Youโll notice from the example above, the text
field is actually nested inside answers
. This means youโll need to e xtract the text
subfield from its nested structure with the method:
To apply this preprocessing function over the entire dataset, use the ๐ Datasets method. You can speed up the map
function by setting batched=True
to process multiple elements of the dataset at once, and increasing the number of processes with num_proc
. Remove any columns you donโt need:
Now create a batch of examples using . Itโs more efficient to dynamically pad the sentences to the longest length in a batch during collation, instead of padding the whole dataset to the maximum length.
If you arenโt familiar with finetuning a model with the , take a look at the basic tutorial !
Youโre ready to start training your model now! Load DistilRoBERTa with :
Define your training hyperparameters in . The only required parameter is output_dir
which specifies where to save your model. Youโll push this model to the Hub by setting push_to_hub=True
(you need to be signed in to BOINC AI to upload your model).
Pass the training arguments to along with the model, datasets, and data collator.
Call to finetune your model.
Once training is completed, use the method to evaluate your model and get its perplexity:
Then share your model to the Hub with the method so everyone can use your model:
If you arenโt familiar with finetuning a model with Keras, take a look at the basic tutorial !
Then you can load DistilRoBERTa with :
Convert your datasets to the tf.data.Dataset
format with :
Configure the model for training with . Note that Transformers models all have a default task-relevant loss function, so you donโt need to specify one unless you want to:
This can be done by specifying where to push your model and tokenizer in the :
Finally, youโre ready to start training your model! Call with your training and validation datasets, the number of epochs, and your callback to finetune the model:
For a more in-depth example of how to finetune a model for masked language modeling, take a look at the corresponding or .
The simplest way to try out your finetuned model for inference is to use it in a . Instantiate a pipeline
for fill-mask with your model, and pass your text to it. If you like, you can use the top_k
parameter to specify how many predictions to return: