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([
相关推荐
吴佳浩11 小时前
我和 rust-anything:AI 时代,重写代码不再靠“信心“,而靠“证据“
人工智能·rust·go
会编程的吕洞宾12 小时前
Spring AI 2.0 MCP 客户端实战:让 Java AI 应用接入现成工具
java·人工智能·经验分享·后端·spring
星恒随风14 小时前
从0实现C++ AI大模型接入SDK(八):SessionManager会话管理——智能指针、原子操作、线程安全与懒加载机制
linux·服务器·开发语言·c++·人工智能
XW96325814 小时前
除湿机行业组织能力与人才地图报告
大数据·服务器·网络·数据结构·数据库·人工智能
Csvn14 小时前
WorkBuddy 使用指南:上下文、记忆、检索与任务交接单
人工智能
Csvn14 小时前
WorkBuddy 使用指南:技能、专家、连接器
人工智能
AI创界者14 小时前
Python 进阶实战(七十一章):研发效能度量、自动化质量门禁与工程规范落地实战
人工智能·python·aigc
硅谷秋水14 小时前
Genesis World 1.0
机器学习·计算机视觉·机器人
yingjie11015 小时前
快榜GEO|官网 AI 可读性自查 12 项(附 Python 检测脚本)
开发语言·人工智能·python