PyTorch快速入门教程【小土堆】-DataLoader的使用

1.dataloder 介绍

数据加载器,把数据加载到神经网络中,每次从dataset中取数据,每次取多少,怎么取,由dataloder中的参数决定。

pytorch官方文档:https://pytorch.org/docs/stable/index.html

(1)dataloader参数介绍

  • dataset (Dataset) – dataset from which to load the data.---数据集位置和信息

  • batch_size (intoptional) – how many samples per batch to load (default: 1).

  •  shuffle (booloptional) – set to True to have the data reshuffled at every epoch (default: False).---为true重新整理数据,false不重新整理,默认false

  • sampler (Sampler or Iterableoptional) – defines the strategy to draw samples from the dataset. Can be any Iterable with __len__ implemented. If specified, shuffle must not be specified.

  • batch_sampler (Sampler or Iterableoptional) – like sampler, but returns a batch of indices at a time. Mutually exclusive with batch_sizeshufflesampler, and drop_last.

  • num_workers (intoptional) – how many subprocesses to use for data loading. 0 means that the data will be loaded in the main process. (default: 0)---加载数据采用多进程还是单进程,默认0代表采用主进程加载

  • collate_fn (callableoptional) – merges a list of samples to form a mini-batch of Tensor(s). Used when using batched loading from a map-style dataset.

  • pin_memory (booloptional) – If True, the data loader will copy Tensors into CUDA pinned memory before returning them. If your data elements are a custom type, or your collate_fn returns a batch that is a custom type, see the example below.

  • drop_last (booloptional) – set to True to drop the last incomplete batch, if the dataset size is not divisible by the batch size. If False and the size of dataset is not divisible by the batch size, then the last batch will be smaller. (default: False)--true,删除最后未整除的,

 drop_last如果数据集大小不能被批处理大小整除,则设置为True可删除最后一个未完成的批处理。 如果为False且数据集的大小不能被批处理大小整除,则最后一批将更小。 (默认值:False)

(2)查看返回值 

 

注:如果tensorboard里面图片显示不出来或者显示不全,建议删掉log文件夹重新运行

2.代码实现

import torchvision

from torch.utils.data import DataLoader

from torch.utils.tensorboard import SummaryWriter

#准备的测试数据集
test_data=torchvision.datasets.CIFAR10("./dataset",train=False,transform=torchvision.transforms.ToTensor())
# test_loader=DataLoader(dataset=test_data,batch_size=64,shuffle=True,num_workers=0,drop_last=False)
# test_loader=DataLoader(dataset=test_data,batch_size=64,shuffle=True,num_workers=0,drop_last=True)
test_loader=DataLoader(dataset=test_data,batch_size=64,shuffle=True,num_workers=0,drop_last=True)
#batch_size=4 每次从test_data中随机抓取4个数据
#测试数据集中第一张图片及target
img,target=test_data[0]
print(img.shape)
print(target)
writer=SummaryWriter("dataloader")
for epoch in range(2):
    step=0
    for data in test_loader:
        imgs,targets=data
        # print(imgs.shape)
        # print(targets)
        #writer.add_images("test_data",imgs,step)# drop_last=False 最后保留
        #writer.add_images("test_data_drop_last", imgs, step) #drop_last=true 最后不保留
        writer.add_images("Epoch:{}".format(epoch), imgs, step)
        step=step+1
writer.close()

3.运行结果

 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值