注意
点击 此处 下载完整的示例代码
学习基础知识 || 快速入门 || 张量 || 数据集和数据加载器 || 转换 || 构建模型 || 自动微分 || 优化 || 保存和加载模型
转换¶
数据并不总是以机器学习算法训练所需的最终处理形式出现。我们使用转换对数据进行一些操作,使其适合训练。
所有 TorchVision 数据集都有两个参数 -transform
用于修改特征,target_transform
用于修改标签 - 它们接受包含转换逻辑的可调用对象。 torchvision.transforms 模块提供了许多常用的现成转换。
FashionMNIST 的特征采用 PIL Image 格式,标签为整数。为了进行训练,我们需要将特征转换为归一化的张量,并将标签转换为独热编码的张量。为了进行这些转换,我们使用 ToTensor
和 Lambda
。
import torch
from torchvision import datasets
from torchvision.transforms import ToTensor, Lambda
ds = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=ToTensor(),
target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1))
)
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to data/FashionMNIST/raw/train-images-idx3-ubyte.gz
0%| | 0.00/26.4M [00:00<?, ?B/s]
0%| | 65.5k/26.4M [00:00<01:12, 364kB/s]
1%| | 229k/26.4M [00:00<00:38, 683kB/s]
4%|3 | 950k/26.4M [00:00<00:11, 2.19MB/s]
15%|#4 | 3.83M/26.4M [00:00<00:02, 7.62MB/s]
38%|###7 | 10.0M/26.4M [00:00<00:00, 17.2MB/s]
62%|######1 | 16.3M/26.4M [00:01<00:00, 23.1MB/s]
85%|########5 | 22.5M/26.4M [00:01<00:00, 26.8MB/s]
100%|##########| 26.4M/26.4M [00:01<00:00, 19.4MB/s]
Extracting data/FashionMNIST/raw/train-images-idx3-ubyte.gz to data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to data/FashionMNIST/raw/train-labels-idx1-ubyte.gz
0%| | 0.00/29.5k [00:00<?, ?B/s]
100%|##########| 29.5k/29.5k [00:00<00:00, 326kB/s]
Extracting data/FashionMNIST/raw/train-labels-idx1-ubyte.gz to data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz
0%| | 0.00/4.42M [00:00<?, ?B/s]
1%|1 | 65.5k/4.42M [00:00<00:12, 361kB/s]
5%|5 | 229k/4.42M [00:00<00:06, 679kB/s]
20%|## | 885k/4.42M [00:00<00:01, 2.03MB/s]
63%|######2 | 2.79M/4.42M [00:00<00:00, 6.52MB/s]
100%|##########| 4.42M/4.42M [00:00<00:00, 6.07MB/s]
Extracting data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz to data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz
0%| | 0.00/5.15k [00:00<?, ?B/s]
100%|##########| 5.15k/5.15k [00:00<00:00, 41.4MB/s]
Extracting data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to data/FashionMNIST/raw
Lambda 转换¶
Lambda 变换应用任何用户定义的 lambda 函数。这里,我们定义了一个函数将整数转换为独热编码张量。它首先创建一个大小为 10 的零张量(数据集中的标签数量),并调用 scatter_,该函数在标签 y
给定的索引处赋值 value=1
。
target_transform = Lambda(lambda y: torch.zeros(
10, dtype=torch.float).scatter_(dim=0, index=torch.tensor(y), value=1))