2

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

3
  • make sure the name is correct in your code. It should be __len__. And it would be better if you post the code that defines your Dataset class. Commented Jul 10, 2019 at 13:22
  • 1
    @akshayk07 such a stupid mistake.... thanks for your answer, i need a break :p Commented Jul 10, 2019 at 13:26
  • I have added it as an answer. Accept the answer so that this question can be closed as such. Commented Jul 10, 2019 at 13:27

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.