สร้างเอ็มเบดดิงประโยคด้วย BERT
ดึงเวกเตอร์ตามบริบทด้วยโค้ด
สร้างเอ็มเบดดิงประโยคด้วย BERT เป็นบทเรียน NLP Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน NLP Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส NLP Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
From Words to Sentences
BERT gives every token its own vector. But often you want one vector for the whole sentence, not each word.
The CLS Token
BERT adds a special token named CLS at the very front of each input. Its output is often used as a sentence summary.
Mean Pooling
Another common trick averages all the token vectors into one. This mean pooling step often beats the raw CLS vector.
Load a Model
Hugging Face makes loading easy: grab a tokenizer and a model with one line each. They form your pipeline for embeddings.
from transformers import AutoTokenizer, AutoModel
tok = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModel.from_pretrained("bert-base-uncased")Tokenize the Text
The tokenizer turns your sentence into id numbers and an attention mask BERT understands. This is the encoding step.
inputs = tok("I love NLP", return_tensors="pt")Run the Model
Pass the encoded inputs into BERT to get hidden states for every token. These vectors are the raw output you will pool.
out = model(**inputs)
states = out.last_hidden_statePool to One Vector
Average the token states along the sequence to collapse them into a single sentence embedding you can store.
vec = states.mean(dim=1)An Easier Path
The Sentence-Transformers library wraps all of this for you. One call returns a clean sentence vector ready to use. ✨
from sentence_transformers import SentenceTransformer
m = SentenceTransformer("all-MiniLM-L6-v2")
vec = m.encode("I love NLP")Compare Sentences
With two sentence vectors you measure closeness using cosine similarity. Higher scores mean the sentences mean similar things.
What You Can Build
Sentence embeddings power semantic search, clustering, and duplicate detection. They turn meaning into something you can search.
A Note on Quality
Models tuned for sentences, like MiniLM, usually beat plain BERT here. Pick one trained for the task you actually have.
Quick Check
How do you turn many token vectors into one sentence vector?
Recap
Tokenize, run BERT, then pool or use CLS to get one sentence embedding. Sentence-Transformers makes it a single call. ✅
คำถามที่พบบ่อย
บทเรียน “สร้างเอ็มเบดดิงประโยคด้วย BERT” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “สร้างเอ็มเบดดิงประโยคด้วย BERT” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส NLP Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส NLP Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “สร้างเอ็มเบดดิงประโยคด้วย BERT”
ดึงเวกเตอร์ตามบริบทด้วยโค้ด คุณปฏิบัติ NLP Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน NLP Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน NLP Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “สร้างเอ็มเบดดิงประโยคด้วย BERT” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน NLP Academy นี้ได้ไหม
ได้ บทเรียน NLP Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดบริบทจึงเปลี่ยนความหมายของคำ
- การสร้างแบบจำลองภาษาด้วยการปิดบัง
- สร้างเอ็มเบดดิงประโยคด้วย BERT
- เลือกโมเดลที่ฝึกไว้ล่วงหน้าให้เหมาะสม