pytorch基础: Tensor数据类型与基础函数

本文介绍了PyTorch中的Tensor数据类型,包括常用的浮点型和整型,以及如何根据需求选择类型。讲解了Tensor的创建与查看,如基础tensor函数和类numpy方法。详细阐述了Tensor的组合、分块、索引和维度变形操作,并探讨了内存共享,如Tensor与Numpy的转换。此外,还提到了数据类型转换的函数和注意事项。
摘要由CSDN通过智能技术生成

1. Tensor的数据类型

在PyTorch中,主要有10种类型的tensor,其中重点使用的为以下八种(还有BoolTensor和BFloat16Tensor):

Data type dtype dtype
32-bit floating point torch.float32 or torch.float torch.(cuda).FloatTensor
64-bit floating point torch.float64 or torch.double torch.(cuda).DoubleTensor
16-bit floating point torch.float16 or torch.half torch.(cuda).HalfTensor
8-bit integer(unsigned) torch.uint8 torch.(cuda).ByteTensor
8-bit integer(signed) torch.int8 torch. (cuda). CharTensor
16-bit integer(signed) torch.int16 or torch.short torch.(cuda).ShortTensor
32-bit integer(signed) torch.int32 or torch.int torch.(cuda).IntTensor
64-bit integer(signed) torch.int64 or torch.long torch.(cuda).LongTensor

在具体使用时可以根据网络模型所需的精度和显存容量进行选取。

  • 一般情况而言,模型参数和训练数据都是采用默认的32位浮点型;16位半精度浮点是为在GPU上运行的模型所设计的,因为这样可以尽可能节省GPU的显存占用。
    当然这样节省显存空间也会缩小所能表达数据的能力。因此自pytorch1.6自动混合精度(automatic mixed precision)被更新后,将半精度和单精度混合使用以达到减少显存、提升推理速度的方法就被大面积的推广开来。在这里不对自动混合精度(AMP)模块做过多介绍。

  • 训练用的标签一般是整型中的32或64位,而在硬盘中存储数据集文件时则一般采用uint8来存分类标签(除非超过了255类),总之就是尽可能在不损失信息的情况下压缩空间。

对于tensor之间的类型转换,可以通过type(),type_as(),int()等进行操作。其中不传入dtype参数的type()函数为查看当前类型,传入参数则是转换为该参数代表的类型。

# 创建新的Tensor时默认类型为float32位
>>> a = torch.randn(2, 2)
>>> a.type()
'torch.FloatTensor'

# 使用int()、float()、double()等进行数据转换
>>> b = a.double()
>>> b
tensor([[ 0.1975, -0.3009],
        [ 1.7323, -0.4336]], dtype=torch.float64)

# 使用传入dtype参数的type()函数
>>> a.type(torch.IntTensor)        
tensor([[0, 0],
        [1, 0]], dtype=torch.int32)

# 使用type_as()函数,将a的类型转换为b的类型
>>> a.type_as(b)
tensor([[ 0.1975, -0.3009],
        [ 1.7323, -0.4336]], dtype=torch.float64)

注意这里提到默认类型为float32,但是在使用from_numpy()函数时创建的tensor将会和原本的ndarray的类型保持一致,这个问题将在下一节具体讨论。

值得一提的是type_as()函数非常方便,在实际建立模型的过程中经常需要保持tensor之间的类型一致,我们只需要使用type_as()即可,不需要操心具体是什么类型。

2. Tensor的创建与查看

Tensor有很多创建方式,最常用基本的就是tensor(),而构造函数Tensor()用的很少。同时也有很多和numpy类似的创建方式,比如ones()zeors()randn()等等。
接下来我用代码的方式来介绍常见的创建方式,以及一些容易混淆的情况。

本节涉及函数

2.1. 基础tensor函数

tensor()是最常使用的,从data参数来构造一个新的tensor,下为官方文档的介绍

data (array_like) – Initial data for the tensor. Can be a list, tuple, NumPy ndarray, scalar, and other types.
基本上任何矩阵模样的数据都可通过tensor()被转换为tensor

Tensor()是最原始的构造函数,不建议使用

# Tensor()参数为每一维大小,得到数据为随机初始化
>>> torch.Tensor(2,2)
tensor([[0.0000, 4.4766],
        [0.0000, 0.0000]])

# tensor()的常见用法和特殊情况
>>> torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])
tensor([[ 0.1000,  1.2000],
        [ 2.2000,  3.1000],
        [ 4.9000,  5.2000]])

>>> torch.tensor([0, 1])  # 会自行类型推断,创建int类型
tensor([ 0,  1])

>>> torch.tensor([])  # 创建一个空tensor (size (0,))
tensor([])

使用Tensor内置的各种数据类型进行创建

torch.DoubleTensor(2, 2)

2.2. 类numpy方法

2.2.1. 特殊矩阵创建方法
>>> torch.zeros(2,2)# 全为0的矩阵
>>> torch.ones(2,2) # 全为1的矩阵
>>> torch.eye(2,2) # 单位矩阵
2.2.2. 随机矩阵创建方法
# 按照所给维度创建矩阵,用标准正态分布(N(0,1))中的随机数来填充
>>> torch.randn(2,2)
tensor([[ 0.9798,  0.4567],
        [-0.4731, -0.3492]])

# 和randn一样,但是这次是用[0,1]均匀分布中的随机数来填充
>>> torch.rand(2,2)
tensor([[0.4497, 0.3038],
        [0.1431, 0.0814]])

# 生成长度为n的随机排列向量
>>> torch.randperm(5)
tensor([0, 4, 1, 3, 2])

# 用0-n的随机整数来填充矩阵,第二个参数为要求的维度
# 此为[0-4]的整数
>>> torch.randint(4, (2,3))
tensor([[2, 0, 3],
        [0, 0, 1]])
2.2.3. like方法

按照所给tensor维度生成相同维度的目标矩阵,这里就只举一个例子好了

>>> a = torch.randn(2, 3)
>>> torch.ones_like(a)
tensor([[1., 1., 1.],
        [1., 1., 1.]])
2.2.4. 序列创建方法

按照所给区间创建各种序列

注:range()函数是deprecated状态,不在此介绍

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值