pytorch Dataset数据集和Dataloader迭代数据集
import torch
from torch.utils.data import Dataset,DataLoader
class SmsDataset(Dataset):
    def __init__(self):
        self.file_path = "./SMSSpamCollection"
        self.lines = open(self.file_path,encoding="utf-8").readlines()
    def __getitem__(self, index):
        line = self.lines[index].strip()
        label = line.split("\t")[0]
        sent = line.split("\t")[1]
        return label,sent
    def __len__(self):
        return len(self.lines)
sms_dataset = SmsDataset()
dataloader = DataLoader(sms_dataset,batch_size=2,shuffle=True)
if __name__ == '__main__':
    for idx,(label,sent) in enumerate(dataloader):
        print(idx)
        print(label)
        print(sent)
        break
    print(len(sms_dataset))
    print(len(dataloader))
0
('ham', 'spam')
('And popping <#> ibuprofens was no help.', 'This is the 2nd time we have tried 2 contact u. U have won the 750 Pound prize. 2 claim is easy, call 08712101358 NOW! Only 10p per min. BT-national-rate')
5574
2787
    多思考也是一种努力,做出正确的分析和选择,因为我们的时间和精力都有限,所以把时间花在更有价值的地方。

                
            
        
浙公网安备 33010602011771号