首先写个数据集的基类,使用__call__函数调用__getitem__函数
class Datasetbase():
def __init__(self) -> None:
pass
def __call__(self,index):
yield self.__getitem__(index)
def __getitem__(self):
pass
def __len__(self):
pass
这样后面写自己的数据集中的__getitem__后,就会被父类的__call__调用了
class mydataset(Datasetbase):
def __init__(self) -> None:
super().__init__()
self.list = [1,2,3,4,5]
def __getitem__(self,index):
return self.list[index]
dataloader的作用有两个是实现并行,和数据的可迭代。如下使用yied实现了数据的可迭代,并行暂时未实现
class dataloader():
def __init__(self,list) -> None:
self.list = list
def __iter__(self):
for i in range(self.__len__()):
yield self.list[i]
def __len__(self):
return len(self.list.list)
T = mydataset()
t_iter = dataloader(T)
for i in t_iter:
print(i)