本文主要是介绍深度学习(小土堆),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用transform
加载数据集,查看数据集的属性
将图片转换成tensor类型
dataset_transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor()
])train_set = torchvision.datasets.CIFAR10(root="./dataset",train=True,transform= dataset_transform,download=True)
test_set = torchvision.datasets.CIFAR10(root="./dataset",train=True,transform= dataset_transform,download=True)print(test_set[0])
将该数据的数据显示在tensorboard中
Dataloader
import torchvision
from torch.utils.data import DataLoader#准备测试数据集
test_data = torchvision.datasets.CIFAR10("./dataset",train=False,transform=torchvision.transforms.ToTensor(),download=True)test_loader = DataLoader(dataset = test_data,batch_size=4,shuffle=True,num_workers=0,drop_last=False)
#测试数据集中第一张图片集
img,target = test_data[0]
print(img.shape)
print(target)for data in test_loader:imgs,targets = dataprint(imgs.shape)print(targets)
出现以上问题,需要将numberworks设置为0
drop_last 当取数据有余数时,是舍去还是保留
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=True)
#测试数据集中第一张图片集
img,target = test_data[0]
print(img.shape)
print(target)writer = SummaryWriter("DataLodaer")#shuffle 为True 两次结果不一样
for epoch in range(2):step = 0for data in test_loader:imgs,targets = data# print(imgs.shape)# print(targets)writer.add_images("Epoch:{}".format(epoch),imgs,step)step = step+1writer.close()
神经网络
这篇关于深度学习(小土堆)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!