Език AI РЪКОВОДСТВО

Токенизация на WordPiece

WordPiece is a subword tokenization method that splits text into pieces from a fixed vocabulary, so rare words like johanson become known parts such as johan and ##son.

Чуйте това ръководствоAI-разказан · ~7 min

На тази страница8 min read
  1. Преглед
  2. Основни изводи
  3. Дълбоко гмуркане
  4. Tokenizing a sentence by hand with greedy longest match
  5. Внедряване в реалния свят
  6. Бъдещето на токенизацията на WordPiece
  7. Прилагайки го на практика
  8. Източници и допълнителна литература
  9. Продължете да изследвате
  10. Често задавани въпроси

Преглед

Developed at Google for Japanese and Korean voice search in 2012, it became the tokenizer behind Google's neural translation system and BERT, and it shaped how a generation of language models reads text.

Основни изводи

  1. WordPiece splits words into subword pieces from a fixed vocabulary, marking continuation pieces with ## in BERT's convention.
  2. It was introduced by Schuster and Nakajima at Google in 2012 for Japanese and Korean voice search.
  3. BERT, published in 2018, used WordPiece embeddings with a 30,000-token vocabulary.
  4. At inference WordPiece uses greedy longest-match-first, and a word with no matching piece becomes [UNK] entirely.
  5. Unlike BPE, WordPiece picks merges by a score that divides a pair's frequency by its parts' frequencies, not by raw frequency.
  6. Google's Fast WordPiece algorithm tokenizes in linear time and was reported 8.2 times faster than Hugging Face Tokenizers.

Дълбоко гмуркане

Tokenization is the first step in any language model: turning raw text into a sequence of units, called tokens, that the model has embeddings for. Splitting on spaces gives whole words, but no fixed word list can cover every name, typo, technical term and inflected form, and every unknown word collapses into a single unknown token. Splitting into characters covers everything but makes sequences long and forces the model to learn spelling from scratch. Subword tokenization sits between the two: common words stay whole, and rare words are broken into smaller pieces that are in the vocabulary. WordPiece is one of the main subword methods, alongside byte-pair encoding (BPE) and the unigram language model used in SentencePiece.

WordPiece comes from Google. Mike Schuster and Kaisuke Nakajima described it in a 2012 ICASSP paper on Japanese and Korean voice search, where text has no spaces between words and a large character set, so a speech recognizer needed a data-driven way to choose units. Google's 2016 neural machine translation paper by Yonghui Wu and colleagues adopted the same model and explained its goal: given a training corpus and a target number of tokens D, pick D wordpieces so the corpus can be segmented into as few pieces as possible, which amounts to maximizing the likelihood of the data under a simple language model. Its example sentence segments Jet makers feud over seat width as _J et _makers _fe ud _over _seat _width, where the underscore marks the start of a word and common words stay whole.

Training a WordPiece vocabulary works much like BPE, by repeatedly merging pieces. Start with every character in the corpus as a token, marking characters that do not begin a word with a prefix; in BERT's convention, word becomes w ##o ##r ##d. Count how often each adjacent pair of tokens appears. Then merge the pair with the highest score and add the result to the vocabulary, repeating until the vocabulary reaches the target size. The difference from BPE is the score. BPE merges the most frequent pair. The WordPiece description in Hugging Face's course divides the pair's frequency by the product of the frequencies of its two parts, which favors pairs whose parts rarely appear apart. The course notes that Google never open-sourced its training code, so this formula is a reconstruction from the literature.

Applying a trained vocabulary to new text is simpler. BERT's tokenizer first normalizes text, converting whitespace and, for uncased models, lowercasing and stripping accents. It then splits off punctuation. Finally it runs WordPiece on each word with greedy longest-match-first: find the longest vocabulary entry that matches the start of the word, emit it, and continue on the rest of the word using pieces with the ## prefix. If at any point no piece matches, the whole word becomes the unknown token [UNK], rather than only the unmatched letters. Only the final vocabulary is stored, not an ordered list of merges, which is another difference from BPE.

