<>简介

Tensor对象是一个任意维度的矩阵,但是一个Tensor中所有元素的数据类型必须一致。torch包含的数据类型和普遍编程语言的数据类型相似,包含浮点型,有符号整型和无符号整型,这些类型既可以定义在cpu上,也可以定义在Gpu上

<>Tensor执行算数运算

* 逐个元素自己相乘
* 矩阵乘法
tensor执行算数运算符的运算,是两个矩阵对应元素的运算。torch.mm执行矩阵乘法的运算
import torch a=torch.tensor([[1,2],[3,4]]) b=torch.tensor([[1,2],[3,4]]) c=a*b
print("逐个元素相乘",c) #逐个元素相乘 #tensor([[ 1, 4], # [ 9, 16]]) c=torch.mm(a,b) print(
"矩阵乘法",c) #矩阵乘法 tensor([[ 7, 10], # [15, 22]])
<>pytorch直接创建数据的内置函数

* torch.arange()
* torch.eye()
* torch.linspace()
* torch,zeros()
* torch.rand()
* torch.randn()
* torch.randint()
* torch.from_numpy()
torch.arange和python内置的range的使用方法基本相同,其中第三个参数是步长
torch.eye(3,3)创建斜对角线为1的矩阵
torch.linspace的第三个参数指定返回的个数
torch.zeros返回全0矩阵
import torch print(torch.arange(5)) #tensor([0, 1, 2, 3, 4]) print(torch.arange
(1,5,2)) #tensor([1, 3]) print(torch.linspace(0,5,10)) #ensor([0.0000, 0.5556,
1.1111, 1.6667, 2.2222, 2.7778, 3.3333, 3.8889, 4.4444,5.0000]) print(torch.ones
(3,3)) #tensor([[1., 1., 1.], # [1., 1., 1.], # [1., 1., 1.]]) print(torch.zeros
(3,3)) #tensor([[0., 0., 0.], # [0., 0., 0.], # [0., 0., 0.]])
torch.rand返回范围为[0,1]的均匀分布采样的元素所组成的矩阵
torch.randn返回从正态分布采样的元素所组成的矩阵
torch.randint返回指定区间的均匀分布采样的随机整数所生成的矩阵
print(torch.rand(3,3)) #0-1之间的三行三列 #tensor([[0.9195, 0.1319, 0.4128],
#[0.0908, 0.6542, 0.5087], # [0.6761, 0.0876, 0.0532]]) print(torch.randint(0,9,
(3,3))) #tensor([[2, 0, 2], # [7, 1, 7], # [4, 0, 1]])
从numpy中获得数据
a=numpy.array([1,2,3,4,5]) print(torch.from_numpy(a)) #tensor([1, 2, 3, 4, 5],
dtype=torch.int32) t=torch.from_numpy(a) print(t[0]) #tensor(1,
dtype=torch.int32)
<>Tensor的索引和切片

* torch.nonzero()
* torch.where(condition,x,y)
*
torch.nonzero用于返回非零值的索引矩阵
a=torch.arange(9).view(3,3) index=torch.nonzero(a>=8) print(index)
#tensor([[2, 2]]) a=torch.randint(0,2,(3,3)) print(a) #tensor([[1, 1, 0], # [1,
1, 1], # [0, 1, 0]]) index=torch.nonzero(a) print(index) #tensor([[0, 0], # [0,
1], # [1, 0], # [1, 1], # [2, 0], # [2, 2]])

torch.where(condition,x,y)判断conditon的条件是否满足,当某个元素满足条件时,则返回对应矩阵x相同矩阵的元素,否则返回矩阵y的元素
x=torch.randn(3,2) print(x) #tensor([[ 0.0686, 0.9833], # [ 0.4247, -0.1751],
# [ 1.3361, -0.5646]]) y=torch.ones(3,2) print(y) #tensor([[1., 1.], # [1., 1.],
# [1., 1.]]) print(torch.where(x>0,x,y)) #tensor([[0.6920, 1.0000], # [0.2726,
1.0000], # [1.0000, 0.4488]])
<>Tensor的变换,拼接和拆分

* torch.cat()
* torch.stack()
* torch.split()
* torch.chunk()
Tensor.nelement,Tensor.ndimension,ndimension.size可分别用来查看矩阵元素个数,轴的个数以及维度
属性Tensor.shape也可以查看Tensor的维度 a=torch.rand(1,2,3,4,5) #五维的 个数为1*2*3*4*5 print(
"元素个数",a.nelement()) #元素个数 120 print("轴的个数",a.ndimension()) #轴的个数 5 print("矩阵维度"
,a.size(),a.shape) #矩阵维度 torch.Size([1, 2, 3, 4, 5]) torch.Size([1, 2, 3, 4, 5])

