首页 > 其他分享 >3_transforms (pytorch tutorial)

3_transforms (pytorch tutorial)

时间:2023-04-23 10:11:12浏览次数:45  
标签:tensor torch transform ToTensor pytorch transforms tutorial Lambda

Transforms

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.

数据不总是以被处理好只需要机器学习的算法形式出现。我们使用transforms来操作数据,让它适合被训练。

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数据集都有2个参数transform调整特征,target_transform调整标签(在transformation逻辑下可调用的)。 torchvision.transforms模块提供几个常用的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.

FashionMUIST特征是PIL数据格式,标签是整数。为了训练,我们需要特征变成归一化的tensors,标签变成one-hot编码的tensors。为了实现这些transformation,我们使用ToTensorLamba

one-hot:就是把一列类别ABCD变成一行表头isA isB isC isD,然后每一列就是1代表是A,0就是不是A,这样就避免了潜在的数字关系

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))
)

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数组转换成FloatTensor,并把图像每个像素的值归一化到[0., 1.]之间

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 transforms应用任何用户定义的lambda函数。这里我们定义了一个函数把整数转化成一个one-hot编码的tensor。它首先创建一个都是size为10(我们数据集里的标签个数)0的tensor,然后调用scatter_根据标签y把对应索引的值变成1。

target_transform = Lambda(lambda y: torch.zeros(
    10, dtype=torch.float).scatter_(dim=0, index=torch.tensor(y), value=1))

Further Reading

标签:tensor,torch,transform,ToTensor,pytorch,transforms,tutorial,Lambda
From: https://www.cnblogs.com/Berthe/p/17345666.html

相关文章

  • pytorch在有限的资源下部署大语言模型(以ChatGLM-6B为例)
    pytorch在有限的资源下部署大语言模型(以ChatGLM-6B为例)Part1知识准备在PyTorch中加载预训练的模型时,通常的工作流程是这样的:my_model = ModelClass(...)state_dict =torch.load(checkpoint_file)用简单的话来说,这些步骤是:用随机初始化的权重创建模型。从磁盘上加载模型权......
  • LearnETutorial 中文系列教程【翻译完成】
    在线阅读在线阅读(Gitee)ApacheCN学习资源目录人工智能中文教程C中文教程Golang中文教程HTML中文教程Java中文教程机器学习中文教程PHP中文教程Python中文教程R中文教程网络安全中文教程贡献指南本项目需要校对,欢迎大家提交PullRequest。请您勇敢地去翻译和改进翻译。虽然......
  • 【pytorch】土堆pytorch教程学习(一)环境配置及安装
    管理虚拟环境不同的项目需要不同的环境,Anaconda集成的conda包可以创建不同的环境并进行隔离。打开AnacondaPromp,创建环境,其中pytorch为创建的环境名:condacreate-n虚拟环境名python=版本启动环境condaactivate虚拟环境名移除虚拟环境condaremove-n虚拟环境名......
  • PyTorch项目实战05——Tensor的广播机制
    1广播机制介绍矩阵运算,往往只能在两个矩阵维度相同或者相匹配时才能运算。比如加减法需要两个矩阵的维度相同,乘法需要前一个矩阵的列数与后一个矩阵的行数相等。当参与运算的两个维度不同也不匹配的矩阵进行运算时,该机制会对数组进行扩展,使数组的shape属性值一样,这样,就可以进行矢......
  • Yolov5_DeepSort_Pytorch:基于 Yolov5 + Deep Sort 的实时多目标跟踪器
    Yolov5_DeepSort_Pytorch:基于Yolov5+DeepSort的实时多目标跟踪器 视界君 Python视界 昨天Python视界分享简介该存储库包含一个两阶段跟踪器。YOLOv5(一系列在COCO数据集上预训练的对象检测架构和模型)生成的检测被传递到跟踪对象的DeepSort算法。它可以跟踪Yolov5模型......
  • pytorch_ssd 代码注释
    box_utils.py#-*-coding:utf-8-*-importtorchdefpoint_form(boxes):"""Convertprior_boxesto(xmin,ymin,xmax,ymax)representationforcomparisontopointformgroundtruthdata.Args:boxes:(tensor)center-s......
  • N1、Pytorch文本分类入门
    一、背景介绍¶本节是一个使用PyTorch实现的简单文本分类实战案例。在这个例子中,我们将使用AGNews数据集进行文本分类。AGNews(AG'sNewsTopicClassificationDataset)是一个广泛用于文本分类任务的数据集,尤其是在新闻领域。该数据集是由AG'sCorpusofNewsArticles收......
  • pytorch训练可视化包visdom的使用
    visdom的使用一、简介二、安装与启动三、设计思路四、在迭代训练中的绘图五、一般绘图六、基本调整与保存文件1、`调整大小、拖拽`:长按拖动即可,右下角落长按移动即可放大缩小2、`查看图片对应的数据`:点击右下角`Edit`,会跳转到新网页,可以灵活进行数据调整3、`图内控件调整`:比如,放大......
  • scrapy startproject tutorial 这句话在哪输入cmd?
    大家好,我是皮皮。一、前言前几天在Python钻石交流群【未央.】问了一个Python网络爬虫的问题,这里拿出来给大家分享下。课程截图如下:官网的截图如下:二、实现过程这里【甯同学】给了提示,不过对于新手来说,还是不太容易上手的。进入终端之后,我们再启动项目,如下:正常来说,这样就可以启动成......
  • 深度学习--PyTorch定义Tensor以及索引和切片
    深度学习--PyTorch定义Tensor一、创建Tensor1.1未初始化的方法​ 这些方法只是开辟了空间,所附的初始值(非常大,非常小,0),后面还需要我们进行数据的存入。torch.empty():返回一个没有初始化的Tensor,默认是FloatTensor类型。#torch.empty(d1,d2,d3)函数输入的是shapetorch.empty......