2
$\begingroup$

Masked language modelling is the standard way of training a language model such as a transformer. Each input token has some probability (e.g. 15%) of being replaced with a <MASK> token. The model must predict the original input token at these positions.

There are various variants of this task, such as sometimes replacing the token with a random token or the original token instead of <MASK>, but the original BERT paper (in the ablation studies in the appendix) found that the simple approach of always replacing with <MASK> works well.

My question is: why does this task produce useful embeddings for non-<MASK> tokens? This is the main use of such models, when we use their embeddings for downstream tasks. When the token is replaced with <MASK>, it makes sense to me that the model learns how to use the surrounding tokens to create a contextual representation of what the token is likely to be. But when the token is not <MASK>, the model should know that this is the correct token, and might as well just leave the token alone, yielding a perfect "prediction". In fact, for non-<MASK> tokens, there was no loss during training, so the model had no incentive to do anything in particular for these tokens. So why does it produce good embeddings?

$\endgroup$

1 Answer 1

2
$\begingroup$

You have answered your own question here (note my emphasis):

When the token is replaced with <MASK>, it makes sense to me that the model learns how to use the surrounding tokens to create a contextual representation of what the token is likely to be.

In other words, the contextual representation of a masked token is a function not only of its embedding but also the embeddings of the other tokens in the input, masked or otherwise. Thus, although the loss is only computed for the masked tokens, its gradient flows to all the token embeddings, i.e. they're updated during training. So, the model is "incentivized" to also produce good embeddings for the unmasked tokens.

$\endgroup$
3
  • $\begingroup$ Thanks for the answer, I think this does go most of the way to explaining why it's useful for non-<MASK> tokens to get good embeddings inside the model. There's one thing I'm still unsure of, though. Why does the final self-attention layer still behave well for non-<MASK> tokens? At this point, they no longer affect the <MASK> predictions, so the model is free to "mess them up" however it likes when it produces the final embeddings. However, I imagine it pretty much leaves the non-<MASK> tokens alone. Is that the case? If so, why, when training doesn't encourage it? $\endgroup$
    – Denziloe
    Commented Nov 25, 2023 at 23:52
  • $\begingroup$ @Denziloe you might want to post that as a separate question $\endgroup$
    – kmkurn
    Commented Nov 27, 2023 at 0:31
  • $\begingroup$ I may just edit the original question to clarify, as it's very related and is one of the aspects that was originally confusing me. Do you have some insight into this detail? $\endgroup$
    – Denziloe
    Commented Nov 28, 2023 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.