Deep Learning with PyTorch
上QQ阅读APP看书,第一时间看更新

DataLoader class

The DataLoader class present in PyTorch's utils class combines a dataset object along with different samplers, such as SequentialSampler and RandomSampler, and provides us with a batch of images, either using a single or multi-process iterators. Samplers are different strategies for providing data to algorithms. The following is an example of a DataLoader for our Dogs vs. Cats dataset:

dataloader = DataLoader(dogsdset,batch_size=32,num_workers=2)
for imgs , labels in dataloader:
#Apply your DL on the dataset.
pass

imgs will contain a tensor of shape (32, 224, 224, 3), where 32 represents the batch size. 

The PyTorch team also maintains two useful libraries, called torchvision and torchtext, which are built on top of the Dataset and DataLoader classes. We will use them in the relevant chapters.