Pytorch Learning

Pytorch Learning following 跟李沐学AI

Basics

  1. 按某一维度求和
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Input:
A = torch.arange(24).reshape(2, 3, 4)
# from the outermost layer to the innermost layer: axis0->axis1->axis2
A, A.sum(axis=0), A.sum(axis=[1, 2])
Output:
(tensor([[[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]],

[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]]),
tensor([[12, 14, 16, 18], # sum the axis0, and the layout of
[20, 22, 24, 26], # axis1 & axis2 remains unchanged
[28, 30, 32, 34]]),
tensor([66, 210]) # axis0, which has 2 dimensions, remains unchanged
# and sum the other 2 layers —— axis1 & axis2
)