PyTorch_Dataloader\TensorBoard---数据加载与可视化

1. Dataloader 使用

在 PyTorch 中,数据加载主要依赖两个核心类:Dataset 与 DataLoader。

  • Dataset:负责告诉程序数据集存放在什么位置,以及如何通过索引(如索引 0)获取对应的数据样本。
  • DataLoader :负责将数据加载到神经网络中。它每次从 Dataset 中取数据,具体取多少、如何取,由 DataLoader 的参数决定。

1.1 代码示例

python 复制代码
import torchvision
from torch.utils.data import DataLoader

# 准备的测试数据集
test_data = torchvision.datasets.CIFAR10(
    "./dataset",
    train=False,
    transform=torchvision.transforms.ToTensor()
)

img, target = test_data[0]
print(img.shape)
print(img)

# batch_size=4 使得 img0, target0 = dataset[0]、img1, target1 = dataset[1]、
# img2, target2 = dataset[2]、img3, target3 = dataset[3],
# 然后这四个数据作为 Dataloader 的一个返回
test_loader = DataLoader(
    dataset=test_data,
    batch_size=4,
    shuffle=True,
    num_workers=0,
    drop_last=False
)

# 用 for 循环取出 DataLoader 打包好的四个数据
for data in test_loader:
    imgs, targets = data  # 每个 data 都是由 4 张图片组成,
                          # imgs.size 为 [4,3,32,32],四张 32×32 图片三通道,
                          # targets 由四个标签组成
    print(imgs.shape)
    print(targets)

1.2 运行结果

text 复制代码
torch.Size([3, 32, 32])
tensor([[[0.6196, 0.6235, 0.6471,  ..., 0.5373, 0.4941, 0.4549],
         [0.5961, 0.5922, 0.6235,  ..., 0.5333, 0.4902, 0.4667],
         [0.5922, 0.5922, 0.6196,  ..., 0.5451, 0.5098, 0.4706],
         ...,
         [0.2667, 0.1647, 0.1216,  ..., 0.1490, 0.0510, 0.1569],
         [0.2392, 0.1922, 0.1373,  ..., 0.1020, 0.1137, 0.0784],
         [0.2118, 0.2196, 0.1765,  ..., 0.0941, 0.1333, 0.0824]],
        [[0.4392, 0.4353, 0.4549,  ..., 0.3725, 0.3569, 0.3333],
         [0.4392, 0.4314, 0.4471,  ..., 0.3725, 0.3569, 0.3451],
         [0.4314, 0.4275, 0.4353,  ..., 0.3843, 0.3725, 0.3490],
         ...,
         [0.4863, 0.3922, 0.3451,  ..., 0.3804, 0.2510, 0.3333],
         [0.4549, 0.4000, 0.3333,  ..., 0.3216, 0.3216, 0.2510],
         [0.4196, 0.4118, 0.3490,  ..., 0.3020, 0.3294, 0.2627]],
        [[0.1922, 0.1843, 0.2000,  ..., 0.1412, 0.1412, 0.1294],
         [0.2000, 0.1569, 0.1765,  ..., 0.1216, 0.1255, 0.1333],
         [0.1843, 0.1294, 0.1412,  ..., 0.1333, 0.1333, 0.1294],
         ...,
         [0.6941, 0.5804, 0.5373,  ..., 0.5725, 0.4235, 0.4980],
         [0.6588, 0.5804, 0.5176,  ..., 0.5098, 0.4941, 0.4196],
         [0.6275, 0.5843, 0.5176,  ..., 0.4863, 0.5059, 0.4314]]])
