官方网站进行查看DataLoader
batch_size 的含义
python
复制代码
import torchvision
from torch.utils.data import DataLoader
# 准备的测试数据集
test_data = torchvision.datasets.CIFAR10('D:\Pytorch\pythonProject\Transform\dataset', train=False, transform=torchvision.transforms.ToTensor())
test_loader = DataLoader(dataset=test_data, batch_size=4, shuffle=False, num_workers=0, drop_last=False)
# 测试数据集中第一张图片及target
img, target = test_data[0]
print(img.shape) # torch.Size([3, 32, 32])
print(target) # 3
for data in test_loader:
imgs, targets = data
print(imgs.shape) # torch.Size([4, 3, 32, 32]); 4就是batch_size, 3是通道, 32×32是图片大小
print(targets) # tensor([3, 8, 8, 0]); 4张图片的target
python
复制代码
import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
# 准备的测试数据集
test_data = torchvision.datasets.CIFAR10('D:\Pytorch\pythonProject\Transform\dataset', train=False, transform=torchvision.transforms.ToTensor())
test_loader = DataLoader(dataset=test_data, batch_size=64, shuffle=True, num_workers=0, drop_last=True)
# 测试数据集中第一张图片及target
img, target = test_data[0]
print(img.shape) # torch.Size([3, 32, 32])
print(target) # 3
writer = SummaryWriter('dataloader')
for epoch in range(2):
step = 0
for data in test_loader:
imgs, targets = data
# print(imgs.shape) # torch.Size([4, 3, 32, 32]); 4就是batch_size, 3是通道, 32×32是图片大小
# print(targets) # tensor([3, 8, 8, 0]); 4张图片的target
writer.add_images('Epoch: {}'.format(epoch), imgs, step)
step += 1
writer.close()
shuffle=True 的话,会随机成batch