概述
它于2012年在Google为日语和韩语语音搜索开发,后来成为Google神经翻译系统和BERT背后的分词器,并塑造了一代语言模型读取文本的方式。
主要要点
- WordPiece将单词拆分为固定词汇表中的子词片段,并按照BERT的惯例用##标记后续片段。
- 它由Schuster和Nakajima于2012年在Google为日语和韩语语音搜索引入。
- 2018年发布的BERT使用了具有30,000个词汇量的WordPiece嵌入。
- 在推理时,WordPiece使用贪婪最长匹配优先原则,没有匹配片段的单词会完全变成[UNK]。
- 与BPE不同,WordPiece通过一个分数来选择合并,该分数将一对片段的频率除以其各部分频率的乘积,而不是仅根据原始频率。
- Google的快速WordPiece算法以线性时间进行分词,据报道比Hugging Face分词器快8.2倍。
深入探讨
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工作示例
手动使用贪婪最长匹配对句子进行分词
以一个玩具词表为例:john、johan、##son、'、s、house、h、##o、##u、##s、##e 和 [UNK]。
将句子 John Johanson's house 规范化为小写并切分出标点符号,得到 john johanson ' s house。
对于 john,从开头开始的最长匹配是 john 本身,因此输出 john。
对于 johanson,最长匹配是 johan;带上带有前缀的 son 继续匹配,得到 ##son。
将 ' 和 s 作为单个片段输出,然后完整匹配 house。
现在尝试单词 hoax:h 匹配,接着是 ##o,但没有以 ##a 开头的片段,因此整个单词变成了 [UNK]。
它显示了什么
该句子被标记为 john johan ##son ' s house,匹配 BERT 仓库中的示例。该骗局案例展示了整词 [UNK] 回退,您可以通过在相同文本上运行 BERT 分词器库来确认。
现实世界的实施
为BERT准备输入,2018年论文指出其使用WordPiece嵌入,词表包含30,000个标记,因此John Johanson的房子在不区分大小写的模型中变成john johan ##son ' s house。
使用BERT对命名实体进行标注,仅对每个单词的第一个子词进行分类,这是BERT论文在CoNLL-2003上采用的方法,并通过单词到标记的索引映射将标签映射回。
使用多语言BERT提供多语言文本服务,其区分大小写的模型覆盖104种语言,使用一个共享的110,000条目WordPiece词表,并在WordPiece运行前拆分中文字符。
在生产环境中加速标记化,使用Google的Fast WordPiece算法,2021年EMNLP论文报告其平均比Hugging Face标记器快8.2倍。
为翻译模型选择词表大小时,参考Google的2016年神经机器翻译论文,该论文发现8,000至32,000个词片能获得良好的BLEU分数并实现快速解码。
WordPiece 代币化的未来
WordPiece的影响现在主要通过大量已安装的BERT系列编码器传播,这些编码器用于搜索、分类和命名实体识别,而许多更新的生成模型使用字节级BPE或SentencePiece标记器,避免未知标记。对WordPiece本身的工程工作侧重于速度,例如Google在2021年推出的Fast WordPiece算法实现线性时间标记化。开放问题与所有子词方案共享。多语言公平性是其中之一:多语言BERT的文档展示了词表分配如何取决于语言采样,欠代表语言最终被拆分成更多、更短的片段。鲁棒性是另一点,因为单个不寻常字符可能将整个单词变为[UNK]。未来模型是否保持固定子词词表或转向字节或字符级输入并学习分组仍是一个开放的研究问题,而非已定趋势。
付诸实践
为什么这很重要
- 速度与规模. 语言工作流程可以在不牺牲一致性的情况下更快地移动。
- 交通与覆盖范围. 它扩展了跨语言和沟通方式的访问。
- 更清晰的判决. 团队可以花更多时间进行判断,而自动化则可以处理重复。
留意
- 幻觉的事实可以悄悄地进入报告、支持流程或研究成果。
- 及时的敏感性可能会在类似的请求中产生不一致的结果。
- 如果访问控制薄弱,敏感文本数据可能会暴露。
后续步骤
- 在推出之前定义输出格式、语气和质量标准。
- 当准确性很重要时,请使用可信来源进行地面响应。
- 为高风险输出保留人工审查检查点。
- 跟踪故障模式并定期重新训练提示或工作流程。
资料来源与延伸阅读
- Google Research (ICASSP 2012)日语和韩语语音搜索
- arXivGoogle 的神经机器翻译系统:弥合人类与机器翻译之间的差距
- arXiv (NAACL 2019)BERT:深度双向Transformer的预训练用于语言理解
- GitHub (Google Research)google-research/bert
- GitHub (Google Research)多语言BERT
- Hugging Face LLM CourseWordPiece 标记化
- arXiv (EMNLP 2021)Fast WordPiece 标记化
不断探索
常见问题
什么是 WordPiece 标记化?
WordPiece 是一种子词标记化方法,它将文本拆分为来自固定词表的片段,因此像 johanson 这样的罕见词会被拆分为已知部分,如 johan 和 ##son。它在 2012 年由 Google 为日语和韩语语音搜索开发,随后成为 Google 神经翻译系统和 BERT 的标记器,并塑造了一代语言模型读取文本的方式。
WordPiece 输出中的“##”前缀表示什么?
WordPiece 用“##”标记子字延续,因此“playing”变成“play”+“##ing”,让解码器重建字边界。
原始 BERT 使用的 WordPiece 词汇量大约有多大?
BERT 使用大约 30,000 个子词单元的 WordPiece 词汇表,平衡覆盖范围与嵌入表大小。
WordPiece 的合并标准与标准字节对编码有何不同?
BPE 合并最频繁的相邻对,而 WordPiece 合并最能增加语料库可能性的对,优先考虑联合频率超过其部分乘积的对。
如果 BERT 风格的 WordPiece 标记器无法将单词拆分为词表片段,它会返回什么?
WordPiece 标记器使用已知词表片段,并在找不到有效拆分时返回未知标记。字符回退并非对所有标记器配置都保证。
在推理时,WordPiece如何选择如何分割单词?
WordPiece 贪婪地进行标记,从当前位置开始匹配最长的词汇条目,然后重复其余部分。