Tensor的算术运算。
常见的算术运算如下:
注意:在pytorch中如果对tensor的一个函数后加上了下划线,则表明这是一个in-place类型,所谓in-place类型是指在一个tensor上操作了之后,是直接修改了这个tensor。
import torch
print("=== init ===")
a = torch.tensor([1., 2.])
b = torch.tensor([3., 4.])
print(a)
print(b)
print("------------")
##add
print("==== add ====")
print(a + b)
print(a.add(b))
print(torch.add(a, b))
print("------------")
# 测试in-place
print(a)
print(a.add_(b))
print(a)
# sub
print("==== sub ====")
print(a - b)
print(torch.sub(a, b))
print(a.sub(b))
print(a.sub_(b))
print(a)
## mul
print("===== mul ====")
print(a)
print(b)
print(a * b)
print(torch.mul(a, b))
print(a.mul(b))
print(a)
print(a.mul_(b))
print(a)
# div
print("=== div ===")
print(a / b)
print(torch.div(a, b))
print(a.div(b))
print(a.div_(b))
print(a)
###matmul
print("=== matmul ===")
a = torch.ones(2, 1)
b = torch.ones(1, 2)
print(a @ b)
print(a.matmul(b))
print(torch.matmul(a, b))
print(torch.mm(a, b))
print(a.mm(b))
##高维tensor
print("=== 高维 matmul ===")
a = torch.ones(1, 2, 3, 4)
b = torch.ones(1, 2, 4, 3)
print(a.matmul(b).shape)
##pow
print("=== pow ===")
a = torch.tensor([1, 2])
print(torch.pow(a, 3))
print(a.pow(3))
print(a ** 3)
print(a.pow_(3))
print(a)
# exp
print("=== exp ===")
a = torch.tensor([1, 2],
dtype=torch.float32)
print(a.type())
print(torch.exp(a))
print(torch.exp_(a))
print(a.exp())
print(a.exp_())
##log
print("=== log ===")
a = torch.tensor([10, 2],
dtype=torch.float32)
print(torch.log(a))
print(torch.log_(a))
print(a.log())
print(a.log_())
a = torch.tensor([16., 64.])
print(torch.log2(a))
print(torch.log2_(a))
a= torch.tensor([100.,1000.])
print(torch.log10(a))
print(torch.log10_(a))
##sqrt
print("=== sqrt ===")
a = torch.tensor([10, 2],
dtype=torch.float32)
print(torch.sqrt(a))
print(torch.sqrt_(a))
print(a.sqrt())
print(a.sqrt_())
print("===math===")
a = torch.rand(2, 2)
a = a * 10
print(a)
#向下取整
print(torch.floor(a))
#向上取整
print(torch.ceil(a))
#四舍五入
print(torch.round(a))
#截断小数点
print(torch.trunc(a))
#截取小数点部分
print(torch.frac(a))
print(a % 2)
b = torch.tensor([[2, 3], [4, 5]],
dtype=torch.float)
#除法余数,余数的正负与被除数相同
print(torch.fmod(a, b))
#除法余数,余数与除数有相同的符号
print(torch.remainder(a, b))
运行结果:
=== init ===
tensor([1., 2.])
tensor([3., 4.])
------------
==== add ====
tensor([4., 6.])
tensor([4., 6.])
tensor([4., 6.])
------------
tensor([1., 2.])
tensor([4., 6.])
tensor([4., 6.])
==== sub ====
tensor([1., 2.])
tensor([1., 2.])
tensor([1., 2.])
tensor([1., 2.])
tensor([1., 2.])
===== mul ====
tensor([1., 2.])
tensor([3., 4.])
tensor([3., 8.])
tensor([3., 8.])
tensor([3., 8.])
tensor([1., 2.])
tensor([3., 8.])
tensor([3., 8.])
=== div ===
tensor([1., 2.])
tensor([1., 2.])
tensor([1., 2.])
tensor([1., 2.])
tensor([1., 2.])
=== matmul ===
tensor([[1., 1.],
[1., 1.]])
tensor([[1., 1.],
[1., 1.]])
tensor([[1., 1.],
[1., 1.]])
tensor([[1., 1.],
[1., 1.]])
tensor([[1., 1.],
[1., 1.]])
=== 高维 matmul ===
torch.Size([1, 2, 3, 3])
=== pow ===
tensor([1, 8])
tensor([1, 8])
tensor([1, 8])
tensor([1, 8])
tensor([1, 8])
=== exp ===
torch.FloatTensor
tensor([2.7183, 7.3891])
tensor([2.7183, 7.3891])
tensor([ 15.1543, 1618.1781])
tensor([ 15.1543, 1618.1781])
=== log ===
tensor([2.3026, 0.6931])
tensor([2.3026, 0.6931])
tensor([ 0.8340, -0.3665])
tensor([ 0.8340, -0.3665])
tensor([4., 6.])
tensor([4., 6.])
tensor([2., 3.])
tensor([2., 3.])
=== sqrt ===
tensor([3.1623, 1.4142])
tensor([3.1623, 1.4142])
tensor([1.7783, 1.1892])
tensor([1.7783, 1.1892])
===math===
tensor([[4.0199, 6.7884],
[0.7380, 2.6133]])
tensor([[4., 6.],
[0., 2.]])
tensor([[5., 7.],
[1., 3.]])
tensor([[4., 7.],
[1., 3.]])
tensor([[4., 6.],
[0., 2.]])
tensor([[0.0199, 0.7884],
[0.7380, 0.6133]])
tensor([[0.0199, 0.7884],
[0.7380, 0.6133]])
tensor([[0.0199, 0.7884],
[0.7380, 2.6133]])
tensor([[0.0199, 0.7884],
[0.7380, 2.6133]])