1.Dataset是一个包装类,用来将数据包装为Dataset类,然后传入DataLoader中,我们再使用DataLoader这个类来更加快捷的对数据进行操作。
2.DataLoader是一个比较重要的类,它为我们提供的常用操作有:batch_size(每个batch的大小), shuffle(是否进行shuffle操作), num_workers(加载数据的时候使用几个子进程)
1.使用torch定义的DataLoader
import torch
from torch.utils.data import DataLoader,Dataset,TensorDataset
features = torch.zeros([40,2],dtype = torch.float32)
labels = torch.zeros([40,1])
dataset = TensorDataset(features,labels)
dataloader = DataLoader(dataset,batch_size=3,shuffle=True,num_workers=2)
for t,(X,Y) in enumerate(dataloader):
# X.shape:[batch_size,num_features]
# Y.shape:[batch_size,1]
print('第',t,'个batch_size,X.shape:',X.shape,'Y.shape:',Y.shape)
输出:
2.使用自定义DataLoader
# 自定义dataloader
class XiaoHeiLoader(Dataset):
def __init__(self,x,y):
self.X = x
self.Y = y
def __getitem__(self,index): # 通过对象下标访问元素
return self.X[index],self.Y[index]
def __len__(self): # 能够使用len()能返回序列长度
return self.Y.shape[0]
features = torch.zeros([40,2],dtype = torch.float32)
labels = torch.zeros([40,1])
loader = XiaoHeiLoader(features,labels)
dataloader = DataLoader(loader,batch_size=2,shuffle = True)
print(len(loader))
print(loader[1])
for t,(X,Y) in enumerate(dataloader):
# X.shape:[batch_size,num_features]
# Y.shape:[batch_size,1]
print('第',t,'个batch_size,X.shape:',X.shape,'Y.shape:',Y.shape)
参考博客:https://blog.csdn.net/zw__chen/article/details/82806900