Assignment 1, Writeup
包含:
- Byte-pair encoding (BPE) tokenizer
- Transformer language model (LM)
- The cross-entropy loss function and the AdamW optimizer
- The training loop, with support for serializing and loading model and optimizer state
Byte-Pair Encoding (BPE) Tokenizer
Problem (unicode1): Understanding Unicode (1 point)
(a) What Unicode character does chr(0) return?
Null 字符。
(b) How does this character’s string representation (__repr__()) differ from its printed representation?
交互环境下输入 chr(0) 就是调用 __repr__(),看到 ‘\x00’。print(chr(0)) 什么都不显示。
(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:
1
2
3
4 chr(0)
print(chr(0))
"this is a test" + chr(0) + "string"
print("this is a test" + chr(0) + "string")
1 | chr(0) |
Problem (unicode2): Unicode Encodings
(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.
UTF-32 是定长的,每个字符固定 4 字节,导致处理英文文本时空间浪费。
UTF-8 对 ASCII 字符(英文、数字)完全兼容(只占 1 字节),在互联网上最通用。
UTF-16 存在大端序/小端序问题。
(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.
1
2
3 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.
1 | decode_utf8_bytes_to_str_wrong("牛".encode("utf-8")) |
原因解释: UTF-8 是变长的。一个汉字占三个字节。
(c) Give a two byte sequence that does not decode to any Unicode character(s).
Deliverable: An example, with a one-sentence explanation.
1 | b'\xFF'.decode("utf-8") |
0xff 这种字节在 UTF-8 规范中不出现。