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([
相关推荐
Dmz丶1 小时前
前端转 AI 100 天 Day 27:Docker 容器化——从“我电脑能跑“到“到处能跑“
python
卿卿的产品经理日记1 小时前
【AI产品经理实战】Day 38|try-except 踩坑实录:报错是数据
人工智能·笔记·python·学习·产品经理
cxj75141 小时前
GEO 工程化实践:关键页面的快慢双轨更新与多引擎引用维护
人工智能·geo·geo优化
Java_AI工程师1 小时前
Spring4 / SpringBoot1.x 老项目想接入 AI?不用升级框架!3 套无痛落地方案
java·人工智能·spring boot
znnnk1 小时前
【AI技术】Vibe Coding 实践指南:从项目规划到高效开发
人工智能·ai·ai编程·ai ide·vibe coding
Simon_lca1 小时前
ZARA、H&M、SHEIN验厂差异全对比:一套底层记录,三套接口输出
大数据·人工智能·经验分享·科技·制造
全栈练习生1 小时前
文档智能与实践
python·ai
爱喝雪碧的可乐1 小时前
DeepSeek 崩了近5小时:一次故障扒透「单点依赖、重试风暴与大模型高可用」
大数据·人工智能·deepseek
GlobalTradercrb1 小时前
独立站SEO先写博客还是先优化产品页?看站点阶段
数据库·人工智能·google·pdf·go·seo