import torch
±*/
- 维度不同的时候会broadcast
- element-wise
a = torch.ones(2,2)
b = torch.tensor(4)
print(a + b)
print(a.add(b))
print(torch.add(a,b))
tensor([[5., 5.],
[5., 5.]])
tensor([[5., 5.],
[5., 5.]])
tensor([[5., 5.],
[5., 5.]])
print(a/b)
print(torch.all(torch.eq(a/b,a.div(b))))
print(torch.equal(a/b,a.div(b)))
tensor([[0.2500, 0.2500],
[0.2500, 0.2500]])
tensor(True)
True
print(a*b)
print(torch.all(torch.eq(a*b,a.mul(b))))
tensor([[4., 4.],
[4., 4.]])
tensor(True)
print(a-b)
print(torch.all(torch.eq(a-b,a.sub(b))))
tensor([[-3., -3.],
[-3., -3.]])
tensor(True)
矩阵的乘法
a = torch.ones(2,3)
b = torch.ones(3,2)+1
print(a.mm(b))
print(a.matmul(b))
tensor([[6., 6.],
[6., 6.]])
tensor([[6., 6.],
[6., 6.]])
次方
a = torch.full([2,3],4)
a.pow(2)
tensor([[16., 16., 16.],
[16., 16., 16.]])
b = torch.full([3,4],9)
b**(0.5)
tensor([[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]])
c = torch.ones(2,2)
torch.exp(c)
tensor([[2.7183, 2.7183],
[2.7183, 2.7183]])
torch.log(c)
tensor([[0., 0.],
[0., 0.]])
近似值
a = torch.tensor(-3.1415926)
print(torch.floor(a))
print(torch.ceil(a))
print(torch.trunc(a))
print(torch.frac(a))
print(torch.round(a))
tensor(-4.)
tensor(-3.)
tensor(-3.)
tensor(-0.1416)
tensor(-3.)
clamp
a = torch.rand(3,3)*10
a
tensor([[5.3887, 0.5297, 2.0162],
[9.9030, 0.1943, 5.6819],
[5.6103, 3.4774, 8.9113]])
a.clamp(1,6)
tensor([[5.3887, 1.0000, 2.0162],
[6.0000, 1.0000, 5.6819],
[5.6103, 3.4774, 6.0000]])