Pytorch学习笔记(七)——数学运算

学习目标

掌握Pytorch

学习内容

常用的数学运算:
加减乘除:这几个操作建议直接使用运算符。

add+
sub
mul*
div/
matmul矩阵相乘
pow指数
sqrt/rsqrt平方根/平方根倒数
round四舍五入

(1)torch.add(input,other,*,alpha=1,out=None) → Tensor
outi​=inputi​+alpha×otheri​
note: 当两个tensor维度不一致时,先进行broadcasting操作,再进行add。

a = torch.randn(4)	#tensor([ 0.0202,  1.0985,  1.3506, -0.6056])
torch.add(a, 20)	#tensor([ 20.0202,  21.0985,  21.3506,  19.3944])
b = torch.randn(4)	#tensor([-0.9732, -0.3497,  0.6245,  0.4022])
c = torch.randn(4, 1)	
# tensor([[ 0.3743],
#         [-1.7724],
#         [-0.5811],
#         [-0.8017]])

torch.add(b, c, alpha=10)
# tensor([[  2.7695,   3.3930,   4.3672,   4.1450],
#         [-18.6971, -18.0736, -17.0994, -17.3216],
#         [ -6.7845,  -6.1610,  -5.1868,  -5.4090],
#         [ -8.9902,  -8.3667,  -7.3925,  -7.6147]])

(2)torch.sub(input, other, *, alpha=1, out=None) → Tensor
操作与torch.add类似。
outi​=inputi​-alpha×otheri​

a = torch.tensor((1, 2))
b = torch.tensor((0, 1))
torch.sub(a, b, alpha=2)	#tensor([1, 0])

(3)torch,matmul(input,other) → Tensor

当两个tensor都是一维时,返回两个向量的点积运算结果。

tensor1 = torch.randn(3)
tensor2 = torch.randn(3)
torch.matmul(tensor1, tensor2).size()	#torch.Size([])

当两个tensor都是二维时,进行矩阵相乘。

tensor1 = torch.randn(2,2)
tensor2 = torch.randn(2,3)
torch.matmul(tensor1, tensor2).size()	#torch.Size([2, 3])

当input为1维,other为2维时,在input前插入长度为1的新维度,使其扩充到二维,然后进行矩阵相乘,再将结果去掉插入的维度,得到的最终结果的维度与input的维度相同。

tensor1 = torch.randn(3)
tensor2 = torch.randn(3,4)
#将tensor1扩充成 (1,3),再与tensor2相乘得到(1,4),最后去掉添加的维度1
torch.matmul(tensor1,tensor2).size()    #torch.Size([4])

当input为2维,other为1维时,在other后插入长度为1的新维度,使其扩充到二维,然后进行矩阵相乘,再将结果去掉插入的维度,得到的最终结果的维度与other的维度相同。

tensor1 = torch.randn(3,4)
tensor2 = torch.randn(4)
#将tensor2扩充成 (4,1),再与tensor2相乘得到(3,1),最后去掉添加的维度1
torch.matmul(tensor1,tensor2).size()    #torch.Size([3])

将input和other都为多维时,将多出的维度提出来,其他部分做矩阵相乘。

tensor1 = torch.randn(2,3,4)
tensor2 = torch.randn(4,6)
#将tensor1的0维提出来,剩下的部分做矩阵相乘
torch.matmul(tensor1,tensor2).size()    #torch.Size([2, 3, 6])
tensor1 = torch.randn(2,3,4)
tensor2 = torch.randn(1,4,6)
#将tensor2的0维1broadcast成2提出来,剩下的部分做矩阵相乘
torch.matmul(tensor1,tensor2).size()    #torch.Size([2, 3, 6])
tensor1 = torch.randn(2,1,3,4)
tensor2 = torch.randn(5,4,3)
#将tensor1的0维2提出来,再将tensor1的1维1broadcast成5提出来,剩下的部分做矩阵相乘
torch.matmul(tensor1,tensor2).size()    #torch.Size([2, 5, 3, 3])

(4)torch.row() :实现求指数操作,可以直接写成符号“**”

求平方也可以写成“ **2 ”

#创建一个每个元素都为3的2X2矩阵
a = torch.full([2,2],3)
a.pow(2)	#也可以写成a**2
# tensor([[9, 9],
#         [9, 9]])

(5)torch.sqrt(): 平方根
torch.rsqrt(): 平方根的倒数

求平方根可以写成“ **0.5”

#创建一个每个元素都为9的2X2矩阵
a = torch.full([2,2],9)
a.sqrt()  #也可以写成a**0.5
# tensor([[3., 3.],
#         [3., 3.]])

a.rsqrt()
# tensor([[0.3333, 0.3333],
#         [0.3333, 0.3333]])

(6)

torch.floor()向下取整
torch.ceil()向上取整
torch.trunc()拆分后取整数部分
torch.frac()拆分后取小数部分
a = torch.tensor(3.14)
a.floor()    #tensor(3.)
a.ceil()     #tensor(4.)
a.trunc()    #tensor(3.)
a.frac()     #tensor(0.1400)

(7)torch.round()

a = torch.tensor(3.6)
b = torch.tensor(2.1)
a.round()    #tensor(4.)
b.round()    #tensor(2.)

总结

又是元气满满的一天,加油

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2022年5月31日
下一篇 2022年5月31日

相关推荐