0Pricing
Deep Learning Academy · บทเรียน

collate_fn สำหรับอินพุตความยาวต่างกัน

เติมค่าและวางตัวอย่างที่มีขนาดไม่สม่ำเสมอซ้อนกัน

collate_fn สำหรับอินพุตความยาวต่างกัน เป็นบทเรียน Deep Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Deep Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

When Samples Don't Match

Stacking into a batch needs every sample the same shape. But sentences and audio clips have different lengths, so the default collate step fails. 🧩

What collate_fn Does

The DataLoader gathers a list of samples and passes them to collate_fn, which merges them into one batch. By default it simply stacks tensors.

Ragged Inputs Break Stacking

Try to stack a length-5 and a length-8 sequence and PyTorch raises a shape error. Ragged lengths are exactly the case a custom collate must handle.

Write Your Own collate_fn

You pass a function to the DataLoader's collate_fn argument. It receives a list of samples and returns whatever batch shape your model expects.

loader = DataLoader(ds, batch_size=4, collate_fn=my_collate)

Step One: Split the List

Inside your function, unzip the list of pairs into separate sequences and labels. Now you can treat each group on its own before merging.

def my_collate(batch):
    seqs, labels = zip(*batch)

Pad to the Longest

The trick for variable lengths is padding: extend every sequence to the longest one with a filler value, so they finally share a shape.

pad_sequence Does It for You

PyTorch ships pad_sequence, which pads a list of tensors to equal length and stacks them. Set batch_first so the batch dimension comes first.

from torch.nn.utils.rnn import pad_sequence
padded = pad_sequence(seqs, batch_first=True)

Remember the Real Lengths

Padding adds fake tokens, so also return each sequence's true length. Your model uses these to ignore the padded positions during the forward pass.

lengths = torch.tensor([len(s) for s in seqs])

Stack the Labels

Labels are usually fixed size, so a normal stack works for them. Return the padded inputs, the lengths, and the stacked labels together.

labels = torch.stack(labels)
return padded, lengths, labels

Mask Out the Padding

Later you build a mask from the lengths so the loss and attention skip padded slots. Padding fills shape without polluting the gradients.

One Function, Any Shape

With a custom collate_fn, the same DataLoader handles text, audio, and graphs. You control exactly how loose samples become one tidy batch.

Quick Check

Why do variable-length sequences need a custom collate_fn?

Recap

A custom collate_fn turns a list of uneven samples into one batch, usually by padding sequences to equal length and tracking their real sizes. 🎉

คำถามที่พบบ่อย

บทเรียน “collate_fn สำหรับอินพุตความยาวต่างกัน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “collate_fn สำหรับอินพุตความยาวต่างกัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Deep Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “collate_fn สำหรับอินพุตความยาวต่างกัน”

เติมค่าและวางตัวอย่างที่มีขนาดไม่สม่ำเสมอซ้อนกัน คุณปฏิบัติ Deep Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Deep Learning Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Deep Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “collate_fn สำหรับอินพุตความยาวต่างกัน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Deep Learning Academy นี้ได้ไหม

ได้ บทเรียน Deep Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เขียนคลาสชุดข้อมูลแบบกำหนดเอง
  2. การจัดแบตช์ การสับเปลี่ยน และ num_workers
  3. collate_fn สำหรับอินพุตความยาวต่างกัน
  4. ปรับอินพุตให้เป็นมาตรฐาน
← กลับไปที่ Deep Learning Academy