.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "beginner/basics/transforms_tutorial.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_beginner_basics_transforms_tutorial.py: `基础知识 `_ || `快速入门 `_ || `张量 `_ || `数据集与数据加载器 `_ || **Transforms** || `构建神经网络 `_ || `自动微分 `_ || `优化模型参数 `_ || `保存和加载模型 `_ Transforms ========== 数据并不总是以训练机器学习算法所需的最终处理形式呈现。我们使用**transforms**来对数据进行一些处理,使其适用于训练。 所有 TorchVision 数据集都有两个参数 - `transform` 用于修改特征,`target_transform` 用于修改标签 - 它们接受包含转换逻辑的可调用对象。`torchvision.transforms `_ 模块提供了几种常用的转换。 FashionMNIST 的特征是以 PIL 图像格式呈现的,标签是整数。对于训练,我们需要将特征转换为归一化的张量, 将标签转换为编码的张量。为了进行这些转换,我们使用了 ``ToTensor`` 和 ``Lambda``。 .. GENERATED FROM PYTHON SOURCE LINES 23-37 .. code-block:: default 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)) ) .. GENERATED FROM PYTHON SOURCE LINES 38-43 ToTensor() ------------------------------- `ToTensor `_ 将 PIL 图像或 NumPy ``ndarray`` 转换为 ``FloatTensor``,并将图像的像素强度值缩放到范围 [0., 1.]。 .. GENERATED FROM PYTHON SOURCE LINES 45-51 Lambda Transforms ------------------------------- Lambda transforms 应用任何用户定义的 lambda 函数。这里,我们定义一个函数将整数转换为独热编码的张量。 它首先创建一个大小为 10(我们数据集中标签的数量)的零张量,然后调用 `scatter_ `_, 在由标签 ``y`` 指定的索引上赋值为 ``1``。 .. GENERATED FROM PYTHON SOURCE LINES 51-55 .. code-block:: default target_transform = Lambda(lambda y: torch.zeros( 10, dtype=torch.float).scatter_(dim=0, index=torch.tensor(y), value=1)) .. GENERATED FROM PYTHON SOURCE LINES 56-58 -------------- .. GENERATED FROM PYTHON SOURCE LINES 60-63 延伸阅读 ~~~~~~~~~~~~~~~~~ - `torchvision.transforms API `_ .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_beginner_basics_transforms_tutorial.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: transforms_tutorial.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: transforms_tutorial.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_