เขียนคลาสชุดข้อมูลแบบกำหนดเอง
ใช้งาน __len__ และ __getitem__
เขียนคลาสชุดข้อมูลแบบกำหนดเอง เป็นบทเรียน Deep Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Deep Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Your Data Needs a Front Door
Before a model can learn, PyTorch needs a clean way to reach your samples one at a time. That front door is a Dataset class. 🚪
Start by Subclassing
You build a custom dataset by subclassing torch.utils.data.Dataset. PyTorch then knows exactly how to ask your object for data.
from torch.utils.data import Dataset
class MyData(Dataset):
passStash Your Data in __init__
The __init__ method runs once when you create the dataset. Use it to load file paths, arrays, or labels into the object's fields.
def __init__(self, X, y):
self.X = X
self.y = yTwo Methods Make It Work
A working dataset only needs two methods: __len__ to report its size and __getitem__ to fetch one sample. That is the whole contract.
__len__ Counts Your Samples
The __len__ method returns how many samples you have. PyTorch reads this to know when an epoch ends and how far an index can go.
def __len__(self):
return len(self.X)__getitem__ Returns One Sample
Given an index, __getitem__ returns a single sample, usually a feature and its label. This is where one row of data is handed over.
def __getitem__(self, idx):
return self.X[idx], self.y[idx]Return Tensors, Not Lists
__getitem__ should hand back tensors so the model can use them directly. Convert NumPy arrays or Python lists right here if needed.
import torch
x = torch.tensor(self.X[idx], dtype=torch.float32)Lazy Loading for Big Data
For huge datasets, do not load everything in __init__. Instead read each file inside __getitem__ so only one sample sits in memory at a time.
Apply Transforms Per Sample
__getitem__ is the natural place to apply a transform, like resizing an image. Store the transform in __init__, then call it before returning.
if self.transform:
x = self.transform(x)Index It Like a List
Once built, your dataset behaves like a list. Calling len(ds) or ds[0] just triggers the two methods you defined. Test it before training.
ds = MyData(X, y)
print(len(ds), ds[0])Now It Plugs Into Everything
This tidy interface is why a custom Dataset drops straight into a DataLoader. You write two methods and the rest of PyTorch just works.
Quick Check
Which method does PyTorch call to fetch a single sample by index?
Recap
A custom dataset subclasses Dataset and defines two methods: __len__ for its size and __getitem__ to return one sample. Two methods, full power. 🎉
คำถามที่พบบ่อย
บทเรียน “เขียนคลาสชุดข้อมูลแบบกำหนดเอง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “เขียนคลาสชุดข้อมูลแบบกำหนดเอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Deep Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Deep Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เขียนคลาสชุดข้อมูลแบบกำหนดเอง”
ใช้งาน __len__ และ __getitem__ คุณปฏิบัติ Deep Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Deep Learning Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Deep Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “เขียนคลาสชุดข้อมูลแบบกำหนดเอง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Deep Learning Academy นี้ได้ไหม
ได้ บทเรียน Deep Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เขียนคลาสชุดข้อมูลแบบกำหนดเอง
- การจัดแบตช์ การสับเปลี่ยน และ num_workers
- collate_fn สำหรับอินพุตความยาวต่างกัน
- ปรับอินพุตให้เป็นมาตรฐาน