tensor数学运算
1.add/minus/multiply/divide
直接使用±*/ 或 使用add、sub、mul、div
a+b 相当于 torch.add(a,b)
2.matmul矩阵相乘
torch.matmal 或 @ #只针对2维数据运算
torch.matmul(a,b) 相当于 a@b
使用matmul对4维tensor元素时,默认前面都不变只计算最后两维
#使用matmul对4维tensor元素时,默认前面都不变只计算最后两维
a = tensor.rand(4,3,28,64)
b = tensor.rand(4,3,64,32)
tensor.matmul(a,b).shape # tensor.Size([4,3,28,32])
#a与b不符合boardcasting机制,报错
a = tensor.rand(4,3,28,64)
b = tensor.rand(4,64,32)
tensor.matmul(a,b).shape # 报错
#a与b符合boardcasting机制,相乘时b会自动拓展维[4,3,64,32]
a = tensor.rand(4,3,28,64)
b = tensor.rand(4,1,64,32)
tensor.matmul(a,b).shape # tensor.Size([4,3,28,32])
3.power次方
求三次方:a.pow(3) 相当于 a**3
开二次方:a.sqrt()相当于 a**(0.5)
平方根的倒数:a.rsqrt()
4.exp/log
5.approximation
.floor() .ceil() #向下/上取整
.round() #四舍五入
.trunc() .frac() #裁剪为整数/小数部分
6.clamp裁剪
#In [5] 最小值10
#In [7] 裁成0到10之间