目录

  • torch.Tensor详解
    • 参考:
    • 提供的数据类型:
    • 初始化:
      • 2、设置类型和设备
      • 3、可以使用 Python 的索引和切片符号访问和修改张量的内容
      • 4、使用 torch.Tensor.item() 从包含单个值的张量中获取 Python 数字:
      • 5、requires_grad=True自动梯度计算

torch.Tensor详解

参考:

torch.Tensor — PyTorch 1.12 documentation

提供的数据类型:

Data type dtype CPU tensor GPU tensor
32-bit floating point torch.float32 or torch.float torch.FloatTensor torch.cuda.FloatTensor
64-bit floating point torch.float64 or torch.double torch.DoubleTensor torch.cuda.DoubleTensor
16-bit floating point [1] torch.float16 or torch.half torch.HalfTensor torch.cuda.HalfTensor
16-bit floating point [2] torch.bfloat16 torch.BFloat16Tensor torch.cuda.BFloat16Tensor
32-bit complex torch.complex32 or torch.chalf
64-bit complex torch.complex64 or torch.cfloat
128-bit complex torch.complex128 or torch.cdouble
8-bit integer (unsigned) torch.uint8 torch.ByteTensor torch.cuda.ByteTensor
8-bit integer (signed) torch.int8 torch.CharTensor torch.cuda.CharTensor
16-bit integer (signed) torch.int16 or torch.short torch.ShortTensor torch.cuda.ShortTensor
32-bit integer (signed) torch.int32 or torch.int torch.IntTensor torch.cuda.IntTensor
64-bit integer (signed) torch.int64 or torch.long torch.LongTensor torch.cuda.LongTensor
Boolean torch.bool torch.BoolTensor torch.cuda.BoolTensor
quantized 8-bit integer (unsigned) torch.quint8 torch.ByteTensor /
quantized 8-bit integer (signed) torch.qint8 torch.CharTensor /
quantized 32-bit integer (signed) torch.qint32 torch.IntTensor /
quantized 4-bit integer (unsigned) torch.quint4x2 torch.ByteTensor /

除了编码常见的类型,还有几种不常见的类型:

1、16-bit floating point[1]:使用 1 个符号、5 个指数和 10 个有效位。 当精度很重要以牺牲范围为代价时很有用。

2、16-bit floating point[2]:使用 1 个符号、8 个指数和 7 个有效位。 当范围很重要时很有用,因为它具有与 float32 相同数量的指数位

3、quantized 4-bit integer (unsigned):量化的 4 位整数存储为 8 位有符号整数。 目前仅在 EmbeddingBag 运算符中支持。

不指定类型的话,默认是:torch.FloatTensor

初始化:

1、使用列表或者序列:

import torchtorch.tensor([[1., -1.], [1., -1.]])
torch.tensor(np.array([[1, 2, 3], [4, 5, 6]]))

注意:torch.tensor会拷贝数据,如果在改变requires_grad时避免拷贝数据,需要使用requires_grad_() or detach()。如果在使用numpy array初始化想避免拷贝,需要使用torch.as_tensor()

2、设置类型和设备
>>> torch.zeros([2, 4], dtype=torch.int32)
tensor([[ 0,  0,  0,  0],[ 0,  0,  0,  0]], dtype=torch.int32)
>>> cuda0 = torch.device('cuda:0')
>>> torch.ones([2, 4], dtype=torch.float64, device=cuda0)
tensor([[ 1.0000,  1.0000,  1.0000,  1.0000],[ 1.0000,  1.0000,  1.0000,  1.0000]], dtype=torch.float64, device='cuda:0')
3、可以使用 Python 的索引和切片符号访问和修改张量的内容
>>> x = torch.tensor([[1, 2, 3], [4, 5, 6]])
>>> print(x[1][2])
tensor(6)
>>> x[0][1] = 8
>>> print(x)
tensor([[ 1,  8,  3],[ 4,  5,  6]])
4、使用 torch.Tensor.item() 从包含单个值的张量中获取 Python 数字:
>>> x = torch.tensor([[1]])
>>> x
tensor([[ 1]])
>>> x.item()
1
>>> x = torch.tensor(2.5)
>>> x
tensor(2.5000)
>>> x.item()
2.5
5、requires_grad=True自动梯度计算
>>> x = torch.tensor([[1., -1.], [1., 1.]], requires_grad=True)
>>> out = x.pow(2).sum()
>>> out.backward()
>>> x.grad
tensor([[ 2.0000, -2.0000],[ 2.0000,  2.0000]])

