สร้างโทเคนสำหรับโมเดล Transformer
คำย่อย การเติมช่องว่าง และมาสก์แอตเทนชัน
สร้างโทเคนสำหรับโมเดล Transformer เป็นบทเรียน NLP Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน NLP Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส NLP Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Tokens Come First
Before a transformer can learn anything, your text must become numbers. That conversion job belongs to the tokenizer.
Match the Model
Always load the tokenizer that was trained with your model. A mismatched vocabulary produces garbage ids the model never saw.
from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained("bert-base-uncased")Subword Pieces
Modern tokenizers split rare words into smaller chunks called subwords, so even unknown text stays representable.
print(tok.tokenize("tokenization"))Why Subwords Win
Subwords keep the vocabulary small while still handling typos and new words. The model rarely meets a true unknown token.
From Tokens to Ids
Each subword maps to an integer id. Calling the tokenizer on text returns those ids ready for the model.
enc = tok("Fine-tuning is fun")
print(enc["input_ids"])Special Tokens
Tokenizers add markers like CLS and SEP so the model knows where a sequence starts and ends. These are the special tokens.
Padding to Equal Length
Batches need same-length rows, so shorter texts get filler tokens. This step is called padding.
tok(texts, padding=True)Truncation for Long Text
Models cap input length, so very long text is cut to fit. Enabling truncation keeps every example within the limit.
tok(texts, truncation=True, max_length=128)The Attention Mask
An attention mask marks real tokens as 1 and padding as 0, so the model ignores the filler positions.
print(enc["attention_mask"])Return Tensors
Ask the tokenizer for framework tensors directly so the output drops straight into training without extra conversion.
tok("hello", return_tensors="pt")Decode Back to Text
To read predictions, run ids through decode and the tokenizer rebuilds the original text, special tokens stripped.
print(tok.decode(enc["input_ids"]))Quick Check
What is the job of the attention mask during batching?
Recap
Tokenizers turn text into subword ids, add special tokens, then handle padding, truncation, and the attention mask for clean batches. ✅
คำถามที่พบบ่อย
บทเรียน “สร้างโทเคนสำหรับโมเดล Transformer” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “สร้างโทเคนสำหรับโมเดล Transformer” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส NLP Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส NLP Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “สร้างโทเคนสำหรับโมเดล Transformer”
คำย่อย การเติมช่องว่าง และมาสก์แอตเทนชัน คุณปฏิบัติ NLP Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน NLP Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน NLP Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “สร้างโทเคนสำหรับโมเดล Transformer” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน NLP Academy นี้ได้ไหม
ได้ บทเรียน NLP Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทัวร์ไลบรารี Transformers
- สร้างโทเคนสำหรับโมเดล Transformer
- ปรับจูนด้วย Trainer API
- ประเมินและบันทึกโมเดลของคุณ