PyTorch-Tutorials【pytorch官方教程中英文详解】- 4 Transforms

72 篇文章 28 订阅
28 篇文章 17 订阅

上篇文章PyTorch-Tutorials【pytorch官方教程中英文详解】- 3 Datasets&DataLoaders介绍了pytorch中读取数据集的Datasets&DataLoaders类,下面进一步看看如何对数据进行转换。

原文链接:Transforms — PyTorch Tutorials 1.10.1+cu102 documentation

Data does not always come in its final processed form that is required for training machine learning algorithms. We use transforms to perform some manipulation of the data and make it suitable for training.

【数据并不总是以训练机器学习算法所需的最终处理形式出现。我们使用转换来对数据执行一些操作,并使其适合于训练。】

All TorchVision datasets have two parameters -transform to modify the features and target_transform to modify the labels - that accept callables containing the transformation logic. The torchvision.transforms module offers several commonly-used transforms out of the box.

【所有的TorchVision数据集都有两个参数——transform用来修改特征,target_transform用来修改标签——它们接受包含转换逻辑的可调用对象。torchvision.transforms模块提供了几个常用的开箱即用转换。】

The FashionMNIST features are in PIL Image format, and the labels are integers. For training, we need the features as normalized tensors, and the labels as one-hot encoded tensors. To make these transformations, we use ToTensor and Lambda.

【FashionMNIST特征采用PIL Image格式,标签为整数。在训练中,我们需要特征是标准化张量,标签是one-hot编码张量。为了做这些变换,我们使用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进度,但是因为之前已经下载过,我运行之后没有任何反应。

1 ToTensor()

ToTensor converts a PIL image or NumPy ndarray into a FloatTensor. and scales the image’s pixel intensity values in the range [0., 1.]

ToTensor将PIL图像或NumPy ndarray转换为FloatTensor,并缩放图像的像素强度值在范围[0,1)内。】

2 Lambda Transforms

Lambda transforms apply any user-defined lambda function. Here, we define a function to turn the integer into a one-hot encoded tensor. It first creates a zero tensor of size 10 (the number of labels in our dataset) and calls scatter_ which assigns a value=1 on the index as given by the label y.

【λ转换应用任何用户定义的Lambda函数。在这里,我们定义了一个函数来将整数转换为一个热编码张量。它首先创建一个0张量,大小为10(我们的数据集中标签的数量),然后调用scatter_,它为标签y给定的索引赋值=1。】

3 Further Reading

说明:记录学习笔记,如果错误欢迎指正!写文章不易,转载请联系我。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值