mirror of
https://github.com/labmlai/annotated_deep_learning_paper_implementations.git
synced 2025-08-14 01:13:00 +08:00
182 lines
8.1 KiB
HTML
182 lines
8.1 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
|
<meta name="description" content=""/>
|
|
|
|
<meta name="twitter:card" content="summary"/>
|
|
<meta name="twitter:image:src" content="https://avatars1.githubusercontent.com/u/64068543?s=400&v=4"/>
|
|
<meta name="twitter:title" content="Masked Language Model (MLM)"/>
|
|
<meta name="twitter:description" content=""/>
|
|
<meta name="twitter:site" content="@labmlai"/>
|
|
<meta name="twitter:creator" content="@labmlai"/>
|
|
|
|
<meta property="og:url" content="https://nn.labml.ai/transformers/mlm/readme.html"/>
|
|
<meta property="og:title" content="Masked Language Model (MLM)"/>
|
|
<meta property="og:image" content="https://avatars1.githubusercontent.com/u/64068543?s=400&v=4"/>
|
|
<meta property="og:site_name" content="LabML Neural Networks"/>
|
|
<meta property="og:type" content="object"/>
|
|
<meta property="og:title" content="Masked Language Model (MLM)"/>
|
|
<meta property="og:description" content=""/>
|
|
|
|
<title>Masked Language Model (MLM)</title>
|
|
<link rel="shortcut icon" href="/icon.png"/>
|
|
<link rel="stylesheet" href="../../pylit.css">
|
|
<link rel="canonical" href="https://nn.labml.ai/transformers/mlm/readme.html"/>
|
|
<!-- Global site tag (gtag.js) - Google Analytics -->
|
|
<script async src="https://www.googletagmanager.com/gtag/js?id=G-4V3HC8HBLH"></script>
|
|
<script>
|
|
window.dataLayer = window.dataLayer || [];
|
|
|
|
function gtag() {
|
|
dataLayer.push(arguments);
|
|
}
|
|
|
|
gtag('js', new Date());
|
|
|
|
gtag('config', 'G-4V3HC8HBLH');
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<div id='container'>
|
|
<div id="background"></div>
|
|
<div class='section'>
|
|
<div class='docs'>
|
|
<p>
|
|
<a class="parent" href="/">home</a>
|
|
<a class="parent" href="../index.html">transformers</a>
|
|
<a class="parent" href="index.html">mlm</a>
|
|
</p>
|
|
<p>
|
|
|
|
<a href="https://github.com/labmlai/annotated_deep_learning_paper_implementations/tree/master/labml_nn/transformers/mlm/readme.md">
|
|
<img alt="Github"
|
|
src="https://img.shields.io/github/stars/labmlai/annotated_deep_learning_paper_implementations?style=social"
|
|
style="max-width:100%;"/></a>
|
|
<a href="https://twitter.com/labmlai"
|
|
rel="nofollow">
|
|
<img alt="Twitter"
|
|
src="https://img.shields.io/twitter/follow/labmlai?style=social"
|
|
style="max-width:100%;"/></a>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div class='section' id='section-0'>
|
|
<div class='docs'>
|
|
<div class='section-link'>
|
|
<a href='#section-0'>#</a>
|
|
</div>
|
|
<h1><a href="https://nn.labml.ai/transformers/mlm/index.html">Masked Language Model (MLM)</a></h1>
|
|
<p>This is a <a href="https://pytorch.org">PyTorch</a> implementation of Masked Language Model (MLM)
|
|
used to pre-train the BERT model introduced in the paper
|
|
<a href="https://papers.labml.ai/paper/1810.04805">BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding</a>.</p>
|
|
<h2>BERT Pretraining</h2>
|
|
<p>BERT model is a transformer model.
|
|
The paper pre-trains the model using MLM and with next sentence prediction.
|
|
We have only implemented MLM here.</p>
|
|
<h3>Next sentence prediction</h3>
|
|
<p>In <em>next sentence prediction</em>, the model is given two sentences <code>A</code> and <code>B</code> and the model
|
|
makes a binary prediction whether <code>B</code> is the sentence that follows <code>A</code> in the actual text.
|
|
The model is fed with actual sentence pairs 50% of the time and random pairs 50% of the time.
|
|
This classification is done while applying MLM. <em>We haven’t implemented this here.</em></p>
|
|
<h2>Masked LM</h2>
|
|
<p>This masks a percentage of tokens at random and trains the model to predict
|
|
the masked tokens.
|
|
They <strong>mask 15% of the tokens</strong> by replacing them with a special <code>[MASK]</code> token.</p>
|
|
<p>The loss is computed on predicting the masked tokens only.
|
|
This causes a problem during fine-tuning and actual usage since there are no <code>[MASK]</code> tokens
|
|
at that time.
|
|
Therefore we might not get any meaningful representations.</p>
|
|
<p>To overcome this <strong>10% of the masked tokens are replaced with the original token</strong>,
|
|
and another <strong>10% of the masked tokens are replaced with a random token</strong>.
|
|
This trains the model to give representations about the actual token whether or not the
|
|
input token at that position is a <code>[MASK]</code>.
|
|
And replacing with a random token causes it to
|
|
give a representation that has information from the context as well;
|
|
because it has to use the context to fix randomly replaced tokens.</p>
|
|
<h2>Training</h2>
|
|
<p>MLMs are harder to train than autoregressive models because they have a smaller training signal.
|
|
i.e. only a small percentage of predictions are trained per sample.</p>
|
|
<p>Another problem is since the model is bidirectional, any token can see any other token.
|
|
This makes the “credit assignment” harder.
|
|
Let’s say you have the character level model trying to predict <code>home *s where i want to be</code>.
|
|
At least during the early stages of the training, it’ll be super hard to figure out why the
|
|
replacement for <code>*</code> should be <code>i</code>, it could be anything from the whole sentence.
|
|
Whilst, in an autoregressive setting the model will only have to use <code>h</code> to predict <code>o</code> and
|
|
<code>hom</code> to predict <code>e</code> and so on. So the model will initially start predicting with a shorter context first
|
|
and then learn to use longer contexts later.
|
|
Since MLMs have this problem it’s a lot faster to train if you start with a smaller sequence length
|
|
initially and then use a longer sequence length later.</p>
|
|
<p>Here is <a href="https://nn.labml.ai/transformers/mlm/experiment.html">the training code</a> for a simple MLM model.</p>
|
|
<p><a href="https://app.labml.ai/run/3a6d22b6c67111ebb03d6764d13a38d1"><img alt="View Run" src="https://img.shields.io/badge/labml-experiment-brightgreen" /></a></p>
|
|
</div>
|
|
<div class='code'>
|
|
|
|
</div>
|
|
</div>
|
|
<div class='footer'>
|
|
<a href="https://papers.labml.ai">Trending Research Papers</a>
|
|
<a href="https://labml.ai">labml.ai</a>
|
|
</div>
|
|
</div>
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-AMS_HTML">
|
|
</script>
|
|
<!-- MathJax configuration -->
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
tex2jax: {
|
|
inlineMath: [ ['$','$'] ],
|
|
displayMath: [ ['$$','$$'] ],
|
|
processEscapes: true,
|
|
processEnvironments: true
|
|
},
|
|
// Center justify equations in code and markdown cells. Elsewhere
|
|
// we use CSS to left justify single line equations in code cells.
|
|
displayAlign: 'center',
|
|
"HTML-CSS": { fonts: ["TeX"] }
|
|
});
|
|
|
|
</script>
|
|
<script>
|
|
function handleImages() {
|
|
var images = document.querySelectorAll('p>img')
|
|
|
|
console.log(images);
|
|
for (var i = 0; i < images.length; ++i) {
|
|
handleImage(images[i])
|
|
}
|
|
}
|
|
|
|
function handleImage(img) {
|
|
img.parentElement.style.textAlign = 'center'
|
|
|
|
var modal = document.createElement('div')
|
|
modal.id = 'modal'
|
|
|
|
var modalContent = document.createElement('div')
|
|
modal.appendChild(modalContent)
|
|
|
|
var modalImage = document.createElement('img')
|
|
modalContent.appendChild(modalImage)
|
|
|
|
var span = document.createElement('span')
|
|
span.classList.add('close')
|
|
span.textContent = 'x'
|
|
modal.appendChild(span)
|
|
|
|
img.onclick = function () {
|
|
console.log('clicked')
|
|
document.body.appendChild(modal)
|
|
modalImage.src = img.src
|
|
}
|
|
|
|
span.onclick = function () {
|
|
document.body.removeChild(modal)
|
|
}
|
|
}
|
|
|
|
handleImages()
|
|
</script>
|
|
</body>
|
|
</html> |