PyTorch: Why is my dataset class giving index out of range errors?

Viewed 860

I am trying to figure out why my data set is giving out of range index errors.

Consider this torch data set:

# prepare torch data set
class MSRH5Processor(torch.utils.data.Dataset):
    def __init__(self, type, shard=False, **args):
        # init configurable string
        self.type = type
        # init shard for sampling large ds if specified
        self.shard = shard
        # set seed if given
        self.seed = args
        # set loc
        self.file_path = 'C:\\data\\h5py_embeds\\'
        # set file paths
        self.val_embed_path = self.file_path + 'msr_dev_bert_embeds.h5'

        # if true, initialize the dev data
        if self.type == 'dev':
            # embeds are shaped: [layers, tokens, features]
            self.embeddings = h5py.File(self.val_embed_path, 'r')["embeds"]

    def __len__(self):
        return len(self.embeddings)

    def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()

        if self.type == 'dev':
            sample = {'embeddings': self.embeddings[idx]}
            return sample

# load dataset
processor = MSRH5Processor(type='dev', shard=False)
# check length
len(processor)  # 22425

# iterate over the samples
count = 0
for step, batch in enumerate(processor):
    count += 1
# error: Index (22425) out of range (0-22424)

with h5py.File('C:\\w266\\h5py_embeds\\msr_dev_bert_embeds.h5', 'r') as f:
    print(f['embeds'].attrs['last_index'])  # 22425
    print(f['embeds'].shape)  # (22425, 128, 768)
    print(len(f['embeds']))  # 22425

If I manually change the data set length to 100 or 22424, I will still get the same error. What is telling PyTorch to look for index 22425?

If I were to make a CSV data set, with 1000 observations (where len = 1000), it would stop entering indices into the __getitem__() method at 999 and not 1000.

Edit:

It seems to be an issue with just the Dataset class and H5py files. If I use a torch dataloader, it will run to the natural length of my data set. Although, I would love to know what Torch is doing to get this figure for my H5 files that is causing it to behave differently than say a CSV.

1 Answers

To use Dataset as an iterable you must implement either __iter__ method or __getitem__ with Sequence semantics. The iteration stops when method __getitem__ raises IndexError for some index idx

The problem with your dataset is that:

self.embeddings = h5py.File(self.val_embed_path, 'r')["embeds"]

is actually of type h5py._hl.dataset.Dataset which on out-of-index requests raises ValueError

You have to either load entire embeddings at the class constructor so that accessing numpy array on out-of-index will raise IndexError or re-throw IndexError on ValueError in __getitem__

Related