c_t
c_t

Reputation: 75

NotImplementedError: when i try to create a DataLoader object in Pytorch

I created a custom Dataset class that inherits from PyTorch's Dataset class, in order to handle my custom dataset which i already preprocessed.

When i try to create a DataLoader object, i get this error:

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in __init__(self, dataset, batch_size, shuffle, sampler, batch_sampler, num_workers, collate_fn, pin_memory, drop_last, timeout, worker_init_fn)
    174             if sampler is None:
    175                 if shuffle:
--> 176                     sampler = RandomSampler(dataset)
    177                 else:
    178                     sampler = SequentialSampler(dataset)

/usr/local/lib/python3.6/dist-packages/torch/utils/data/sampler.py in __init__(self, data_source, replacement, num_samples)
     62                              "since a random permute will be performed.")
     63 
---> 64         if not isinstance(self.num_samples, int) or self.num_samples <= 0:
     65             raise ValueError("num_samples should be a positive integer "
     66                              "value, but got num_samples={}".format(self.num_samples))

/usr/local/lib/python3.6/dist-packages/torch/utils/data/sampler.py in num_samples(self)
     70         # dataset size might change at runtime
     71         if self._num_samples is None:
---> 72             return len(self.data_source)
     73         return self._num_samples
     74 

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataset.py in __len__(self)
     18 
     19     def __len__(self):
---> 20         raise NotImplementedError
     21 
     22     def __add__(self, other):

NotImplementedError: 

So, the error message regards to the not implementation of the len() function in the dataset.py, right? But i did implement it and the getitem(), init() as well .

How can i overcome this? Thank you

Upvotes: 2

Views: 6221

Answers (1)

akshayk07
akshayk07

Reputation: 2200

Make sure the name is correct in your code. It should be __len__.

Upvotes: 6

Related Questions