BERT made WordPiece famous. The October 2018 paper by Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova used WordPiece embeddings with a 30,000-token vocabulary, citing the 2016 translation paper, along with special tokens such as [CLS] at the start of each input, [SEP] between sentences and [MASK] for pretraining. The BERT repository's example shows the whole pipeline: John Johanson's house becomes john johan ##son ' s house. Multilingual BERT extended the idea to 104 languages with a shared vocabulary of 110,000 entries, sampling languages with exponential smoothing so that English was drawn about 100 times more often than Icelandic instead of about 1,000 times.

Compared with alternatives, WordPiece and BPE produce similar vocabularies and similar results; the 2016 translation paper reported that a 32,000-wordpiece model reached 38.95 BLEU on WMT English to French against 37.90 for a word model and 38.01 for a character model, and 24.61 against 23.12 and 22.62 on English to German. The main practical differences are the merge score, the longest-match inference and the whole-word [UNK] fallback. SentencePiece and byte-level BPE, used in many later models, work directly on raw text or bytes and so never need an unknown token, while WordPiece as used in BERT depends on a pre-tokenization step that splits on spaces and punctuation.

WordPiece has known weaknesses. Because it relies on whitespace and punctuation splitting, it needs special handling for languages without spaces; multilingual BERT puts spaces around every Chinese, Japanese Kanji and Korean Hanja character before WordPiece runs. Uncased models strip accents, which the multilingual BERT authors admit can remove meaning in some languages. A word with even one character missing from the vocabulary becomes [UNK] entirely. Subword splits also do not follow morphology: a meaningful unit like un or able may or may not survive as a piece, which the course illustrates by noting that a frequent pair can still score low. And token counts vary by language, so the same sentence can cost many more tokens in a language underrepresented in the vocabulary.

In practice, WordPiece is cheap and well supported. The original BERT checkpoints ship a vocab.txt file mapping each piece to an ID, and changing the vocabulary requires updating the model's vocab_size setting. Libraries such as Hugging Face Tokenizers and TensorFlow Text implement it, and Google's Fast WordPiece algorithm, presented at EMNLP 2021, made tokenization linear in input length. The key operational rule is to use exactly the tokenizer a model was trained with, including its casing setting, since a mismatch silently changes every token ID.

04Работен пример

Tokenizing a sentence by hand with greedy longest match

  1. Take a toy vocabulary: john, johan, ##son, ', s, house, h, ##o, ##u, ##s, ##e and [UNK].

  2. Normalize the sentence John Johanson's house to lowercase and split off punctuation to get john johanson ' s house.

  3. For john, the longest match from the start is john itself, so emit john.

  4. For johanson, the longest match is johan; continue on son with the prefix and match ##son.

  5. Emit ' and s as single pieces, then match house whole.

  6. Now try the word hoax: h matches, then ##o, but no piece starts with ##a, so the entire word becomes [UNK].

Какво показва

The sentence tokenizes to john johan ##son ' s house, matching the example in the BERT repository. The hoax case shows the whole-word [UNK] fallback, which you can confirm by running a BERT tokenizer library on the same text.

Внедряване в реалния свят

Preparing input for BERT, which the 2018 paper says uses WordPiece embeddings with a 30,000-token vocabulary, so John Johanson's house becomes john johan ##son ' s house in the uncased model.

Labeling named entities with BERT by classifying only the first sub-token of each word, the approach the BERT paper used on CoNLL-2003, and mapping labels back with a word-to-token index map.

Serving multilingual text with multilingual BERT, whose cased model covers 104 languages with one shared 110,000-entry WordPiece vocabulary and splits Chinese characters before WordPiece runs.

Speeding up tokenization in production with Google's Fast WordPiece algorithm, which a 2021 EMNLP paper reports is on average 8.2 times faster than Hugging Face Tokenizers on general text.

Choosing a vocabulary size for a translation model, guided by Google's 2016 neural machine translation paper, which found 8,000 to 32,000 wordpieces gave good BLEU scores and fast decoding.

Бъдещето на токенизацията на WordPiece

WordPiece's influence now runs mostly through the large installed base of BERT-family encoders used for search, classification and named entity recognition, while many newer generative models use byte-level BPE or SentencePiece tokenizers that avoid unknown tokens. Engineering work on WordPiece itself has focused on speed, as in Google's 2021 Fast WordPiece algorithm with linear-time tokenization. Open problems are shared with all subword schemes. Multilingual fairness is one: multilingual BERT's documentation shows how much vocabulary allocation depends on how languages are sampled, and underrepresented languages end up split into more, shorter pieces. Robustness is another, since a single unusual character can turn a whole word into [UNK]. Whether future models keep fixed subword vocabularies or move toward byte- or character-level inputs with learned grouping is an open research question rather than a settled trend.

