PyTorch 读取大数据
生活随笔
收集整理的這篇文章主要介紹了
PyTorch 读取大数据
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
PyTorch 讀取大數(shù)據(jù)
數(shù)據(jù)量太大,必須分批從磁盤加載,下面是單機單卡的思路:
from torch.utils.data import Dataset, DataLoader import torchclass PretrainData(Dataset):def __init__(self):'''假設data是個數(shù)據(jù)量很大的文件,每次只能從內(nèi)存中加載3條數(shù)據(jù),后續(xù)可以把data放在odps_batch_data中改寫成從文件中讀數(shù)據(jù)'''self.meta_list = []self.shift = 0self.odps_batch = 3self.data = [[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5], [6, 6, 6], [7, 7, 7], [8, 8, 8]]self.datalength = len(self.data)def __len__(self):if len(self.meta_list) == 0:return self.odps_batchelse:return len(self.meta_list)def __getitem__(self, idx):return self.meta_list[idx]def get_odps_batch_data(self):'''通過偏移量shift和大小odps_batch,來從表中讀數(shù)據(jù)'''if self.shift + self.odps_batch < self.datalength:self.meta_list = torch.tensor(self.data[self.shift:self.shift + self.odps_batch])else:self.meta_list = torch.tensor(self.data[self.shift:])print("self.meta_list:", self.meta_list)if __name__ == "__main__":dataset = PretrainData()dataloader = DataLoader(dataset, batch_size=2, shuffle=True, drop_last=False)for epoch in range(3):for shift in range(dataset.datalength // dataset.odps_batch + 1):dataset.shift = shift * dataset.odps_batchdataloader.dataset.get_odps_batch_data()print(len(dataloader))for data in dataloader:print("epoch:", epoch)print("data:", data)user = data[:, 0]item = data[:, 1]click = data[:, 2]print(user, item, click)總結(jié)
以上是生活随笔為你收集整理的PyTorch 读取大数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【LeetCode】3月29日打卡-Da
- 下一篇: 简单讲述一下Intent的传值过程