下列文本提取自本地原版 PDF。图表和公式的准确布局请对照 PDF。
2 Byte-Pair Encoding (BPE) Tokenizer
In the first part of the assignment, we will train and implement a byte-level byte-pair encoding (BPE)
tokenizer [R. Sennrich et al., 2016; C. Wang et al., 2019]. In particular, we will represent arbitrary
(Unicode) strings as a sequence of bytes and train our BPE tokenizer on this byte sequence. Later, we
will use this tokenizer to encode text (a string) into tokens (a sequence of integers) for language modeling.
2.1 The Unicode Standard
Unicode is a text encoding standard that maps characters to integer code points. As of Unicode 17.0
(released in September 2025), the standard defines 159,801 characters across 172 scripts. For example, the
character “s” has the code point 115 (typically notated as U+0073, where U+ is a conventional prefix and
0073 is 115 in hexadecimal), and the character “牛” has the code point 29275. In Python, you can use the
ord() function to convert a single Unicode character into its integer representation. The chr() function
converts an integer Unicode code point into a string with the corresponding character.
>>> ord('牛')
29275
>>> chr(29275)
'牛'
Problem (unicode1): Understanding Unicode (1 point)
(a) What Unicode character does chr(0) return?
Deliverable: A one-sentence response.
(b) How does this character’s string representation (__repr__()) differ from its printed
representation?
Deliverable: A one-sentence response.
(c) What happens when this character occurs in text? It may be helpful to play around with the
following in your Python interpreter and see if it matches your expectations:
>>> chr(0)
>>> print(chr(0))
>>> "this is a test" + chr(0) + "string"
>>> print("this is a test" + chr(0) + "string")
Deliverable: A one-sentence response.
2.2 Unicode Encodings
While the Unicode standard defines a mapping from characters to code points (integers), it’s impractical
to train tokenizers directly on Unicode code points, since the vocabulary would be prohibitively large
(around 150K items) and sparse (since many characters are quite rare). Instead, we’ll use a Unicode
encoding, which converts a Unicode character into a sequence of bytes. The Unicode standard itself
defines three encodings: UTF-8, UTF-16, and UTF-32, with UTF-8 being the dominant encoding for the
Internet (more than 98% of all webpages).
To encode a Unicode string into UTF-8, we can use the encode() function in Python. To access the
underlying byte values for a Python bytes object, we can iterate over it (e.g., call list()). Finally, we can
use the decode() function to decode a UTF-8 byte string into a Unicode string.
>>> test_string = "hello! こんにちは!"
>>> utf8_encoded = test_string.encode("utf-8")
>>> print(utf8_encoded)
b'hello! \xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf!'
>>> print(type(utf8_encoded))
<class 'bytes'>
>>> # Get the byte values for the encoded string (integers from 0 to 255).
>>> list(utf8_encoded)
[104, 101, 108, 108, 111, 33, 32, 227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129,
161, 227, 129, 175, 33]
>>> # One byte does not necessarily correspond to one Unicode character!
>>> print(len(test_string))
13
>>> print(len(utf8_encoded))
23
>>> print(utf8_encoded.decode("utf-8"))
hello! こんにちは!
By converting our Unicode code points into a sequence of bytes (e.g., via the UTF-8 encoding), we are
essentially taking a sequence of code points (21-bit integers with 159,801 valid values) and transforming it
into a sequence of byte values (integers in the range 0 to 255). The 256-length byte vocabulary is much
more manageable to deal with. When using byte-level tokenization, we do not need to worry about out-of-
vocabulary tokens, since we know that any input text can be expressed as a sequence of integers from 0
to 255.
Problem (unicode2): Unicode Encodings (3 points)
(a) What are some reasons to prefer training our tokenizer on UTF-8 encoded bytes, rather than
UTF-16 or UTF-32? It may be helpful to compare the output of these encodings for various
input strings.
Deliverable: A one-to-two sentence response.
(b) Consider the following (incorrect) function, which is intended to decode a UTF-8 byte string
into a Unicode string. Why is this function incorrect? Provide an example of an input byte
string that yields incorrect results.
def decode_utf8_bytes_to_str_wrong(bytestring: bytes):
return "".join([bytes([b]).decode("utf-8") for b in bytestring])
>>> decode_utf8_bytes_to_str_wrong("hello".encode("utf-8"))
'hello'
Deliverable: An example input byte string for which decode_utf8_bytes_to_str_wrong
produces incorrect output, with a one-sentence explanation of why the function is incorrect.
(c) Give a two-byte sequence that does not decode to any Unicode character(s).
Deliverable: An example, with a one-sentence explanation.
2.3 Subword Tokenization
While byte-level tokenization can alleviate the out-of-vocabulary issues faced by word-level tokenizers,
tokenizing text into bytes results in extremely long input sequences. This slows down model training,
since a sentence with 10 words might only be 10 tokens long in a word-level language model, but could be
50 or more tokens long in a character-level model (depending on the length of the words). Processing
these longer sequences requires more computation at each step of the model. Furthermore, language
modeling on byte sequences is difficult because the longer input sequences create long-term dependencies
in the data.
Subword tokenization is a midpoint between word-level tokenizers and byte-level tokenizers. Note that a
byte-level tokenizer’s vocabulary has 256 entries (byte values are 0 to 255). A subword tokenizer trades off
a larger vocabulary size for better compression of the input byte sequence. For example, if the byte
sequence b'the' often occurs in our raw text training data, assigning it an entry in the vocabulary would
reduce this 3-token sequence to a single token.
How do we select these subword units to add to our vocabulary? R. Sennrich et al. [3] propose to use
byte-pair encoding (BPE; P. Gage [5]), a compression algorithm that iteratively replaces (“merges”) the
most frequent pair of bytes with a single, new unused index. Note that this algorithm adds subword
tokens to our vocabulary to maximize the compression of our input sequences—if a word occurs in our
input text enough times, it’ll be represented as a single subword unit.
Subword tokenizers with vocabularies constructed via BPE are often called BPE tokenizers. In this
assignment, we’ll implement a byte-level BPE tokenizer, where the vocabulary items are bytes or merged
sequences of bytes, which give us the best of both worlds in terms of out-of-vocabulary handling and
manageable input sequence lengths. The process of constructing the BPE tokenizer vocabulary is known
as “training” the BPE tokenizer.
本章讨论
在 GitHub 查看记录 ↗