จัดแพ็กลำดับและรับมือกับแพดดิง
ฝึกอย่างมีประสิทธิภาพกับข้อมูลที่มีความยาวต่างกัน
จัดแพ็กลำดับและรับมือกับแพดดิง เป็นบทเรียน Deep Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Deep Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Batches Want Rectangles
To process many sequences at once, a batch must be a neat rectangle. But real sentences have different lengths, so they don't line up.
Padding to a Common Length
The fix is padding: add filler tokens, usually zeros, to short sequences so every row in the batch is the same length.
pad_sequence Helper
PyTorch's pad_sequence stacks a list of variable-length tensors into one padded batch in a single call.
from torch.nn.utils.rnn import pad_sequence
batch = pad_sequence(seqs, batch_first=True)Padding Wastes Compute
Those filler tokens are fake. If the RNN processes them anyway, it burns compute and can let the padding pollute the hidden state.
Track the Real Lengths
Before padding, record each sequence's true length. You'll hand these lengths to PyTorch so it knows where the real data ends.
lengths = [len(s) for s in seqs]Packing the Batch
pack_padded_sequence turns the padded batch plus lengths into a compact form the RNN can run without touching the padding.
packed = pack_padded_sequence(batch, lengths, batch_first=True)Run the RNN on Packed Data
Feed the packed object straight into nn.LSTM or nn.GRU. The cell skips padded steps, so memory stays clean and clear.
out_packed, h_n = lstm(packed)Unpack the Output
To read per-step outputs again, call pad_packed_sequence to restore the rectangular padded shape after the RNN finishes.
out, lengths = pad_packed_sequence(out_packed, batch_first=True)Sort by Length
Classic packing wants sequences sorted longest first. Set enforce_sorted to False and PyTorch handles unsorted batches for you.
Mask the Loss
Even with packing, ignore padded positions when scoring. A mask or ignore_index keeps fake tokens out of the loss.
loss_fn = nn.CrossEntropyLoss(ignore_index=PAD_ID)Wire It Into the DataLoader
Do the padding inside a custom collate_fn so every batch arrives already padded, with lengths ready for packing.
Quick Check
Why pack a padded batch before feeding it to an RNN?
Recap
Pad to align a batch, track true lengths, then pack so the RNN ignores filler. Mask padded tokens in the loss too. ✅
คำถามที่พบบ่อย
บทเรียน “จัดแพ็กลำดับและรับมือกับแพดดิง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “จัดแพ็กลำดับและรับมือกับแพดดิง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Deep Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “จัดแพ็กลำดับและรับมือกับแพดดิง”
ฝึกอย่างมีประสิทธิภาพกับข้อมูลที่มีความยาวต่างกัน คุณปฏิบัติ Deep Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Deep Learning Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Deep Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “จัดแพ็กลำดับและรับมือกับแพดดิง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Deep Learning Academy นี้ได้ไหม
ได้ บทเรียน Deep Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดลำดับจึงต้องอาศัยหน่วยความจำ
- เซลล์ RNN แบบพื้นฐาน
- เกตของ LSTM และ GRU
- จัดแพ็กลำดับและรับมือกับแพดดิง