Elaborating BPE's Role
I'm now outlining a plan to clarify BPE's function within tokenization. This includes pinpointing where BPE slots into the process: before, after, or interleaved with vocabulary building. The goal is to provide a concise and precise explanation.
Developing the BPE Walkthrough
I'm now focusing on a plan for the walkthrough. I'm structuring it logically, ensuring each step of BPE, from vocabulary construction to tokenization, is clearly delineated. The plan will start with pre-tokenization and conclude with the tokenization of fresh input. My aim is to demonstrate the process completely, with emphasis on the interplay between the original vocabulary and the learned subwords.
BPE(Byte-Pair Encoding)算法既是构建词表的过程,其产物(基础词表和合并规则)又是 tokenize 新句子的核心依据。
更准确地说,BPE 算法的核心思想是:从一个小的基础词表(通常是字符集)开始,通过迭代地学习最高频的字节对(byte pair)并将它们合并成新的、更大的 token,从而动态地构建出最终的词表。
所以,BPE 存在于**“构建词表”这个阶段,并且其学习到的合并规则 (merges)** 直接决定了 tokenize 时如何对新句子进行切分。
让我们通过一个简单的例子,手动走一遍 BPE 的完整流程。
假设我们的训练语料库 (Corpus) 只有几句话:
"new normal"
"new york"
"new new"
第1步:确定基础词汇表 (Initial Vocabulary)
首先,我们从语料库中提取出所有最基础的字符,形成我们的初始词汇表。同时,为了方便处理,我们将每个词的末尾加上一个特殊的结束符 </w> 来表示单词的边界。
"new normal" -> n e w </w> (1次)"new york" -> n e w </w> (1次), y o r k </w> (1次)"new new" -> n e w </w> (2次)(n e w </w>): 4次(n o r m a l </w>): 1次(y o r k </w>): 1次['< /w >', 'a', 'e', 'k', 'l', 'm', 'n', 'o', 'r', 'w', 'y']