asp资源下载网站,软件定制开发企云云,龙岩网站建设要多少费用,网站建设和网页设计是不是一样1 张量的基本创建及其类型 和Numpy中的array一样#xff0c;张量的本质也是结构化地组织了大量的数据。 并且在实际操作中#xff0c;张量的创建和基本功能也与其非常类似。 1.1 张量(Tensor)函数创建方法
张量的最基本创建方法和Numpy中创建Array的格式一致。
# Numpy创建…1 张量的基本创建及其类型 和Numpy中的array一样张量的本质也是结构化地组织了大量的数据。 并且在实际操作中张量的创建和基本功能也与其非常类似。 1.1 张量(Tensor)函数创建方法
张量的最基本创建方法和Numpy中创建Array的格式一致。
# Numpy创建数组
import numpy as np #导入numpy
a np.array([1, 2, 3])import torch # 首次使用,导入torch包# 通过列表创建张量
t torch.tensor([1, 2])
t # output : tensor([1, 2])# 通过元组创建张量
t torch.tensor((1, 2))
t # output : tensor([1, 2])# 通过数组创建张量
a np.array([1, 2])
t torch.tensor(a)
t # output : tensor([1, 2],dtype torch.int32)通过上述返回结果我们发现张量也有dtype类型 1.2 张量的类型
张量和数组类似都拥有dtype方法可返回张量类型。
来看整型
# 整型数组的dtype方法
np.array([1, 2, 3]).dtype
# output : int32# 整型张量的dtype方法
torch.tensor([1, 2, 3]).dtype
# output : torch.int64不难发现整数型的数组默认创建int32整型类型而张量则默认创建int64长整型类型。 如果tensor中传入的参数是array数组类型那么它将会告诉你生成的是torch.int32类型
a np.array([1, 2])
t torch.tensor(a)
t # output : tensor([1, 2],dtype torch.int32)来看浮点型
# 浮点型数组的dtype方法
np.array([1.1, 2.2]).dtype
# output : float64# 浮点型张量的dtype方法
torch.tensor([1.1, 2.2]).dtype
# output : torch.float32不难发现浮点型的数组默认创建float64单精度浮点型而张量则默认创建float32双精度浮点型。 如果tensor中传入的是array数组类型那么它将会告诉你生成的是torch.float64类型
a np.array([1.1, 2.2])
t torch.tensor(a)
t # output : tensor([1.1000, 2.2000],dtype torch.float64)除了数值型张量常用的张量类型还有布尔型张量也就是构成张量的各元素都是布尔类型的张量。
ttorch.tensor([True,False])
t.dtype
# output : torch.bool当然在PyTorch中也支持复数类型对象创建
t torch.tensor(1 2j) # 1是实部、2是虚部
t # output : tensor(1.2.j)此外我们还可以通过dtype参数在创建张量过程中设置输出结果
# 创建int16整型张量
torch.tensor([1.1, 2.7],dtype torch.int16)
# output : tensor([1, 2], dtype torch.int16)和数组不同对于张量而言数值型和布尔型张量就是最常用的两种张量类型相关类型总结如下
数据类型dtype32bit浮点数torch.float32或torch.float64bit浮点数torch.float64或torch.double16bit浮点数torch.float16或torch.half8bit无符号整数torch.unit88bit有符号整数torch.int816bit有符号整数torch.int16或torch.short16bit有符号整数torch.int16或torch.short32bit有符号整数torch.int32或torch.int64bit有符号整数torch.int64或torch.long布尔型torch.bool复数型torch.complex64 1.3 张量类型的转化
张量类型的隐式转化
与Numpy中array相同当各张量类型属于不同类型时系统会自动进行隐式转化
# 浮点型和整数型的隐式转化
torch.tensor([1.1, 2]).dtype
# output : torch.float# 布尔型和数值型的隐式转化
torch.tensor([True, 2.0])
# output : torch([1., 2.])张量类型的转化方法
除了隐式转化我们还可以使用.float()、.int()方法对张量类型进行转化
t torch.tensor([1, 2]) #创建张量t# 转化为默认浮点型(32位)
t.float()
# output : tensor([1., 2.])# 转化为双精度浮点型
t.double()
# output : tensor([1., 2.],dtype torch.float64)# 转化为16位整数
t.short()
# output : tensor([1, 2],dtype torch.int16)2 张量的维度与形变 张量作为一组数的结构化表示同样具有维度的概念。 简单理解向量就是一维的数组而矩阵就是二维的数组。 在张量中我们还可以定义更高维度的数组。 当然张量的高维数组和Numpy中Array概念类似。 2.1 创建高维张量
用简单序列创建一维数组
t torch.tensor([1, 2])# 使用ndim属性查看张量的维度
t.ndim
# output : 1# 使用shape查看形状
t.shape
# output : torch.Size([2])# 和shape函数相同
t.shape()
# output : torch.Size([2])注和Numpy不同PyTorch中size方法返回结果和shape属性返回结果一致 此外还需要注意有两个常用的函数/方法用来查看张量的形状
# 返回拥有几个N-1维张量
len(t1)
# output : 2,表示有两个0维张量
# 比如对二维张量采用len()方法返回的就是多少个一维张量# 返回总共拥有几个数
t.numel()
# output : 2注一维张量len和numel返回结果相同但更高维度张量则不然 用“序列”的“序列”创建二维数组
以此类推我们还可以用形状相同的序列组成一个新的序列进而将其转化为二维张量。
# 用list的list创建二维数组
t torch.tensor([[1, 2], [3, 4]])
t
# output:
tensor([[1, 2],[3, 4]])# 查看维度
t.ndim
# output : 2# 查看形状
t.shape
# output : torch.Size([2, 2])# 查看形状
t.size()
# output : torch.Size([2, 2])# 查看有几个N-1维张量
len(t)
# output : 2,表示由两个一维张量构成# 查看拥有几个数
t.numel()
# output : 4“零”维张量
在Pytorch中还有一类特殊的张量被称为零维张量。该类张量只包含一个元素但又不是单独一个数。
ttorch.tensor(1)
t # output : tensor(1)t.ndim # output : 0t.shape # output : torch.Size([])t.numel() # output : 1理解我们可以将零维张量视为拥有张量属性的单独一个数。 例如张量可以存在GPU上但Python原生的数值型对象不行而零维张量可以。 从学术名称来说Python中单独一个数是scalars(标量)而零维的张量则是tensor 高维张量 三维及三维以上的张量称其为高维张量。 a1 np.array([[1, 2, 2], [3, 4, 4]])
a1
# output :
array([[1, 2, 2][3, 4, 4]])a2 np.array([[5, 6, 6], [7, 8, 8]])
a2
# output :
array([[5, 6, 6][7, 8, 8]])# 由两个形状相同的二维数组创建一个三维的张量
t3 torch.tensor([a1, a2])
t3
# output :
tensor([[[1, 2, 2],[3, 4, 4]],[[5, 6, 6],[7, 8, 8]]], dtypetorch.int32)t3.ndim
# output : 3t3.shape
# output : torch.Size([2, 2, 3]) 包含两个两行三列的矩阵的张量len(t3)
# output : 2t3.numel()
# output : 122.2 张量的形变 张量作为数字的结构化集合其结构也是可以根据实际需求灵活调整的 flatten拉平将任意维度张量转化为一维张量
t2 torch.tensor([[1, 2], [3, 4]]) #二维张量t2.flatten()
# output : tensor([1, 2, 3, 4])t3 torch.tensor([a1, a2]) #三维张量t3.flatten()
# output : tensor([1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8], dtype int32)注如果将零维张量使用flatten则会转化为一维张量 t torch.tensor(1) # 零维张量t.flatten()
# output : tensor([1])reshape方法任意变形
t1 torch.tensor([1, 2])# 转化为两行、一列的向量
t1.reshape(2, 1)
# output :
tensor([[1],[2]])注意reshape转化后的维度由该方法输入的参数个数决定 转化后生成一维张量
t1.reshape(2)
# output : tensor([1, 2])
# 另一种表达形式
t1.reshape(2, )t1.reshape(2).ndim
# output : 1转化后生成二维张量
t1.reshape(1, 2)
# output : tensor([[1, 2]])t1.reshape(1, 2).ndim
# output : 2转化后生成三维张量
t1.reshape(1, 1, 2)
# output : tensor([[[1, 2]]])t1.reshape(1, 2, 1)
# output :
tensor([[[1],[2]]])# 注意转化过程维度的变化
t1.reshape(1, 2, 1).ndim
# output : 33 特殊张量的创建方法 在很多数学科学计算过程中会创建一些特殊取值的张量用于模拟特殊取值的矩阵。如全0矩阵、对角矩阵等。 因此PyTorch中也存在很多创建特殊张量的函数。 3.1 特殊取值的张量创建方法
全0张量
torch.zeros([2, 3]) #创建全是0的两行、三列的张量矩阵
# output :
tensor([[0., 0., 0.],[0., 0., 0.]])由于zeros就已经确定了张量元素取值因此该函数传入的参数实际上决定了张量的形状 全1张量
torch.ones([2, 3])
# output :
tensor([[1., 1., 1.],[1., 1., 1.]])单位矩阵
torch.eye(5)
# output :
tensor([[1., 0., 0., 0., 0.],[0., 1., 0., 0., 0.],[0., 0., 1., 0., 0.],[0., 0., 0., 1., 0.],[0., 0., 0., 0., 1.]])对角矩阵 在pytorch中需要利用一维张量去创建对角矩阵 t1 torch.tensor([1, 2])torch.diag(t1)
# output :
tensor([[1, 0],[0, 2]])千万不能直接使用list创建对角矩阵. rand服从0-1均匀分布的张量
torch.rand(2, 3)
# output :
tensor([[0.0214, 0.3989, 0.0814],[0.8888, 0.1773, 0.2567]])randn服从标准正态分布的张量
torch.randn(2, 3)
# output :
tensor([[-0.8638, 1.0079, 0.0267],[ 1.3223, 0.0856, -0.7271]])normal服从指定正态分布的张量
torch.normal(2, 3, size(2, 2)) # 均值为2标准差为3的张量
# output :
tensor([[ 1.5616, -0.9688],[ 3.4087, -0.0917]])randint整数随机采样结果
torch.randint(1, 10, [2, 4]) # 在1-10之间随机抽取整数组成两行四列的张量
# output :
tensor([[4, 1, 9, 8],[4, 2, 5, 7]])arange/linspace生成数列
torch.arange(5) # 和range相同
# output : tensor([0, 1, 2, 3, 4])torch.arange(1, 5, 0.5) # 从1到5左闭右开每隔0.5取值一个
# output :
tensor([1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000, 4.5000])torch.linspace(1, 5, 3) # 从1到5左右都包含等距取三个数
# output :
tensor([1., 3., 5.])empty生成未初始化的指定形状矩阵
torch.empty(2, 3) # 生成的值只是内存中的原始字节
# output :
tensor([[3.1056e-33, 1.8203e-42, 0.0000e00],[0.0000e00, 0.0000e00, 0.0000e00]])full根据指定形状填充指定数值
torch.full([2, 4], 2)
# output :
tensor([[2, 2, 2, 2],[2, 2, 2, 2]])3.2 创建指定形状的数组 我们还可以根据指定对象的形状进行数值填充只需要在上述函数后面加上_like即可 t1 torch.tensor([1, 2])
t2 torch.tensor([[1, 2], [3, 4]])torch.full_like(t1, 2) # 根据t1形状填充数值2
# output : tensor([2, 2])torch.randint_like(t2, 1, 10)
# output :
tensor([[1, 1],[2, 9]])torch.zeros_like(t1)
# output : tensor([0, 0])需要注意的是_like函数需要注意转化前后数据类型一致的问题。 t10 torch.tensor([1.1, 2.2]) # 传入randn_like的参数必须为浮点型
torch.randn_like(t10)
# output :
tensor([1.0909, 0.0379])4 张量和其它相关类型之间的转化方法 张量、数组和列表是较为相似的三种类型对象。在实际操作中经常会涉及三种对象的相互转化。 在前面中torch.tensor函数可以直接将数组或者列表转化为张量我们也可以将张量转化为数组或者列表 .numpy方法张量转化为数组
t torch.tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])t.numpy()
# output :
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype int64)
# 也可以用np.array函数直接转化为array
np.array(t).tolist方法张量转化为列表
t.tolist()
# output : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]list函数张量转化为列表
list(t)
# output :
[tensor(1),tensor(2),tensor(3),tensor(4),tensor(5),tensor(6),tensor(7),tensor(8),tensor(9),tensor(10)]此时转化的列表是由一个个零维张量构成的列表而非张量的数值组成的列表 .item()方法转化为数值 在很多情况下我们需要将最终计算的结果作为单独的数值进行输出 n torch.tensor(1)
n.item()
# output : 15 张量的深拷贝
张量和python一样等号赋值操作实际上是浅拷贝。
进行深拷贝时需要使用clone方法
t1 torch.tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])t2 t1
t1[1] 10
t2 #浅拷贝会和t2的值一起修改
# output : tensor([ 1, 10, 3, 4, 5, 6, 7, 8, 9, 10])此时t1和t2指向相同的对象。要使t2不随t1对象改变而改变则需要对t2进行深拷贝独自拥有一份对象。
t3 t1.clone()
t1[0] 100
t3
# output : tensor([ 1, 10, 3, 4, 5, 6, 7, 8, 9, 10])