pytorch 数学运算

Pytorch 数学运算

基本操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

a = torch.rand(3, 4)
b = torch.rand(4)

a+b
tensor([[0.9806, 1.2292, 0.7037, 0.1900],
[1.2180, 1.3809, 0.5762, 0.3377],
[1.1987, 0.8854, 1.2460, 0.8422]])

>>> torch.add(a, b)
tensor([[0.9806, 1.2292, 0.7037, 0.1900],
[1.2180, 1.3809, 0.5762, 0.3377],
[1.1987, 0.8854, 1.2460, 0.8422]])
>>>
>>> torch.all(torch.eq(a-b, torch.sub(a, b)))
tensor(1, dtype=torch.uint8)
>>>
>>> torch.all(torch.eq(a*b, torch.mul(a, b)))
tensor(1, dtype=torch.uint8)
>>>
>>> torch.all(torch.eq(a/b, torch.div(a, b)))
tensor(1, dtype=torch.uint8)

matmul @ mm(only 2D)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
>>> a=torch.full((2, 2), 3)
>>> a
tensor([[3., 3.],
[3., 3.]])
>>>
>>> b = torch.ones(2, 2)
>>>
>>> torch.mm(a, b)
tensor([[6., 6.],
[6., 6.]])
>>>
>>> torch.matmul(a, b)
tensor([[6., 6.],
[6., 6.]])
>>>
>>> [email protected]
tensor([[6., 6.],
[6., 6.]])
>>>
>>>
>>>
>>> a = torch.rand(4, 784)
>>>
>>> x = torch.rand(4, 784)
>>>
>>> w = torch.rand(512, 784)
>>>
>>> ([email protected]()).shape
torch.Size([4, 512])
>>>
>>>
>>>
>>> a = torch.rand(4, 3 ,28, 64)
>>> b = torch.rand(4, 3, 64, 32)
>>>
>>> torch.mm(a, b).shape
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: matrices expected, got 4D, 4D tensors at /pytorch/aten/src/TH/generic/THTensorMath.cpp:956
>>>
>>> torch.matmul(a, b).shape
torch.Size([4, 3, 28, 32])
>>>
>>> b = torch.rand(4, 1, 64, 32)
>>>
>>> torch.matmul(a, b).shape
torch.Size([4, 3, 28, 32])
>>>
>>> b = torch.rand(4, 64, 32)
>>>
>>> torch.matmul(a, b).shape
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: The size of tensor a (3) must match the size of tensor b (4) at non-singleton dimension 1

Power / sqrt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

>>> a = torch.full([2, 2], 3)
>>>
>>> a.pow(2)
tensor([[9., 9.],
[9., 9.]])
>>>
>>> a ** 2
tensor([[9., 9.],
[9., 9.]])
>>>
>>> aa = a ** 2
>>>
>>> aa.sqrt()
tensor([[3., 3.],
[3., 3.]])
>>>
>>> aa.rsqrt()
tensor([[0.3333, 0.3333],
[0.3333, 0.3333]])
>>>
>>> aa**(0.5)
tensor([[3., 3.],
[3., 3.]])

log

1
2
3
4
5
6
7
8
9
>>> a = torch.exp(torch.ones(2, 2))
>>>
>>> a
tensor([[2.7183, 2.7183],
[2.7183, 2.7183]])
>>>
>>> torch.log(a)
tensor([[1., 1.],
[1., 1.]])

近似值 floor, ceil, round trunc, frac

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
>>> a = torch.tensor(3.14)
>>>
>>> a.floor()
tensor(3.)
>>>
>>> a.ceil()
tensor(4.)
>>>
>>> a.trunc()
tensor(3.)
>>>
>>> a.frac()
tensor(0.1400)
>>>
>>> a = torch.tensor(3.499)
>>>
>>> a.round()
tensor(3.)
>>>
>>> a = torch.tensor(3.5)
>>>
>>> a.round()
tensor(4.)

clamp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
>>> grad = torch.rand(2, 3)*15
>>> grad
tensor([[ 8.9165, 9.5164, 0.1095],
[12.0321, 9.3978, 1.3909]])
>>>
>>> grad.max()
tensor(12.0321)
>>>
>>> grad.median()
tensor(8.9165)
>>>
>>> grad.clamp(10)
tensor([[10.0000, 10.0000, 10.0000],
[12.0321, 10.0000, 10.0000]])
>>>
>>> grad.clamp(0, 10)
tensor([[ 8.9165, 9.5164, 0.1095],
[10.0000, 9.3978, 1.3909]])