Issue
I following this pytorch tutorial
On the basis of this link I wrote the following code:
import torch
import torchvision
from torch.utils.data import Dataset,DataLoader
import numpy as np
import math
class WineDataset(Dataset):
def __int__(self):
xy =np.loadtxt("wine.csv",delimiter=',',dtype=np.float32,skiprows=1)
self.n_samples =xy.shape[0]
self.x_data = torch.from_numpy(xy[:, 1:])
self.y_data = torch.from_numpy(xy[:, [0]])
def __getitem__(self, index):
return self.x_data[index],self.y_data[index]
def __len__(self):
return self.n_samples
# create dataset
dataset = WineDataset()
# get first sample and unpack
first_data = dataset[0]
features, labels = first_data
print(features, labels)
But when I run the code, it gives me following error:
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\AI_Project\Dataset_DataLoader.py", line 19, in <module>
first_data = dataset.__getitem__(0)
File "C:\Users\User\PycharmProjects\AI_Project\Dataset_DataLoader.py", line 13, in __getitem__
return self.x_data[index],self.y_data[index]
AttributeError: 'WineDataset' object has no attribute 'x_data'
This line is also written on GitHub:
# create dataset
dataset = WineDataset()
# get first sample and unpack
first_data = dataset[0]
features, labels = first_data
print(features, labels)
Where is the mistake?
Solution
Change def __int__(self)
to def __init__(self)
. Right now you have no constructor.
Answered By - Ryan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.