pytorch-tensor运算

发布时间 2023-07-31 15:01:24作者: 哎呦哎(iui)

Math operation
▪ Add/minus/multiply/divide(加减乘除)
▪ Matmul(矩阵相乘)
▪ Pow(次方)
▪ Sqrt/rsqrt(次方根)
▪ Round()

add/minus/multiply/divide

这个使用的时候可以直接使用运算符"+,-,*,/"。
也可以使用

torch.add(a,b)
torch.sub(a,b)
torch.mul(a,b)
torch.div(a,b)
image

torch.all(torch.eq(a+b,torch.add(a,b)))
# tensor(True)



torch.all(torch.eq(a-b,torch.sub(a,b)))
# tensor(True)



torch.all(torch.eq(a*b,torch.mul(a,b)))
# tensor(True)

matmul(矩阵乘法)

2D tensor matmul

这个和上面那个不一样,上面那个*(mul),是对应位置相乘。

  • torch.mm(只能是2D)
  • torch.matmul
  • @(这个和torch.matmul一样)
a=torch.full((2,2),3.)
a
# tensor([[3., 3.],
#         [3., 3.]])



b=torch.ones(2,2)
b
# tensor([[1., 1.],
#         [1., 1.]])



a@b
# tensor([[6., 6.],
#         [6., 6.]])



torch.matmul(a,b)
# tensor([[6., 6.],
#         [6., 6.]])

一个线性层的实例
image
784->512

>2D tensor matmul

然后我们看一下大于2D的矩阵怎么相乘

tensor.matmul(a,b)
这个相乘的规则就是a,b的最后两位相乘,其他维度都符合Broadcast规则,就是其他维度可以不相等但是必须得有一个1.

a=torch.rand(4,3,28,64)
b=torch.rand(4,3,64,32)
torch.matmul(a,b).shape
# torch.Size([4, 3, 28, 32])

这个是可以的,最后两位相乘,前两位不变

a=torch.rand(4,3,28,64)
b=torch.rand(4,1,64,32)
torch.matmul(a,b).shape
# torch.Size([4, 3, 28, 32])

这个也是可以的,前面符合broadcast机制

a=torch.rand(4,3,28,64)
b=torch.rand(4,64,32)
torch.matmul(a,b).shape
# RuntimeError: The size of tensor a (3) must match the size of tensor b (4) at non-singleton dimension 1

这个是不可以的。
image

power

次方
这个次方是tensor的对应位置的值次方,

a.pow(k)
指的是a的对应位置的值k次方

a**(k)
对应位置的值k次方

a.sqrt()
开根号

a.rsqrt()
计算平方根导数

image
image

Exp log

torch.exp(a)
就是对a中的每一个数都去e的次方

torch.log(a)
相反,就是lg(a)

image

近似值(四舍五入、上下取整)

  • .floor() 下取整
  • .ceil() 上取整
  • .round() 四舍五入
  • .trunc() 取整数部分
  • .frac() 取小数部分
a=torch.tensor(3.14)
a.floor(),a.ceil(),a.trunc(),a.frac()
# (tensor(3.), tensor(4.), tensor(3.), tensor(0.1400))



a.round()
# tensor(3.)



a=torch.tensor(3.5)
a.round()
# tensor(4.)

clamp

这个是tesor数值的裁剪

a.clamp(a)
这个是tensor中数值大于a的都变成a

a.clamp(a,b)
这个是tensor中数值小于a的都变成a,大于b的都变成b


grad=torch.rand(2,3)*15
grad
# tensor([[14.0561,  5.3139, 13.9946],
#         [ 8.1637, 13.1810,  2.6178]])



grad.clamp(10)
# tensor([[14.0561, 10.0000, 13.9946],
#         [10.0000, 13.1810, 10.0000]])
		
		
		
		
grad.clamp(3,10)
# tensor([[10.0000,  5.3139, 10.0000],
#         [ 8.1637, 10.0000,  3.0000]])

image