Прилагайки го на практика

Защо има значение

  • Скорост и мащаб. Езиковите работни процеси могат да се движат по-бързо, без да се жертва последователността.
  • Достъп и обхват. Той разширява достъпа между езици и стилове на комуникация.
  • По-ясни решения. Екипите могат да отделят повече време за преценка, докато автоматизацията се справя с повторението.

Внимавайте за

  • Халюцинираните факти могат тихо да влязат в отчети, потоци за поддръжка или резултати от изследвания.
  • Бързата чувствителност може да създаде противоречиви резултати при подобни заявки.
  • Чувствителните текстови данни могат да бъдат разкрити, ако контролите за достъп са слаби.

Следващи стъпки

  1. Определете изходен формат, тон и стандарти за качество преди внедряване.
  2. Наземни отговори с доверени източници винаги, когато точността има значение.
  3. Поддържайте контролна точка за човешки преглед за изходи с високи залози.
  4. Проследявайте моделите на неуспехи и редовно обучавайте подкани или работни потоци.

Източници и допълнителна литература

  1. Google Research (ICASSP 2012)Japanese and Korean Voice Search
  2. arXivGoogle's Neural Machine Translation System: Bridging the Gap between Human and Machine Translation
  3. arXiv (NAACL 2019)BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
  4. GitHub (Google Research)google-research/bert
  5. GitHub (Google Research)Multilingual BERT
  6. Hugging Face LLM CourseWordPiece tokenization
  7. arXiv (EMNLP 2021)Fast WordPiece Tokenization

Продължете да изследвате

Безплатен бюлетин

Получавайте дневния AI бюлетин

Три проверени AI истории всяка делнична сутрин, написани на достъпен език. Безплатно завинаги, без реклами.

Един имейл всеки делничен ден. Отписване с едно кликване. Никога не продаваме и не споделяме вашия имейл адрес.

Тествайте себе си

Take the WordPiece Tokenization quiz

Незабавна обратна връзка за всеки отговор и сертификат за споделяне с подлежащ на проверка идентификационен номер, след като преминете курс.

Стартирай теста

Подкрепете безплатното AI образование. AI Understanding е 501(c)(3) с нестопанска цел — без реклами, без платен достъп, никога. Направете дарение

Често задавани въпроси

What is WordPiece Tokenization?

WordPiece is a subword tokenization method that splits text into pieces from a fixed vocabulary, so rare words like johanson become known parts such as johan and ##son. Developed at Google for Japanese and Korean voice search in 2012, it became the tokenizer behind Google's neural translation system and BERT, and it shaped how a generation of language models reads text.

Какво показва префиксът '##' в изхода на WordPiece?

WordPiece маркира продълженията на поддумата с '##', така че 'playing' става 'play' + '##ing', позволявайки на декодера да реконструира границите на думите.

Приблизително колко голям е речникът на WordPiece, използван от оригиналния BERT?

BERT използва речник на WordPiece от приблизително 30 000 единици поддуми, като балансира покритието спрямо размера на таблицата за вграждане.

По какво се различава критерият за сливане на WordPiece от стандартното кодиране на двойки байтове?

BPE обединява най-често срещаната съседна двойка, докато WordPiece обединява двойката, която най-много увеличава вероятността за корпуса, предпочитайки двойки, чиято обща честота надвишава произведението на техните части.

What does a BERT-style WordPiece tokenizer return if it cannot segment a word into vocabulary pieces?

WordPiece tokenizers use known vocabulary pieces and can return an unknown-token marker if no valid segmentation is found. Character fallback is not guaranteed for every tokenizer configuration.

По време на извод как WordPiece избира как да раздели дума?

WordPiece токенизира алчно, съвпадайки с най-дългия запис в речника, започвайки от текущата позиция, след което се повтаря в остатъка.

Продължавай да учиш

Свързани ръководства

Още ръководства, избрани за тази тема

Получете безплатен AI сертификатКратък безплатен курс и изпит. Без разходи, без карта. Около 2,5 часа.Започнете безплатно →