Tensor.view和Tensor.reshape都能被用来更改Tensor的维度,它们的区别在于,Tensor.view要求Tensor的物理存储必须是连续的否则将报错,而Tensor.reshape没有这种要求。Tensor.view返回的是一个索引,更改返回值,则原始值也会被改变。Tensor,reshape返回的是引用还是复制是不确定的。它们的相同之处在于都要接收输出的维度作为参数。可以在维度中输出-1,PyTorch会自动推断它的数值
b=a.view(2*3,4*5) print(b.shape) #torch.Size([6, 20]) c=a.reshape(-1) print(c.
shape) #torch.Size([120]) d=a.reshape(2*3,-1) print(d.shape) #torch.Size([6,
20])

PyTorch提供了torch,cat和torch.stack用于拼接矩阵,不同之处是torch.cat在已有的轴dim上拼接矩阵,给定轴的维度可以不同,而其他轴的维度必须相同。torch,stack在新的轴上拼接,它要求被拼接的矩阵所有维度都要相同
a=torch.randn(2,3) print(a) #tensor([[-0.6382, -0.2846, -0.4478], # [ 0.5261,
0.1390, -1.2212]]) b=torch.randn(3,3) print(b) #tensor([[ 0.4052, -1.1623,
1.0163], #[ 0.6644, 1.1510, -0.4528], #[ 0.4046, -0.1125, 1.1805]]) #默认维度为dim=0
c=torch.cat((a,b)) print(c) #tensor([[ 0.6781, 0.3404, -1.0138], # [-0.0582,
0.2744, 0.4065], # [ 2.0596, 0.8160, 0.6391], # [ 1.6246, 0.1964, 1.8078], # [
1.0482, 0.6085, -0.6127]]) d=torch.cat((b,b,b),dim=1) #维度为1是按列拼接 print(d)
#tensor([[-0.3286, 1.9219, -0.9029, -0.3286, 1.9219, -0.9029, -0.3286, 1.9219,
# -0.9029] # [ 0.3017, -0.4240, -0.0260, 0.3017, -0.4240, -0.0260, 0.3017,
-0.4240, # -0.0260], # [-0.0038, -1.1274, -0.4668, -0.0038, -1.1274, -0.4668,
-0.0038, -1.1274, # -0.4668]]) c=torch.stack((b,b),dim=1) print(c) #tensor([[[
0.1392, 2.2047, -0.2022], # [ 0.1392, 2.2047, -0.2022]], # [[ 0.6079, -0.0103,
1.3908], # [ 0.6079, -0.0103, 1.3908]], # [[-2.0081, 0.3080, 0.2132], #
[-2.0081, 0.3080, 0.2132]]]) d=torch.stack((b,b),dim=0) print(d) #tensor([[[
0.1392, 2.2047, -0.2022], # [ 0.6079, -0.0103, 1.3908], # [-2.0081, 0.3080,
0.2132]], # # [[ 0.1392, 2.2047, -0.2022], # [ 0.6079, -0.0103, 1.3908], #
[-2.0081, 0.3080, 0.2132]]])

除了拼接矩阵,PyTorch还提供了torch.split和torch.chunk用于拆分矩阵,它们的不同之处在于torch.split传入的是拆分后的每个矩阵的大小,可以传入list
,也可以传入整数
a=torch.randn(10,3) for x in torch.split(a,[1,2,3,4],dim=0):
#按照10这个维度去分块,每大块分为1,2,3,4 print(x.shape) #torch.Size([1, 3]) #torch.Size([2, 3])
#torch.Size([3, 3]) #torch.Size([4, 3]) for x in torch.split(a,4,dim=0):
#按照10这个维度去分,每块包含四个小块,最后一块只能凑到两个小块 print(x.shape) #torch.Size([4, 3])
#torch.Size([4, 3]) #torch.Size([2, 3]) for x in torch.chunk(a,4,dim=0): print(x
.shape) #torch.Size([3, 3]) #torch.Size([3, 3]) #torch.Size([3, 3])
#torch.Size([1, 3]) #拆分的矩阵个数为4
<>Pytorch的Reduction操作

* 求全局的最大值及其索引
* 沿横轴计算每一列的累加
* 沿纵轴计算每一列的累乘
* 计算矩阵的均值,中值,协方差
a.mean(),a.median(),a.std()
* torch.unique用来找出矩阵中出现了哪些元素 a=torch.tensor([[1,2],[3,4]]) print("全局最大值:",
torch.max(a)) #全局最大值: tensor(4) #沿着横轴计算每一列的叠加 print(torch.max(a,dim=0))
#返回最大值以及索引 #tensor([[1, 2], # [4, 6]]) #沿着纵轴计算每一行的叠加 print(torch.cumsum(a,dim=1)
) #tensor([[1, 3], # [3, 7]]) print(torch.cumprod(a,dim=0)) #tensor([[1, 2], #
[3, 8]]) #沿着纵轴计算每一行的累乘 print(torch.cumprod(a,dim=1)) #tensor([[ 1, 2], # [ 3,
12]]) #torch.unique用来找出矩阵中出现了哪些元素 a=torch.randint(0,3,(3,3)) print(a)
#tensor([[0, 2, 2], # [1, 0, 1], # [0, 1, 0]]) print(torch.unique(a))
#tensor([0, 1, 2])

技术
今日推荐
PPT
阅读数 126
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信