Downloads · 30 days
0
42charlie/GPT-2-Shakespeare-Tokenizer
GPT-2-Shakespeare-Tokenizer is a machine learning model from 42charlie. Use it for the machine learning task on the model card, and read the license before you ship it in a product. The card lists the license as mit.
A from-scratch implementation of the Byte Pair Encoding (BPE) algorithm to demystify how Large Language Models (LLMs) process text.
Downloads · 30 days
0
Access
Public
Updated Dec 4, 2025
Repo size
—
Likes
1
Public
Click a slice to open those files.
.json70.8 KB · 64%
From the Hugging Face model README
A from-scratch implementation of the Byte Pair Encoding (BPE) algorithm to demystify how Large Language Models (LLMs) process text.
⚠️ Note for Learning: This project was built for educational purposes to reverse-engineer and understand the BPE algorithm. While functional, it is a Python-based implementation optimized for clarity and learning rather than maximum training speed on massive corpora.
About 8 months ago, fascinated by the release of powerful LLMs like ChatGPT, I decided that simply using them wasn't enough—I wanted to understand them. I went back to the basics: The Tokenizer.
I built this project to reverse-engineer the tokenization process used by models like GPT-2. Instead of relying on pre-built libraries like HuggingFace, I wrote the logic myself in pure Python to fully grasp the mechanics of vocabulary construction, merge rules, and text compression.
Ġ) used by GPT-2 to preserve sentence structure.This project implements Byte Pair Encoding (BPE), an iterative algorithm that compresses text by replacing the most frequent pair of adjacent bytes with a new, single token.
('t', 'h')) is merged into a new token ('th').This tokenizer was trained on the complete works of William Shakespeare.
To reproduce the training results:
the_complete_work.txt in the project root.(Note: The dataset itself is not included in this repository to keep it lightweight.)
from tokenizer import Tokenizer
# 1. Initialize
tokenizer = Tokenizer()
# 2. Train on your own dataset (only needs to be done once)
# tokenizer.train("the_complete_work.txt", ntokens=4000)
# 3. Encode text (converts string -> list of integer IDs)
text = "I think the noble Duke is good"
ids = tokenizer.encode(text)
print(f"Token IDs: {ids}")
# Output: [7, 1062, 1959, 315, 1494, 2218]
# 4. Decode (converts IDs -> original string)
decoded_text = tokenizer.decode(ids)
print(f"Decoded: {decoded_text}")
Building this project was a deep dive into Natural Language Processing (NLP) and software engineering fundamentals:
dictionaries and sets for O(1) lookups during the frequent pair counting process.This implementation is based on the methods described in the foundational papers that adapted BPE (originally a compression algorithm) for Neural Machine Translation and LLMs.
Primary Reference:
"Neural Machine Translation of Rare Words with Subword Units" > Rico Sennrich, Barry Haddow, Alexandra Birch (2016)
"We introduce a simpler and more effective approach... making the NMT model capable of open-vocabulary translation by encoding rare and unknown words as sequences of subword units."
Original Algorithm:
"A New Algorithm for Data Compression" > Philip Gage (1994)
Created by [Ahmed Sadik]