torch.Tensor详解相关推荐

  1. 【PyTorch系例】torch.Tensor详解和常用操作

    学习教材: 动手学深度学习 PYTORCH 版(DEMO) (https://github.com/ShusenTang/Dive-into-DL-PyTorch) PDF 制作by [Marcus ...

  2. 2021 PyTorch官方实战教程(一)Tensor 详解

    点击上方"AI算法与图像处理",选择加"星标"或"置顶"重磅干货,第一时间送达 这个系列时pytorch官方实战教程,后续会继续更新.. 一 ...

  3. torch.unsqueeze和 torch.squeeze() 详解

    1. torch.unsqueeze 详解 torch.unsqueeze(input, dim, out=None) 作用:扩展维度 返回一个新的张量,对输入的既定位置插入维度 1 注意: 返回张量 ...

  4. torch.roll() 详解

    torch.roll(input, shifts, dims=None) input (Tensor) – the input tensor. shifts (int or tuple of pyth ...

  5. pytorch拼接函数:torch.stack()和torch.cat()--详解及例子

    原文链接: https://blog.csdn.net/xinjieyuan/article/details/105205326 https://blog.csdn.net/xinjieyuan/ar ...

  6. torch unsqueeze()详解

    Torch官网解释: torch.unsqueeze(input, dim) → Tensor Returns a new tensor with a dimension of size one in ...

  7. 【torch.argmax与torch.max详解】

    Pytorch常用函数 一.torch.max 1.调用方式 2.相关介绍 3.代码实例及图示理解 二.torch.argmax 1.调用方式 2.相关介绍 3.代码实例及图示理解 三.torch.m ...

  8. pytorch稀疏张量模块torch.sparse详解

      torch.sparse是一个专门处理稀疏张量的模块.通常,张量会按一定的顺序连续地进行存取.但是,对于一个存在很多空值的稀疏张量来说,顺序存储的效率显得较为低下.因此,pytorch推出了稀疏张 ...

  9. Pytorch中, torch.einsum详解。

    爱因斯坦简记法:是一种由爱因斯坦提出的,对向量.矩阵.张量的求和运算的求和简记法. 在该简记法当中,省略掉的部分是:1)求和符号与2)求和号的下标 省略规则为:默认成对出现的下标(如下例1中的i和例2 ...

  10. torch.matmul() 详解

    最近在准备做 HW04,在读 transformer 的源码的时候发现 attention score 的 torch.matmul() 的奇妙设置,故有此篇文章进行分享. 前言碎碎念: 一开始我以为 ...

最新文章

  1. 国内ntp服务器ip地址
  2. JavaScript 变量克隆和判断变量类型
  3. ListView、AdapterView、RecyclerView全面解析
  4. 用ASP实现隐藏链接方法
  5. DataTable的Merge\COPY\AcceptChange使用说明
  6. 【C语言进阶】 宏定义实现字符串引用
  7. 一览众山小的上一句是什么,怎么理解一览众山小的意思?
  8. 基于Vue+nodejs+Web的网上书城系统
  9. [UEFI启动教程]移动硬盘安装U盘装机助理(双模式启动)
  10. python源代码程序编译后文件扩展名_Python源代码程序编译后的文件扩展名为_________。...
  11. 08cms房产门户系统v8.4升级补丁支持新版APP和小程序
  12. EMV(一):初步了解EMV和EMV的分层结构
  13. 脉脉热帖:学历、履历双造假,拿了抖音Offer
  14. 噪声来源、定义及影响【转自微信公众号微波射频网】
  15. 15年IT经验,如何从一无所有成为上市公司高层!
  16. EasyCVR添加萤石云SDK接入的设计与开发流程
  17. 图片加密(一)颜色加密
  18. 那些年,我们一起参加过的高考
  19. 信息技术教案 计算机病毒,四年级下信息技术教案-计算机病毒知识二辽师大版.docx...
  20. Android第一行代码——第八章多媒体

热门文章

  1. OpenStack开源云平台
  2. python---之struck.pack()和struct.unpack
  3. java对excel加密_随笔:Java 对Excel等文件进行加密、解密
  4. 华三交换机开机dhcp snooping
  5. 【Easyx库】(1)
  6. 动手学深度学习(pytorch)中d2lzh_pytorch包的安装(附资源)
  7. 解决win2003里IIS运行ASP时出现请求资源在使用中的问题
  8. 一、高并发秒杀API简介与业务分析
  9. word如何将选择题按首字母拼音排序
  10. MT【258】椭圆第三定义