基础知识

基本方法

  1. np(torch).stack([], axis=)函数

作用:是将一组数组沿着新的轴堆叠起来。

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
import numpy as np

a = np.array([[1, 2, 3],
[4, 5, 6]])

b = np.array([[7, 8, 9],
[10, 11, 12]])

c = np.stack([a, b], 0) # axis = 0

# 按照原数组直接堆叠
# array([[[ 1, 2, 3],
# [ 4, 5, 6]],

# [[ 7, 8, 9],
# [10, 11, 12]]])

c = np.stack([a, b], 1) # axis = 1

# 根据列表中数组的行堆叠
# array([[[ 1, 2, 3],
# [ 7, 8, 9]],

# [[ 4, 5, 6],
# [10, 11, 12]]])

c = np.stack([a, b], 2) # axis = 2

# 根据列表中数组的列d
# array([[[ 1, 7],
# [ 2, 8],
# [ 3, 9]],

# [[ 4, 10],
# [ 5, 11],
# [ 6, 12]]])
  1. np.meshgrid()

np.meshgrid是一个函数,用于生成多维网格矩阵。它接受多个一维张量作为输入,并返回与输入张量数相同的输出张量列表,每个输出张量包含相应维度中输入张量值的重复。

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
import numpy as np

# 二维
x = torch.tensor([1, 2, 3]) # 3
y = torch.tensor([4, 5]) # 2

X, Y = np.meshgrid(x, y)
print(X)
# tensor([[1, 2, 3],
# [1, 2, 3]])

print(Y)
# tensor([[4, 4, 4],
# [5, 5, 5]])

# 三维
z = torch.tensor([6,7,8])

X,Y,Z = np.meshgrid(x,y,z)

print(X.shape, Y.shape, Z.shape)
# torch.Size([2, 3, 3]) torch.Size([2, 3, 3]) torch.Size([2, 3, 3])

print(X) # 2*3*3
# tensor([[[1, 2, 3],
# [1, 2, 3],
# [1, 2, 3]],

# [[1, 2, 3],
# [1, 2, 3],
# [1, 2, 3]]])

print(Y)
# tensor([[[4, 4, 4],
# [5, 5, 5],
# [6, 6, 6]],

# [[4, 4, 4],
# [5, 5, 5],
# [6, 6, 6]]])

print(Z)
# tensor([[[6, 6, 6],
# [6, 6, 6],
# [6, 6, 6]],

# [[7, 7, 7],
# [7, 7, 7],
# [7, 7, 7]]])


  1. torch.meshgrid()

torch.meshgrid是一个函数,用于生成多维网格矩阵。它接受多个一维张量作为输入,并返回与输入张量数相同的输出张量列表,每个输出张量包含相应维度中输入张量值的重复。torch和numpy的作用是一样的,但是其x,y的坐标有所区别。

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
import torch

# 二维
x = torch.tensor([1, 2, 3]) # 3
y = torch.tensor([4, 5]) # 2

X, Y = torch.meshgrid(x, y)

print(X) # i j

# tensor([[1, 1],
# [2, 2],
# [3, 3]])

print(Y)
# tensor([[4, 5],
# [4, 5],
# [4, 5]])

#三维
z = torch.tensor([6,7,8]) # 3

X,Y,Z = torch.meshgrid(x,y,z)

print(X) # 3*2*3
# tensor([[[1, 1, 1],
# [1, 1, 1]],
#
# [[2, 2, 2],
# [2, 2, 2]],
#
# [[3, 3, 3],
# [3, 3, 3]]])

print(Y)
# tensor([[[4, 4, 4],
# [5, 5, 5]],
#
# [[4, 4, 4],
# [5, 5, 5]],
#
# [[4, 4, 4],
# [5, 5, 5]]])

print(Z)
# tensor([[[6, 7, 8],
# [6, 7, 8]],
#
# [[6, 7, 8],
# [6, 7, 8]],
#
# [[6, 7, 8],
# [6, 7, 8]]])
  1. torch.norm()

torch.norm()函数用于计算张量的范数。它可以计算给定维度上的向量范数或矩阵范数,并返回一个张量。默认为2范数,可以设置p指定范数。同时可以设置keepdim来决定是否保留原先的维度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import torch

# Example 1: Compute the L2-norm of a vector
x = torch.tensor([1, 2, 3], dtype=torch.float32)
l2_norm = torch.norm(x, p=2)
print(l2_norm) # Output: tensor(3.7417)

# Example 2: Compute the Frobenius norm of a matrix
A = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32)
fro_norm = torch.norm(A, p='fro')
print(fro_norm) # Output: tensor(5.4772)

# Example 3: Compute the L1-norm of a vector along a specific dimension
B = torch.tensor([[1, -2, 3], [-4, 5, -6]], dtype=torch.float32)
l1_norm = torch.norm(B, p=1, dim=1)
print(l1_norm) # Output: tensor([6., 15.])

  1. torch.gather()

作用:沿着由dim指定的轴收集数值,作为取值的索引。

torch.gather(input, dim, index, *, sparse_grad=False, out=None) → Tensor

input (Tensor) – 目标变量,输入 dim (int) – 需要沿着取值的坐标轴 index (LongTensor) – 需要取值的索引矩阵 sparse_grad (bool,optional) – 如果为真,输入将是一个稀疏张量 out (Tensor, optional) – 输出

index矩阵作为当前轴的索引,剩下的轴依旧按照顺序排序[0-n]

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

input = [
[0.0, 0.1, 0.2, 0.3],
[1.0, 1.1, 1.2, 1.3],
[2.0, 2.1, 2.2, 2.3]
]#shape [3,4]
input = torch.tensor(input)
length = torch.LongTensor([
[2,2,2,2],
[1,1,1,1],
[0,0,0,0],
[0,1,2,0]
])#[4,4]
out = torch.gather(input, dim=0, index=length)
print(out)

# 结果
# tensor([[2.0000, 2.1000, 2.2000, 2.3000],
# [1.0000, 1.1000, 1.2000, 1.3000],
# [0.0000, 0.1000, 0.2000, 0.3000],
# [0.0000, 1.1000, 2.2000, 0.3000]])

\[ 取值索引矩阵= \left[\begin{array}{llll} X_{20} & X_{21} & X_{22} & X_{23} \\ X_{10} & X_{11} & X_{12} & X_{13} \\ X_{00} & X_{01} & X_{02} & X_{03} \\ X_{00} & X_{11} & X_{22} & X_{03} \end{array}\right] \]

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

input = [
[0.0, 0.1, 0.2, 0.3],
[1.0, 1.1, 1.2, 1.3],
[2.0, 2.1, 2.2, 2.3]
]#shape [3,4]
input = torch.tensor(input)
length = torch.LongTensor([
[2,2,2,2],
[1,1,1,1],
[0,1,2,0]
])#[3,4]
out = torch.gather(input, dim=1, index=length)
print(out)


# 结果
# tensor([[0.2000, 0.2000, 0.2000, 0.2000],
# [1.1000, 1.1000, 1.1000, 1.1000],
# [2.0000, 2.1000, 2.2000, 2.0000]])

\[ 取值索引矩阵 = \left[\begin{array}{llll} \mathrm{X}_{02} & \mathrm{X}_{02} & \mathrm{X}_{02} & \mathrm{X}_{02} \\ \mathrm{X}_{11} & \mathrm{X}_{11} & \mathrm{X}_{11} & \mathrm{X}_{11} \\ \mathrm{X}_{20} & \mathrm{X}_{21} & \mathrm{X}_{22} & \mathrm{X}_{20} \end{array}\right] \]