'Why my custom dataset gives attribute error?

my initial data was like this

My data is a pandas dataframe with columns 'title' and 'label'. I want to make a custom dataset with this. so I made the dataset like below. I'm working on google colab

class newsDataset(torch.utils.data.Dataset):
  def __init__(self,train=True,transform=None):
    if train:
      self.file = ttrain
    else:
      self.file= ttest
    
    self.text_list = self.file['title'].values.tolist()
    self.class_list=self.file['label'].values.tolist()

  def __len__(self):
    return len(self.text_list)
  
  def __getitem__(self,idx):
    label = self.class_list[idx]
    text = self.text_list[idx]

    if self.transform is not None:
      text=self.transform(text)
      
    return label, text

and this is how I call the dataloader

trainset=newsDataset()
train_iter = DataLoader(trainset)
iter(train_iter).next()

and it gives

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-153-9872744bc8a9> in <module>()
----> 1 iter(train_iter).next()

5 frames
/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataset.py in __getattr__(self, attribute_name)
     81             return function
     82         else:
---> 83             raise AttributeError
     84 
     85     @classmethod

AttributeError: 

There was no exact error message. can anybody help me?



Solution 1:[1]

Please add the following missing line to your __init__ function:

self.transform = transform

Solution 2:[2]

You don't have self.transform attribute so you need to initialize it in __init__ method

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kkgarg
Solution 2 Henry Ecker