基本方法 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 ) c = np.stack([a, b], 1 ) c = np.stack([a, b], 2 )
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 ]) y = torch.tensor([4 , 5 ]) X, Y = np.meshgrid(x, y)print (X)print (Y) z = torch.tensor([6 ,7 ,8 ]) X,Y,Z = np.meshgrid(x,y,z)print (X.shape, Y.shape, Z.shape)print (X) print (Y)print (Z)
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 ]) y = torch.tensor([4 , 5 ]) X, Y = torch.meshgrid(x, y)print (X) print (Y) z = torch.tensor([6 ,7 ,8 ]) X,Y,Z = torch.meshgrid(x,y,z)print (X) print (Y)print (Z)
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 x = torch.tensor([1 , 2 , 3 ], dtype=torch.float32) l2_norm = torch.norm(x, p=2 )print (l2_norm) A = torch.tensor([[1 , 2 ], [3 , 4 ]], dtype=torch.float32) fro_norm = torch.norm(A, p='fro' )print (fro_norm) B = torch.tensor([[1 , -2 , 3 ], [-4 , 5 , -6 ]], dtype=torch.float32) l1_norm = torch.norm(B, p=1 , dim=1 )print (l1_norm)
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 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 ] ]input = torch.tensor(input ) length = torch.LongTensor([ [2 ,2 ,2 ,2 ], [1 ,1 ,1 ,1 ], [0 ,0 ,0 ,0 ], [0 ,1 ,2 ,0 ] ]) out = torch.gather(input , dim=0 , index=length)print (out)
\[ 取值索引矩阵= \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 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 ] ]input = torch.tensor(input ) length = torch.LongTensor([ [2 ,2 ,2 ,2 ], [1 ,1 ,1 ,1 ], [0 ,1 ,2 ,0 ] ]) out = torch.gather(input , dim=1 , index=length)print (out)
\[ 取值索引矩阵 = \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] \]