curiousily / Getting-Things-Done-with-Pytorch

Jupyter Notebook tutorials on solving real-world problems with Machine Learning & Deep Learning using PyTorch. Topics: Face detection with Detectron 2, Time Series anomaly detection with LSTM Autoencoders, Object Detection with YOLO v5, Build your first Neural Network, Time Series forecasting for Coronavirus daily cases, Sentiment Analysis with BER
https://mlexpert.io
Apache License 2.0
2.33k stars 625 forks source link

AttributeError: 'dict' object has no attribute 'dataset' #21

Closed ulfat191 closed 2 years ago

ulfat191 commented 2 years ago

I am getting this error while writing the following class code. The error message is attached below the code.

class GPReviewDataset(data.dataset):

def init(self, review, target, tokenizer, max_len): self.review = review self.target = target self.tokenizer = tokenizer self.max_len = max_len

def len(self):

return the number of reviews we have

return len(self.reviews)

def getitem(self, item): # takes the index and the reviews review = str(self.reviews[item])

encoding = self.tokenizer.encode_plus(
  review,
  add_special_tokens=True,
  max_length=self.max_len,
  return_token_type_ids=False,
  pad_to_max_length=True,
  return_attention_mask=True,
  return_tensors='pt'
)

return {
  'review_text': review,
  'input_ids': encoding['input_ids'].flatten(),
  'attention_mask': encoding['attention_mask'].flatten(),
  'targets': torch.tensor(self.target, dtype=torch.long)
}

image

ulfat191 commented 2 years ago

for those who are facing this error as you code while watching the video, the solution is as follows - 1) class GPReviewDataset(data.dataset): -> change 'data.dataset' to 'Dataset' -> change is already made in the code but not in the video

2) In the create dataloader function write return DataLoader instead of return data.DataLoader

Thanks.