torch.Size([4, 3, 32, 32])
tensor([2, 6, 4, 5])
torch.Size([4, 3, 32, 32])
tensor([7, 2, 1, 6])
torch.Size([4, 3, 32, 32])
tensor([6, 7, 4, 4])
torch.Size([4, 3, 32, 32])
tensor([9, 6, 1, 3])
torch.Size([4, 3, 32, 32])
tensor([1, 5, 8, 5])
torch.Size([4, 3, 32, 32])
tensor([1, 8, 4, 6])
torch.Size([4, 3, 32, 32])
tensor([9, 8, 7, 1])
torch.Size([4, 3, 32, 32])
tensor([6, 0, 9, 4])
torch.Size([4, 3, 32, 32])
tensor([7, 1, 9, 9])
torch.Size([4, 3, 32, 32])
tensor([6, 8, 7, 0])
torch.Size([4, 3, 32, 32])
tensor([6, 8, 1, 5])
torch.Size([4, 3, 32, 32])
tensor([5, 2, 7, 8])
torch.Size([4, 3, 32, 32])
tensor([7, 6, 7, 0])
torch.Size([4, 3, 32, 32])
tensor([9, 2, 0, 0])
torch.Size([4, 3, 32, 32])
tensor([0, 6, 0, 8])
torch.Size([4, 3, 32, 32])
tensor([6, 7, 3, 1])
torch.Size([4, 3, 32, 32])
tensor([3, 2, 6, 0])
torch.Size([4, 3, 32, 32])
tensor([3, 2, 6, 0])
torch.Size([4, 3, 32, 32])
tensor([9, 5, 0, 5])
torch.Size([4, 3, 32, 32])
tensor([5, 4, 2, 9])
torch.Size([4, 3, 32, 32])
tensor([9, 5, 9, 8])
torch.Size([4, 3, 32, 32])
tensor([3, 3, 8, 0])
torch.Size([4, 3, 32, 32])
tensor([2, 6, 1, 8])
torch.Size([4, 3, 32, 32])
tensor([1, 5, 6, 4])
torch.Size([4, 3, 32, 32])
tensor([9, 9, 6, 5])
torch.Size([4, 3, 32, 32])
tensor([7, 1, 2, 4])
torch.Size([4, 3, 32, 32])
tensor([1, 0, 8, 5])
torch.Size([4, 3, 32, 32])
tensor([2, 8, 1, 8])
torch.Size([4, 3, 32, 32])
tensor([7, 4, 6, 4])
torch.Size([4, 3, 32, 32])
tensor([5, 8, 5, 1])
torch.Size([4, 3, 32, 32])
tensor([8, 5, 9, 7])
torch.Size([4, 3, 32, 32])
tensor([2, 0, 9, 8])
torch.Size([4, 3, 32, 32])
tensor([2, 1, 4, 7])
torch.Size([4, 3, 32, 32])
tensor([9, 8, 1, 3])
torch.Size([4, 3, 32, 32])
tensor([4, 8, 1, 8])
torch.Size([4, 3, 32, 32])
tensor([5, 3, 8, 1])
torch.Size([4, 3, 32, 32])
tensor([2, 3, 7, 7])
torch.Size([4, 3, 32, 32])
tensor([6, 6, 8, 8])
torch.Size([4, 3, 32, 32])
tensor([4, 2, 7, 6])
torch.Size([4, 3, 32, 32])
tensor([2, 3, 1, 0])
torch.Size([4, 3, 32, 32])
tensor([0, 0, 5, 2])
torch.Size([4, 3, 32, 32])
tensor([0, 1, 1, 4])
torch.Size([4, 3, 32, 32])
tensor([1, 3, 2, 5])
torch.Size([4, 3, 32, 32])
tensor([2, 1, 1, 3])
torch.Size([4, 3, 32, 32])
tensor([5, 6, 1, 3])
torch.Size([4, 3, 32, 32])
tensor([8, 6, 2, 9])
torch.Size([4, 3, 32, 32])
tensor([5, 6, 4, 5])
torch.Size([4, 3, 32, 32])
tensor([9, 8, 5, 7])
torch.Size([4, 3, 32, 32])
tensor([9, 8, 5, 7])
torch.Size([4, 3, 32, 32])
tensor([6, 2, 8, 1])
torch.Size([4, 3, 32, 32])
tensor([6, 8, 7, 7])
torch.Size([4, 3, 32, 32])
tensor([1, 3, 6, 5])
torch.Size([4, 3, 32, 32])
tensor([6, 1, 8, 4])
torch.Size([4, 3, 32, 32])
tensor([1, 5, 7, 7])
torch.Size([4, 3, 32, 32])
tensor([5, 7, 1, 5])
torch.Size([4, 3, 32, 32])
tensor([5, 7, 7, 3])
torch.Size([4, 3, 32, 32])
tensor([1, 7, 4, 8])
torch.Size([4, 3, 32, 32])
tensor([2, 3, 0, 5])
torch.Size([4, 3, 32, 32])
tensor([6, 6, 7, 4])
torch.Size([4, 3, 32, 32])
tensor([2, 2, 7, 9])
torch.Size([4, 3, 32, 32])
tensor([8, 5, 8, 7])
torch.Size([4, 3, 32, 32])
tensor([5, 0, 8, 4])
torch.Size([4, 3, 32, 32])
tensor([4, 5, 9, 8])
torch.Size([4, 3, 32, 32])
tensor([6, 6, 7, 2])
torch.Size([4, 3, 32, 32])
tensor([6, 6, 1, 1])
torch.Size([4, 3, 32, 32])
tensor([2, 8, 3, 2])
torch.Size([4, 3, 32, 32])
tensor([7, 5, 5, 5])
torch.Size([4, 3, 32, 32])
tensor([1, 5, 5, 7])
torch.Size([4, 3, 32, 32])
tensor([0, 7, 5, 0])
torch.Size([4, 3, 32, 32])
tensor([9, 1, 4, 1])
torch.Size([4, 3, 32, 32])
tensor([8, 4, 6, 1])
torch.Size([4, 3, 32, 32])
tensor([3, 3, 0, 1])
torch.Size([4, 3, 32, 32])
tensor([7, 8, 6, 5])
torch.Size([4, 3, 32, 32])
tensor([3, 9, 6, 0])
torch.Size([4, 3, 32, 32])
tensor([9, 4, 5, 7])
torch.Size([4, 3, 32, 32])
tensor([0, 3, 4, 9])
torch.Size([4, 3, 32, 32])
tensor([1, 2, 8, 8])
torch.Size([4, 3, 32, 32])
tensor([7, 5, 0, 9])
torch.Size([4, 3, 32, 32])
tensor([3, 9, 3, 5])
torch.Size([4, 3, 32, 32])
tensor([3, 9, 3, 5])
torch.Size([4, 3, 32, 32])
tensor([0, 4, 3, 5])
torch.Size([4, 3, 32, 32])
tensor([6, 3, 5, 6])
torch.Size([4, 3, 32, 32])
tensor([2, 8, 8, 7])
torch.Size([4, 3, 32, 32])
tensor([6, 3, 4, 3])
torch.Size([4, 3, 32, 32])
tensor([7, 3, 2, 5])
torch.Size([4, 3, 32, 32])
tensor([1, 7, 5, 5])
torch.Size([4, 3, 32, 32])
tensor([3, 5, 2, 5])
torch.Size([4, 3, 32, 32])
tensor([3, 1, 5, 7])
torch.Size([4, 3, 32, 32])
tensor([0, 4, 7, 4])
torch.Size([4, 3, 32, 32])
tensor([6, 9, 9, 5])
torch.Size([4, 3, 32, 32])
tensor([8, 8, 6, 6])
torch.Size([4, 3, 32, 32])
tensor([0, 9, 3, 9])
torch.Size([4, 3, 32, 32])
tensor([4, 2, 2, 2])
torch.Size([4, 3, 32, 32])
tensor([5, 7, 6, 8])
torch.Size([4, 3, 32, 32])
tensor([8, 5, 2, 3])
torch.Size([4, 3, 32, 32])
tensor([1, 4, 3, 8])
torch.Size([4, 3, 32, 32])
tensor([8, 0, 3, 7])
torch.Size([4, 3, 32, 32])
tensor([2, 7, 1, 9])
torch.Size([4, 3, 32, 32])
tensor([1, 4, 4, 8])
torch.Size([4, 3, 32, 32])
tensor([0, 8, 6, 3])
torch.Size([4, 3, 32, 32])
tensor([6, 7, 2, 1])
torch.Size([
相关推荐
程序员果子18 分钟前
本体构建方法
人工智能·python·知识图谱·agent框架·本体
深蓝AI33 分钟前
Meta FAIR 开源 e-means:k-means 十分之一的轮数跑出更低损失,已并入 Faiss
人工智能·程序员
IT_陈寒35 分钟前
Vue这个奇葩的响应式问题让我加班到凌晨
前端·人工智能·后端
yunhuibin43 分钟前
AI分类任务种类
人工智能·自然语言处理·分类
东坡肘子1 小时前
参与学生移动应用大赛有感 -- 肘子的 Swift 周报 #161
人工智能·swiftui·swift
天天被压力1 小时前
【A股分钟级高频数据完全手册 #11】券商实时行情数据接口:实时交易(券商源)接入与字段
java·人工智能·python
唐老板1 小时前
agent 撑爆 Git 之后,两家改的不是同一层
人工智能·ai编程
总裁余(余登武)1 小时前
【python项目】:1操作EXCEl自动化开发
python·自动化·excel
芝梵家电数据笔记1 小时前
用字段表核对暖菜板长期使用条件:结构、暖区、质保边界
python·家电·数据校验·dataclass·选购