0Pricing
Deep Learning Academy · レッスン

カスタムDatasetクラスを書く

__len__と__getitem__を実装します

「カスタムDatasetクラスを書く」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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):
    pass

Stash 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 = y

Two 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. 🎉

よくある質問

「カスタムDatasetクラスを書く」レッスンは無料ですか?

はい。「カスタムDatasetクラスを書く」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。

「カスタムDatasetクラスを書く」で何を学びますか?

__len__と__getitem__を実装します ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Deep Learning Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「カスタムDatasetクラスを書く」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このDeep Learning Academyレッスンでコードを書いて実行できますか?

はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. カスタムDatasetクラスを書く
  2. バッチ化、シャッフル、num_workers
  3. 可変長入力のためのcollate_fn
  4. 入力を正規化・標準化する
← Deep Learning Academyに戻る