File size: 29,653 Bytes
4c7b631 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 |
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1"><link rel="shortcut icon" type="image/x-icon" href="/narsil.github.io/favicon.ico"><!-- Begin Jekyll SEO tag v2.6.1 -->
<title>Model based encodings | Narsil</title>
<meta name="generator" content="Jekyll v3.8.5" />
<meta property="og:title" content="Model based encodings" />
<meta name="author" content="nicolas" />
<meta property="og:locale" content="en_US" />
<meta name="description" content="How to use BPE without this hardcoded algorithm" />
<meta property="og:description" content="How to use BPE without this hardcoded algorithm" />
<link rel="canonical" href="http://localhost:4000/narsil.github.io/ml/nlp/2019/05/16/model-based-bpe-encodings.html" />
<meta property="og:url" content="http://localhost:4000/narsil.github.io/ml/nlp/2019/05/16/model-based-bpe-encodings.html" />
<meta property="og:site_name" content="Narsil" />
<meta property="og:type" content="article" />
<meta property="article:published_time" content="2019-05-16T00:00:00+02:00" />
<script type="application/ld+json">
{"description":"How to use BPE without this hardcoded algorithm","author":{"@type":"Person","name":"nicolas"},"mainEntityOfPage":{"@type":"WebPage","@id":"http://localhost:4000/narsil.github.io/ml/nlp/2019/05/16/model-based-bpe-encodings.html"},"@type":"BlogPosting","url":"http://localhost:4000/narsil.github.io/ml/nlp/2019/05/16/model-based-bpe-encodings.html","headline":"Model based encodings","dateModified":"2019-05-16T00:00:00+02:00","datePublished":"2019-05-16T00:00:00+02:00","@context":"https://schema.org"}</script>
<!-- End Jekyll SEO tag -->
<link href="https://unpkg.com/@primer/css/dist/primer.css" rel="stylesheet" />
<link rel="stylesheet" href="/narsil.github.io/assets/main.css">
<link rel="stylesheet" href="//use.fontawesome.com/releases/v5.0.7/css/all.css"><link type="application/atom+xml" rel="alternate" href="http://localhost:4000/narsil.github.io/feed.xml" title="Narsil" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" crossorigin="anonymous">
<script type="text/javascript" async src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"> </script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.js" integrity="sha384-y23I5Q6l+B6vatafAwxRu/0oK/79VlbSz7Q9aiSZUvyWYIYsd+qj+o24G5ZU2zJz" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/contrib/auto-render.min.js" integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"></script>
<script>
document.addEventListener("DOMContentLoaded", function() {
renderMathInElement( document.body, {
delimiters: [
{left: "$$", right: "$$", display: true},
{left: "[%", right: "%]", display: true},
{left: "$", right: "$", display: false}
]}
);
});
</script>
<script>
function wrap_img(fn) {
if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading") {
var elements = document.querySelectorAll(".post img");
Array.prototype.forEach.call(elements, function(el, i) {
if (el.getAttribute("title")) {
const caption = document.createElement('figcaption');
var node = document.createTextNode(el.getAttribute("title"));
caption.appendChild(node);
const wrapper = document.createElement('figure');
wrapper.className = 'image';
el.parentNode.insertBefore(wrapper, el);
el.parentNode.removeChild(el);
wrapper.appendChild(el);
wrapper.appendChild(caption);
}
});
} else { document.addEventListener('DOMContentLoaded', fn); }
}
window.onload = wrap_img;
</script>
<script>
document.addEventListener("DOMContentLoaded", function(){
// add link icon to anchor tags
var elem = document.querySelectorAll(".anchor-link")
elem.forEach(e => (e.innerHTML = '<i class="fas fa-link fa-xs"></i>'));
// remove paragraph tags in rendered toc (happens from notebooks)
var toctags = document.querySelectorAll(".toc-entry")
toctags.forEach(e => (e.firstElementChild.innerText = e.firstElementChild.innerText.replace('¶', '')))
});
</script>
</head><body><header class="site-header" role="banner">
<div class="wrapper"><a class="site-title" rel="author" href="/narsil.github.io/">Narsil</a><nav class="site-nav">
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger">
<span class="menu-icon">
<svg viewBox="0 0 18 15" width="18px" height="15px">
<path d="M18,1.484c0,0.82-0.665,1.484-1.484,1.484H1.484C0.665,2.969,0,2.304,0,1.484l0,0C0,0.665,0.665,0,1.484,0 h15.032C17.335,0,18,0.665,18,1.484L18,1.484z M18,7.516C18,8.335,17.335,9,16.516,9H1.484C0.665,9,0,8.335,0,7.516l0,0 c0-0.82,0.665-1.484,1.484-1.484h15.032C17.335,6.031,18,6.696,18,7.516L18,7.516z M18,13.516C18,14.335,17.335,15,16.516,15H1.484 C0.665,15,0,14.335,0,13.516l0,0c0-0.82,0.665-1.483,1.484-1.483h15.032C17.335,12.031,18,12.695,18,13.516L18,13.516z"/>
</svg>
</span>
</label>
<div class="trigger"><a class="page-link" href="/narsil.github.io/about/">About Me</a><a class="page-link" href="/narsil.github.io/search/">Search</a><a class="page-link" href="/narsil.github.io/categories/">Tags</a></div>
</nav></div>
</header>
<main class="page-content" aria-label="Content">
<div class="wrapper">
<article class="post h-entry" itemscope itemtype="http://schema.org/BlogPosting">
<header class="post-header">
<h1 class="post-title p-name" itemprop="name headline">Model based encodings</h1><p class="page-description">How to use BPE without this hardcoded algorithm</p><p class="post-meta post-meta-title"><time class="dt-published" datetime="2019-05-16T00:00:00+02:00" itemprop="datePublished">
May 16, 2019
</time>•
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
<span class="p-author h-card" itemprop="name">nicolas</span></span>
• <span class="read-time" title="Estimated read time">
11 min read
</span></p>
<p class="category-tags"><i class="fas fa-tags category-tags-icon"></i></i>
<a class="category-tags-link" href="/narsil.github.io/categories/#ml">ml</a>
<a class="category-tags-link" href="/narsil.github.io/categories/#nlp">nlp</a>
</p>
</header>
<div class="post-content e-content" itemprop="articleBody">
<ul class="section-nav">
<li class="toc-entry toc-h1"><a href="#tldr-in-this-article-we-present-an-idea-to-generate-byte-pair-encodings-not-based-on-frequency-in-the-dataset-but-on-the-quality-of-the-prediction-of-our-model-this-enables-us-to-predict-multi-word-tokens-like-new-york-and-address-languages-that-dont-use-spaces-to-split-words">TL;DR In this article we present an idea to generate Byte pair encodings, not based on frequency in the dataset, but on the quality of the prediction of our model. This enables us to predict multi word tokens like “New York” and address languages that don’t use spaces to split words.</a>
<ul>
<li class="toc-entry toc-h2"><a href="#what-are-byte-pair-encodings-">What are Byte Pair Encodings ?</a></li>
<li class="toc-entry toc-h2"><a href="#whats-the-problem-with-bpe-">What’s the problem with BPE ?</a></li>
<li class="toc-entry toc-h2"><a href="#ε-bpe-or-model-based-bpe-encoding">ε-BPE or model based BPE encoding</a></li>
<li class="toc-entry toc-h2"><a href="#results">Results</a></li>
<li class="toc-entry toc-h2"><a href="#future-work">Future Work</a></li>
</ul>
</li>
</ul><p><a href="https://en.wikipedia.org/wiki/Byte_pair_encoding">Byte-pair encodings</a> (BPE) are now very commonly used in NLP. In <a href="https://openai.com/blog/better-language-models/">GPT-2</a>, Byte-pair encodings are used to preformat the raw texts before feeding the model. But this is a relatively costly step for your preprocessing and has some limitations. For instance, you have to split your data on spaces if you want your byte pair algorithm to compute in reasonable time.</p>
<blockquote>
<h1 id="tldr-in-this-article-we-present-an-idea-to-generate-byte-pair-encodings-not-based-on-frequency-in-the-dataset-but-on-the-quality-of-the-prediction-of-our-model-this-enables-us-to-predict-multi-word-tokens-like-new-york-and-address-languages-that-dont-use-spaces-to-split-words">
<a class="anchor" href="#tldr-in-this-article-we-present-an-idea-to-generate-byte-pair-encodings-not-based-on-frequency-in-the-dataset-but-on-the-quality-of-the-prediction-of-our-model-this-enables-us-to-predict-multi-word-tokens-like-new-york-and-address-languages-that-dont-use-spaces-to-split-words" aria-hidden="true"><span class="octicon octicon-link"></span></a>TL;DR In this article we present an idea to generate Byte pair encodings, not based on frequency in the dataset, but on the quality of the prediction of our model. This enables us to predict multi word tokens like “New York” and address languages that don’t use spaces to split words.</h1>
</blockquote>
<h2 id="what-are-byte-pair-encodings-">
<a class="anchor" href="#what-are-byte-pair-encodings-" aria-hidden="true"><span class="octicon octicon-link"></span></a>What are Byte Pair Encodings ?</h2>
<p>Byte-pair encodings are a way to compress information from pairs of bytes that will form tokens. Let’s take an example :</p>
<p>“I love carrots and I love apples.”</p>
<p>This sentence read by a computer is only a sequence of bytes (bytes are simply a number between 0 and 255). That means to a computer our sentence looks like</p>
<p>“I love carrots and I love apples.” -> [73, 32, 108, 111, 118, 101, 32, 99, 97, 114, 114, 111, 116, 115, 32, 97, 110, 100, 32, 73, 32, 108, 111, 118, 101, 32, 97, 112, 112, 108, 101, 115, 46]</p>
<p>From that example, you may remark that some bytes are occurring multiple times together like [108, 111] that occurs twice (it’s “lo” from “love”). So let’s build a new token for this frequent pair. Numbers from 0 to 255 are already taken so we’ll take the next available number which is 256, and we are going to store that information in a table</p>
<p>[108, 111] -> 256</p>
<p>Now if we use that new token to encode our original bytes, whenever we encounter [108, 111], we’ll replace that by 256, so the original byte string becomes :</p>
<p>[73, 32, 108, <strong>256</strong>, 101, 32, 99, 97, 114, 114, 111, 116, 115, 32, 97, 110, 100, 32, 73, 32, <strong>256</strong>, 118, 101, 32, 97, 112, 112, 108, 101, 115, 46]</p>
<p>We went from 33 numbers to 31 numbers. We can rinse and repeat to compress the number of numbers even further. Originally, BPE was proposed as a compression algorithm. It’s not the best compression tool, so we won’t look at that side of the algorithm. Now you get what we are looking at when we train a model on BPEs, just a list of numbers.</p>
<p>Typically a BPE vocabulary contains ~10k tokens (GPT-2 has 50k), that means it can capture very frequent words like “the” entirely, and parts of words that contain many variations like “ment” (<strong>ment</strong>ally, environ<strong>ment</strong> …). What’s great about it it that you can now have words share semantic parts of them for their representation in your model so (environ-ment, environ-ment-al, environ-ment-ally will all share “environ” which will contain most of the semantic meaning, the rest will contain grammar information hopefully).</p>
<p>The real advantage of BPE over classical Word Embeddings is that it does not fall into the out-of-vocabulary error (when a word was not seen). At worse you can always fall back to single bytes.</p>
<h2 id="whats-the-problem-with-bpe-">
<a class="anchor" href="#whats-the-problem-with-bpe-" aria-hidden="true"><span class="octicon octicon-link"></span></a><strong>What’s the problem with BPE ?</strong>
</h2>
<p>BPE algorithm is pretty bad in terms of complexity to calculate (roughly O(n²), you can look at a very good implementation <a href="https://github.com/glample/fastBPE">https://github.com/glample/fastBPE</a>). BPE is also pretty bad when you want to encode some new text. A greedy algorithm will be O(n) but not the best encoding possible, the best encoding possible is actually O(n²) in the general case.</p>
<p>To be honest, most implementations split on spaces as mentioned earlier which speeds up the algorithm quite a bit. Once we have encoded a full word like “the” there is no way to add tokens to it, so it’s not necessary to look at it anymore for potential byte pairs, so we can assume the encoding&table creation go from O(n²) to something much closer to O(n). In addition, at encoding time, once we know the encoding for “the” we can cache that information leading to further speed ups. But using spaces as a special character has drawbacks, namely:</p>
<ul>
<li>
<p>We can’t address as well languages that don’t use a space to separate words like Chinese (arguably German).</p>
</li>
<li>
<p>We can’t encode frequently occurring multi words like “New York” or “European Union” or “black holes”</p>
</li>
</ul>
<p>The second problem is especially bad when you consider examples where semantic is very different from the composing words like “Chicago Bulls” have nothing to do with bulls.</p>
<h2 id="ε-bpe-or-model-based-bpe-encoding">
<a class="anchor" href="#%CE%B5-bpe-or-model-based-bpe-encoding" aria-hidden="true"><span class="octicon octicon-link"></span></a><strong>ε-BPE or model based BPE encoding</strong>
</h2>
<p>The core idea is that instead of using frequency in the dataset to create the byte pairs, we can use the probability transition of the model to create the BPE. Let’s use some kind of transformer, GPT-2 for instance. The core idea of that model, is to predict the next token (in the BPE sense) given a fixed context size. But we can use the output probability of the model in order to create new tokens, not because they are frequent but because they are easy to predict. For instance in a book that contains a character “Sir Francis” that appears rarely, but there is only one character named “Sir …”, the algorithm might learn quite easily that “Sir “ is followed by “Francis” with great confidence, even if the occurence of the words is pretty low compared to common words like “the”, “like” and “I”.</p>
<p>So the core algorithm, will train a simple transformer on a dataset on regular bytes (at least at the start). Then, as the algorithm learns, some predictions will be above 1-ε. We can keep track of those and keep track of the last token we received, to check if we were correct.</p>
<p>Let’s keep a hit map to see how successful our algorithm is. For instance, I predicted “Fo” will be followed by “gg” (Phileas Fogg is a character in Around the world in 80 days) with probability > 1-ε. I was correct in 14 cases, and got it wrong in 1 case (let’s say it was classical “Fo” “g “). We were correct 14/15 times that’s 93% accuracy. If we look at the fluctuation interval associated with that, we get [92.74-93.25%] range. If 92.74 > 1–ε we can conclude that our transition prediction is really very good, it’s not a fluke of the model.</p>
<p>More generally, if we want 95% confidence when we upgrade this transition, we need to respect the following inequality : k / n - 1/sqrt(n) > 1-ε, where k is the number of successful predictions, n is the total number of predictions and ε the probability margin explained earlier.</p>
<p>This model is slightly different from byte pair encoding, but now we don’t suffer from the 2 problems mentioned above, we can get pretty long tokens if the dataset allows for it, and we can use Chinese or German as the space character does not play any special role.</p>
<h2 id="results">
<a class="anchor" href="#results" aria-hidden="true"><span class="octicon octicon-link"></span></a><strong>Results</strong>
</h2>
<p>Implementation can be found here. On the first run, we ran on a book <a href="https://en.wikipedia.org/wiki/Around_the_World_in_Eighty_Days">Around the world in 80 days</a> by Jules Verne. It’s a very small dataset but the idea is to check that we can actually overcome BPE’s limitations. Here are a few telling tokens that were created while running on the dataset :</p>
<table>
<thead>
<tr>
<th>Promotion #</th>
<th>Token created</th>
</tr>
</thead>
<tbody>
<tr>
<td>338</td>
<td>“Mr. Fogg”</td>
</tr>
<tr>
<td>357</td>
<td>“Phileas Fogg”</td>
</tr>
<tr>
<td>360</td>
<td>“Passepartout”</td>
</tr>
<tr>
<td>635</td>
<td>“ir Franc” (Sir Francis)</td>
</tr>
<tr>
<td>781</td>
<td>“It was”</td>
</tr>
<tr>
<td>900</td>
<td>’” asked’ (contains a quote character)</td>
</tr>
</tbody>
</table>
<p>What is interesting, it that:</p>
<ul>
<li>
<p>We managed to create multi word tokens like “Phileas Fogg”</p>
</li>
<li>
<p>Multi word tokens are a minority in terms of tokens created by the algorithm. Out of 421 tokens that contain a space character only 27 are multi word tokens like “New York”. The remaining 394 tokens contain an ending space, meaning our algorithm is learning word boundaries. It is reassuring because traditional BPE are usually hardcoding that information.</p>
</li>
<li>
<p>Multi word tokens are name of characters in the book, which are occurring frequently, they are an entity by themselves (Fogg even has 2 tokens associated to him)</p>
</li>
<li>
<p>2 Multi word tokens are <strong>not</strong> specific to the book, “it was” is a pretty common 2 word token in English in descriptions, “(…) asked” is a very common continuation when we start a quote and end a sentence with a question mark. We can guess that “(…) said” would be a token further down the line, but it’s harder as there are probably a wider variety of verbs that can fit (said, replied, answered and so on…)</p>
</li>
</ul>
<p>Here is a more complete comparison of standard BPE with ε-BPE, with the first 100 tokens generated, as you can see more tokens are dedicated to syntax in eBPE, which Standard BPE ignore gladly by splitting on newlines and spaces.</p>
<table>
<thead>
<tr>
<th>Standard BPE</th>
<th>eBPE</th>
</tr>
</thead>
<tbody>
<tr>
<td>‘th’</td>
<td>‘\r\n’</td>
</tr>
<tr>
<td>‘the ‘</td>
<td>’, ‘</td>
</tr>
<tr>
<td>‘an’</td>
<td>‘d ‘</td>
</tr>
<tr>
<td>‘in’</td>
<td>‘Th’</td>
</tr>
<tr>
<td>‘ou’</td>
<td>‘ve’</td>
</tr>
<tr>
<td>‘er’</td>
<td>‘y ‘</td>
</tr>
<tr>
<td>‘ed ‘</td>
<td>’; ‘</td>
</tr>
<tr>
<td>‘ar’</td>
<td>‘f ‘</td>
</tr>
<tr>
<td>‘hi’</td>
<td>’,\r\n’</td>
</tr>
<tr>
<td>‘on’</td>
<td>‘\r\n\r\n’</td>
</tr>
<tr>
<td>‘re’</td>
<td>‘th’</td>
</tr>
<tr>
<td>‘en’</td>
<td>‘qu’</td>
</tr>
<tr>
<td>‘and ‘</td>
<td>‘the’</td>
</tr>
<tr>
<td>‘of ‘</td>
<td>’ ‘</td>
</tr>
<tr>
<td>‘st’</td>
<td>‘the ‘</td>
</tr>
<tr>
<td>‘to ‘</td>
<td>‘The’</td>
</tr>
<tr>
<td>‘as ‘</td>
<td>‘\r\n’</td>
</tr>
<tr>
<td>‘se’</td>
<td>’, ‘</td>
</tr>
<tr>
<td>‘ha’</td>
<td>‘y ‘</td>
</tr>
<tr>
<td>‘or’</td>
<td>‘d ‘</td>
</tr>
<tr>
<td>’.\r ‘</td>
<td>‘Th’</td>
</tr>
<tr>
<td>‘it’</td>
<td>‘ve’</td>
</tr>
<tr>
<td>‘he ‘</td>
<td>’; ‘</td>
</tr>
<tr>
<td>‘le’</td>
<td>‘f ‘</td>
</tr>
<tr>
<td>‘ing ‘</td>
<td>’,\r\n’</td>
</tr>
<tr>
<td>’,\r ‘</td>
<td>’ ‘</td>
</tr>
<tr>
<td>‘as’</td>
<td>‘\r\n’</td>
</tr>
<tr>
<td>‘in ‘</td>
<td>’, ‘</td>
</tr>
<tr>
<td>‘at’</td>
<td>‘d ‘</td>
</tr>
<tr>
<td>‘at ‘</td>
<td>‘y ‘</td>
</tr>
<tr>
<td>‘ro’</td>
<td>‘Th’</td>
</tr>
<tr>
<td>‘er ‘</td>
<td>‘ve’</td>
</tr>
<tr>
<td>‘al’</td>
<td>‘f ‘</td>
</tr>
<tr>
<td>‘es’</td>
<td>’; ‘</td>
</tr>
<tr>
<td>‘on ‘</td>
<td>’ ‘</td>
</tr>
<tr>
<td>‘was ‘</td>
<td>’,\r\n’</td>
</tr>
<tr>
<td>‘no’</td>
<td>‘th’</td>
</tr>
<tr>
<td>‘his ‘</td>
<td>‘\r\n’</td>
</tr>
<tr>
<td>‘ed’</td>
<td>’, ‘</td>
</tr>
<tr>
<td>‘ac’</td>
<td>‘d ‘</td>
</tr>
<tr>
<td>’“\r ‘</td>
<td>‘y ‘</td>
</tr>
<tr>
<td>‘ri’</td>
<td>‘Th’</td>
</tr>
<tr>
<td>‘be’</td>
<td>‘ve’</td>
</tr>
<tr>
<td>‘ly ‘</td>
<td>‘f ‘</td>
</tr>
<tr>
<td>‘om’</td>
<td>’; ‘</td>
</tr>
<tr>
<td>‘li’</td>
<td>’ ‘</td>
</tr>
<tr>
<td>‘en ‘</td>
<td>’,\r\n’</td>
</tr>
<tr>
<td>‘ti’</td>
<td>‘th’</td>
</tr>
<tr>
<td>‘og’</td>
<td>‘\r\n\r\n’</td>
</tr>
<tr>
<td>‘ra’</td>
<td>‘the’</td>
</tr>
<tr>
<td>‘di’</td>
<td>‘the ‘</td>
</tr>
<tr>
<td>‘art’</td>
<td>‘The’</td>
</tr>
<tr>
<td>‘Fog’</td>
<td>‘qu’</td>
</tr>
<tr>
<td>‘the’</td>
<td>’s ‘</td>
</tr>
<tr>
<td>‘ma’</td>
<td>‘The ‘</td>
</tr>
<tr>
<td>‘ve ‘</td>
<td>‘g ‘</td>
</tr>
<tr>
<td>‘is ‘</td>
<td>’,”’</td>
</tr>
<tr>
<td>‘or ‘</td>
<td>‘no’</td>
</tr>
<tr>
<td>‘ld ‘</td>
<td>‘t ‘</td>
</tr>
<tr>
<td>‘whi’</td>
<td>‘th ‘</td>
</tr>
<tr>
<td>‘il’</td>
<td>‘o ‘</td>
</tr>
<tr>
<td>‘ur’</td>
<td>’?”’</td>
</tr>
<tr>
<td>’s, ‘</td>
<td>‘\r\n\r\n”’</td>
</tr>
<tr>
<td>‘de’</td>
<td>’,” ‘</td>
</tr>
<tr>
<td>‘wh’</td>
<td>‘Mr’</td>
</tr>
<tr>
<td>‘lo’</td>
<td>‘e ‘</td>
</tr>
<tr>
<td>‘ch ‘</td>
<td>‘yo’</td>
</tr>
<tr>
<td>‘ere ‘</td>
<td>‘Yo’</td>
</tr>
<tr>
<td>‘ith ‘</td>
<td>‘ou’</td>
</tr>
<tr>
<td>‘The ‘</td>
<td>’. ‘</td>
</tr>
<tr>
<td>‘am’</td>
<td>‘nd ‘</td>
</tr>
<tr>
<td>‘ent’</td>
<td>‘h ‘</td>
</tr>
<tr>
<td>‘un’</td>
<td>‘n ‘</td>
</tr>
<tr>
<td>‘gh’</td>
<td>’;\r\n’</td>
</tr>
<tr>
<td>‘with ‘</td>
<td>‘og’</td>
</tr>
<tr>
<td>‘an ‘</td>
<td>‘you’</td>
</tr>
<tr>
<td>‘oun’</td>
<td>‘r ‘</td>
</tr>
<tr>
<td>‘part’</td>
<td>‘of ‘</td>
</tr>
<tr>
<td>‘ver’</td>
<td>‘to ‘</td>
</tr>
<tr>
<td>‘si’</td>
<td>’s F’</td>
</tr>
<tr>
<td>‘had ‘</td>
<td>‘Pa’</td>
</tr>
<tr>
<td>‘not ‘</td>
<td>‘as ‘</td>
</tr>
<tr>
<td>‘ould ‘</td>
<td>'’s ‘</td>
</tr>
<tr>
<td>‘ing’</td>
<td>’. F’</td>
</tr>
<tr>
<td>‘out ‘</td>
<td>‘is ‘</td>
</tr>
<tr>
<td>‘el’</td>
<td>‘ld ‘</td>
</tr>
<tr>
<td>‘sa’</td>
<td>‘ng ‘</td>
</tr>
<tr>
<td>‘ce’</td>
<td>‘at ‘</td>
</tr>
<tr>
<td>‘that ‘</td>
<td>‘re’</td>
</tr>
<tr>
<td>‘asse’</td>
<td>‘ve ‘</td>
</tr>
<tr>
<td>‘fi’</td>
<td>‘gh’</td>
</tr>
<tr>
<td>‘ol’</td>
<td>‘ut ‘</td>
</tr>
<tr>
<td>‘sh’</td>
<td>‘ll’</td>
</tr>
<tr>
<td>‘r. ‘</td>
<td>‘Pas’</td>
</tr>
<tr>
<td>’.”\r ‘</td>
<td>‘re ‘</td>
</tr>
<tr>
<td>‘Passe’</td>
<td>‘ed ‘</td>
</tr>
<tr>
<td>‘Passepart’</td>
<td>’. Fog’</td>
</tr>
<tr>
<td>‘ut ‘</td>
<td>‘ch ‘</td>
</tr>
<tr>
<td>‘which ‘</td>
<td>‘and ‘</td>
</tr>
<tr>
<td>‘ay’</td>
<td>‘ea’</td>
</tr>
</tbody>
</table>
<p>I would love to check the tokenization of German or Chinese but I’m not a speaker of either language so it’s hard for me to analyze the results anyway. What’s for sure is that the technique is applicable.</p>
<p>I also tried the technique on different types of files like wav files or mp3 files, even jpeg images. Analysis is harder to do. Still some interesting notes, it took longer for the model to emit new tokens on the mp3 files than on the wav files. The mp3 file is encoded, therefore should have a lower entropy (meaning it’s harder to predict the next token) than the wav files so the model takes longer to actually get good at predicting. It’s probable (I haven’t checked) that we have to overfit the mp3 file and jpeg files before we can predict any meaningful content (except maybe the header part)</p>
<h2 id="future-work">
<a class="anchor" href="#future-work" aria-hidden="true"><span class="octicon octicon-link"></span></a><strong>Future Work</strong>
</h2>
<p>Many interesting ideas are still left to explore to continue exploring the idea of models creating their own tokenization. For now a limiting factor is the actual BPE encoding process that takes longer and longer as the model creates new tokens. That’s because the encoding process is done in Python, so it’s quite slow and can’t be precalculated as you would do with fixed BPE encodings. To give a sense of the slowdown, the training loop starts at ~11it/s on a GTX970 and finished at roughly 10s/it. That’s a 100x slowdown over the course of the training, with only 1k tokens in the end, far from the 50k used by GPT-2 for instance.</p>
<p>It’s going to be an actual requirement to train on larger and more representative datasets. Training on bigger datasets would help us understand how important are those multi word tokens and maybe what are those multi words. The token “(…) <strong>asked</strong>” was pretty surprising to me, I’m eager to see what else can be discovered.</p>
<p>The actual epsilon used was 40% which actually quite a big (value was chosen with trial and error, to get a small but not null rejection rate of new tokens, to add tokens as fast as possible but not making too many mistakes). That value probably has a sweet spot depending on the number of current tokens, after speeding up the process it would be interesting to look at the best value for epsilon as a function of the number of tokens.</p>
</div><a class="u-url" href="/narsil.github.io/ml/nlp/2019/05/16/model-based-bpe-encodings.html" hidden></a>
</article>
</div>
</main><footer class="site-footer h-card">
<data class="u-url" href="/narsil.github.io/"></data>
<div class="wrapper">
<h2 class="footer-heading">Narsil</h2>
<div class="footer-col-wrapper">
<div class="footer-col footer-col-1">
<ul class="contact-list">
<li class="p-name">Narsil</li></ul>
</div>
<div class="footer-col footer-col-2"><ul class="social-media-list">
<li><a href="https://github.com/Narsil"><svg class="social svg-icon"><use xlink:href="/narsil.github.io/assets/minima-social-icons.svg#github"></use></svg> <span class="username">Narsil</span></a></li><li><a href="https://www.twitter.com/narsilou"><svg class="social svg-icon"><use xlink:href="/narsil.github.io/assets/minima-social-icons.svg#twitter"></use></svg> <span class="username">narsilou</span></a></li></ul>
</div>
<div class="footer-col footer-col-3">
<p>Small experiements insights from ML and software development.</p>
</div>
</div>
</div>
</footer>
</body>
</html>
|