__len__ is the Python special method that lets len() report an object's length. In a PyTorch Dataset, it returns the number of available items. section 7.1.2
A Dataset implementation uses __len__ alongside __getitem__ to provide uniform indexed access:
- Return one constant dataset length; some PyTorch use cases cache this value. section 12.5
- Make __getitem__ return a valid item for every index from 0 through N–1 when __len__ returns N. section 12.5
- Base the result on the collection of samples, such as the length of candidateInfo_list. section 12.5
Counting dataset items
For CIFAR-10, calling len(cifar10) returns 50000. In a custom dataset, def __len__(self): return len(self.candidateInfo_list) reports the number of candidate samples. section 7.1.2 section 12.5
Figure 7.2 Concept of a PyTorch Dataset object. It doesn’t necessarily hold the data, but it provides uniform access to it through __len__ and __getitem__.
A diagram showing how __len__ and __getitem__ provide uniform access to a PyTorch Dataset, whether or not it stores the data directly. section 7.1.2
