NLP Transformers - Chapter 10 - Training Transformers from Scratch
Mục tiêu đọc
- Hiểu khi nào cần train từ đầu thay vì fine-tune.
- Nắm cách xây dựng corpus lớn và tokenizer riêng.
- Biết các bước pretraining model ở mức kiến trúc và training loop.
Ý chính
- Train từ đầu cần dữ liệu lớn, compute lớn và mục tiêu rõ ràng.
- Tokenizer ảnh hưởng trực tiếp đến khả năng biểu diễn domain-specific text.
- Pretraining objectives quyết định model học loại thông tin nào từ corpus.
Demo thực hành
Train tokenizer nhỏ trên corpus toy.
from tokenizers import Tokenizer
from tokenizers.models import BPE
from tokenizers.pre_tokenizers import Whitespace
from tokenizers.trainers import BpeTrainer
corpus = [
"transformers are useful for natural language processing",
"tokenizers split text into tokens",
"domain specific data can need a custom tokenizer",
]
with open("toy_corpus.txt", "w") as f:
for line in corpus:
f.write(line + "\n")
tokenizer = Tokenizer(BPE(unk_token="[UNK]"))
tokenizer.pre_tokenizer = Whitespace()
trainer = BpeTrainer(vocab_size=50, special_tokens=["[UNK]", "[PAD]"])
tokenizer.train(["toy_corpus.txt"], trainer)
encoded = tokenizer.encode("transformers tokenize domain text")
print(encoded.tokens)Khái niệm quan trọng
Active Recall
- Khi nào train từ đầu hợp lý hơn fine-tune?
- Corpus cho pretraining cần kiểm soát những rủi ro nào?
- Tokenizer riêng giúp gì cho domain đặc thù?
- Pretraining objective ảnh hưởng output cuối như thế nào?
Checklist
- Đọc xong chapter
- Chạy demo tokenizer
- Viết lại pipeline train từ đầu bằng 5 bước
- Tách concept cần dùng lại
- Cập nhật tiến độ sách