# 从零实现字节对编码（BPE）分词器

- 原标题：Implementing A Byte Pair Encoding \(BPE\) Tokenizer From Scratch
- 类型：教程
- 来源：Sebastian Raschka
- 发布时间：2025-01-17
- 首次精选：2026-10-29 23:09
- 分级：值得看
- 学习层级：进阶
- 主题：[neural-model-foundations](<https://oplearnx.com/md/topics/neural-model-foundations.md>)（第 14 位）
- 原文链接：[https://sebastianraschka.com/blog/2025/bpe-from-scratch.html](<https://sebastianraschka.com/blog/2025/bpe-from-scratch.html>)
- 本站页面：[https://oplearnx.com/resources/pk\_0d00153277a4](<https://oplearnx.com/resources/pk_0d00153277a4>)

## 摘要

一份用于教学目的的独立 notebook，从零实现 GPT-2 至 GPT-4、Llama 3 等模型使用的字节对编码（BPE）分词算法，并附带训练分词器的函数，还可加载 OpenAI 原始分词器词表与合并规则；文中提及 minBPE、tiktoken 等实现，以及 BPE 算法最早出自 1994 年 Philip Gage 的论文。适合想搞懂 BPE 分词原理的学习者。

## 原文涉及

- [GitHub - openai/tiktoken: tiktoken is a fast BPE tokeniser for use with OpenAI's models.](<https://github.com/openai/tiktoken>)

---
来源：LearnMap · 学径（https://oplearnx.com）
