相对位置2d矩阵和kron运算的思考

devtools/2025/3/26 11:18:57/

文章目录

  • 1. 相对位置矩阵2d
  • 2. kron运算

1. 相对位置矩阵2d

在swin-transformer中,我们会计算每个patch之间的相对位置,那么我们看到有一连串的拉伸和相减,直接贴代码:

python">import torch
import torch.nn as nntorch.set_printoptions(precision=3, sci_mode=False,threshold=torch.inf)if __name__ == "__main__":run_code = 2x_len = 5y_len = 5x_tensor = torch.arange(x_len)y_tensor = torch.arange(y_len)x_meshgrid, y_meshgrid = torch.meshgrid(x_tensor, y_tensor)print(f"x_tensor=\n{x_tensor}")print(f"y_tensor=\n{y_tensor}")print(f"x_meshgrid=\n{x_meshgrid}")print(f"x_meshgrid.shape=\n{x_meshgrid.shape}")print(f"y_meshgrid.shape=\n{y_meshgrid.shape}")print(f"y_meshgrid=\n{y_meshgrid}")stack_meshgrid = torch.stack(torch.meshgrid(x_tensor, y_tensor))print(f"stack_meshgrid.shape=\n{stack_meshgrid.shape}")print(f"stack_meshgrid=\n{stack_meshgrid}")stack_meshgrid_flatten = torch.flatten(stack_meshgrid, 1)print(f"stack_meshgrid_flatten.shape=\n{stack_meshgrid_flatten.shape}")print(f"stack_meshgrid_flatten=\n{stack_meshgrid_flatten}")stack_meshgrid_flatten_1 = stack_meshgrid_flatten[:, None, :]stack_meshgrid_flatten_2 = stack_meshgrid_flatten[:, :, None]relative_coords_bias = stack_meshgrid_flatten_2 - stack_meshgrid_flatten_1print(f"stack_meshgrid_flatten_1=\n{stack_meshgrid_flatten_1}")print(f"stack_meshgrid_flatten_2=\n{stack_meshgrid_flatten_2}")print(f"relative_coords_bias=\n{relative_coords_bias}")relative_coords_bias[0, :, :] += x_lenrelative_coords_bias[1, :, :] += y_lenprint(f"relative_coords_bias=\n{relative_coords_bias}")
  • result:
python">x_tensor=
tensor([0, 1, 2, 3, 4])
y_tensor=
tensor([0, 1, 2, 3, 4])
x_meshgrid=
tensor([[0, 0, 0, 0, 0],[1, 1, 1, 1, 1],[2, 2, 2, 2, 2],[3, 3, 3, 3, 3],[4, 4, 4, 4, 4]])
x_meshgrid.shape=
torch.Size([5, 5])
y_meshgrid.shape=
torch.Size([5, 5])
y_meshgrid=
tensor([[0, 1, 2, 3, 4],[0, 1, 2, 3, 4],[0, 1, 2, 3, 4],[0, 1, 2, 3, 4],[0, 1, 2, 3, 4]])
stack_meshgrid.shape=
torch.Size([2, 5, 5])
stack_meshgrid=
tensor([[[0, 0, 0, 0, 0],[1, 1, 1, 1, 1],[2, 2, 2, 2, 2],[3, 3, 3, 3, 3],[4, 4, 4, 4, 4]],[[0, 1, 2, 3, 4],[0, 1, 2, 3, 4],[0, 1, 2, 3, 4],[0, 1, 2, 3, 4],[0, 1, 2, 3, 4]]])
stack_meshgrid_flatten.shape=
torch.Size([2, 25])
stack_meshgrid_flatten=
tensor([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4,4],[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3,4]])
stack_meshgrid_flatten_1=
tensor([[[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4,4, 4]],[[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2,3, 4]]])
stack_meshgrid_flatten_2=
tensor([[[0],[0],[0],[0],[0],[1],[1],[1],[1],[1],[2],[2],[2],[2],[2],[3],[3],[3],[3],[3],[4],[4],[4],[4],[4]],[[0],[1],[2],[3],[4],[0],[1],[2],[3],[4],[0],[1],[2],[3],[4],[0],[1],[2],[3],[4],[0],[1],[2],[3],[4]]])
relative_coords_bias=
tensor([[[ 0,  0,  0,  0,  0, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -3, -3,-3, -3, -3, -4, -4, -4, -4, -4],[ 0,  0,  0,  0,  0, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -3, -3,-3, -3, -3, -4, -4, -4, -4, -4],[ 0,  0,  0,  0,  0, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -3, -3,-3, -3, -3, -4, -4, -4, -4, -4],[ 0,  0,  0,  0,  0, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -3, -3,-3, -3, -3, -4, -4, -4, -4, -4],[ 0,  0,  0,  0,  0, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -3, -3,-3, -3, -3, -4, -4, -4, -4, -4],[ 1,  1,  1,  1,  1,  0,  0,  0,  0,  0, -1, -1, -1, -1, -1, -2, -2,-2, -2, -2, -3, -3, -3, -3, -3],[ 1,  1,  1,  1,  1,  0,  0,  0,  0,  0, -1, -1, -1, -1, -1, -2, -2,-2, -2, -2, -3, -3, -3, -3, -3],[ 1,  1,  1,  1,  1,  0,  0,  0,  0,  0, -1, -1, -1, -1, -1, -2, -2,-2, -2, -2, -3, -3, -3, -3, -3],[ 1,  1,  1,  1,  1,  0,  0,  0,  0,  0, -1, -1, -1, -1, -1, -2, -2,-2, -2, -2, -3, -3, -3, -3, -3],[ 1,  1,  1,  1,  1,  0,  0,  0,  0,  0, -1, -1, -1, -1, -1, -2, -2,-2, -2, -2, -3, -3, -3, -3, -3],[ 2,  2,  2,  2,  2,  1,  1,  1,  1,  1,  0,  0,  0,  0,  0, -1, -1,-1, -1, -1, -2, -2, -2, -2, -2],[ 2,  2,  2,  2,  2,  1,  1,  1,  1,  1,  0,  0,  0,  0,  0, -1, -1,-1, -1, -1, -2, -2, -2, -2, -2],[ 2,  2,  2,  2,  2,  1,  1,  1,  1,  1,  0,  0,  0,  0,  0, -1, -1,-1, -1, -1, -2, -2, -2, -2, -2],[ 2,  2,  2,  2,  2,  1,  1,  1,  1,  1,  0,  0,  0,  0,  0, -1, -1,-1, -1, -1, -2, -2, -2, -2, -2],[ 2,  2,  2,  2,  2,  1,  1,  1,  1,  1,  0,  0,  0,  0,  0, -1, -1,-1, -1, -1, -2, -2, -2, -2, -2],[ 3,  3,  3,  3,  3,  2,  2,  2,  2,  2,  1,  1,  1,  1,  1,  0,  0,0,  0,  0, -1, -1, -1, -1, -1],[ 3,  3,  3,  3,  3,  2,  2,  2,  2,  2,  1,  1,  1,  1,  1,  0,  0,0,  0,  0, -1, -1, -1, -1, -1],[ 3,  3,  3,  3,  3,  2,  2,  2,  2,  2,  1,  1,  1,  1,  1,  0,  0,0,  0,  0, -1, -1, -1, -1, -1],[ 3,  3,  3,  3,  3,  2,  2,  2,  2,  2,  1,  1,  1,  1,  1,  0,  0,0,  0,  0, -1, -1, -1, -1, -1],[ 3,  3,  3,  3,  3,  2,  2,  2,  2,  2,  1,  1,  1,  1,  1,  0,  0,0,  0,  0, -1, -1, -1, -1, -1],[ 4,  4,  4,  4,  4,  3,  3,  3,  3,  3,  2,  2,  2,  2,  2,  1,  1,1,  1,  1,  0,  0,  0,  0,  0],[ 4,  4,  4,  4,  4,  3,  3,  3,  3,  3,  2,  2,  2,  2,  2,  1,  1,1,  1,  1,  0,  0,  0,  0,  0],[ 4,  4,  4,  4,  4,  3,  3,  3,  3,  3,  2,  2,  2,  2,  2,  1,  1,1,  1,  1,  0,  0,  0,  0,  0],[ 4,  4,  4,  4,  4,  3,  3,  3,  3,  3,  2,  2,  2,  2,  2,  1,  1,1,  1,  1,  0,  0,  0,  0,  0],[ 4,  4,  4,  4,  4,  3,  3,  3,  3,  3,  2,  2,  2,  2,  2,  1,  1,1,  1,  1,  0,  0,  0,  0,  0]],[[ 0, -1, -2, -3, -4,  0, -1, -2, -3, -4,  0, -1, -2, -3, -4,  0, -1,-2, -3, -4,  0, -1, -2, -3, -4],[ 1,  0, -1, -2, -3,  1,  0, -1, -2, -3,  1,  0, -1, -2, -3,  1,  0,-1, -2, -3,  1,  0, -1, -2, -3],[ 2,  1,  0, -1, -2,  2,  1,  0, -1, -2,  2,  1,  0, -1, -2,  2,  1,0, -1, -2,  2,  1,  0, -1, -2],[ 3,  2,  1,  0, -1,  3,  2,  1,  0, -1,  3,  2,  1,  0, -1,  3,  2,1,  0, -1,  3,  2,  1,  0, -1],[ 4,  3,  2,  1,  0,  4,  3,  2,  1,  0,  4,  3,  2,  1,  0,  4,  3,2,  1,  0,  4,  3,  2,  1,  0],[ 0, -1, -2, -3, -4,  0, -1, -2, -3, -4,  0, -1, -2, -3, -4,  0, -1,-2, -3, -4,  0, -1, -2, -3, -4],[ 1,  0, -1, -2, -3,  1,  0, -1, -2, -3,  1,  0, -1, -2, -3,  1,  0,-1, -2, -3,  1,  0, -1, -2, -3],[ 2,  1,  0, -1, -2,  2,  1,  0, -1, -2,  2,  1,  0, -1, -2,  2,  1,0, -1, -2,  2,  1,  0, -1, -2],[ 3,  2,  1,  0, -1,  3,  2,  1,  0, -1,  3,  2,  1,  0, -1,  3,  2,1,  0, -1,  3,  2,  1,  0, -1],[ 4,  3,  2,  1,  0,  4,  3,  2,  1,  0,  4,  3,  2,  1,  0,  4,  3,2,  1,  0,  4,  3,  2,  1,  0],[ 0, -1, -2, -3, -4,  0, -1, -2, -3, -4,  0, -1, -2, -3, -4,  0, -1,-2, -3, -4,  0, -1, -2, -3, -4],[ 1,  0, -1, -2, -3,  1,  0, -1, -2, -3,  1,  0, -1, -2, -3,  1,  0,-1, -2, -3,  1,  0, -1, -2, -3],[ 2,  1,  0, -1, -2,  2,  1,  0, -1, -2,  2,  1,  0, -1, -2,  2,  1,0, -1, -2,  2,  1,  0, -1, -2],[ 3,  2,  1,  0, -1,  3,  2,  1,  0, -1,  3,  2,  1,  0, -1,  3,  2,1,  0, -1,  3,  2,  1,  0, -1],[ 4,  3,  2,  1,  0,  4,  3,  2,  1,  0,  4,  3,  2,  1,  0,  4,  3,2,  1,  0,  4,  3,  2,  1,  0],[ 0, -1, -2, -3, -4,  0, -1, -2, -3, -4,  0, -1, -2, -3, -4,  0, -1,-2, -3, -4,  0, -1, -2, -3, -4],[ 1,  0, -1, -2, -3,  1,  0, -1, -2, -3,  1,  0, -1, -2, -3,  1,  0,-1, -2, -3,  1,  0, -1, -2, -3],[ 2,  1,  0, -1, -2,  2,  1,  0, -1, -2,  2,  1,  0, -1, -2,  2,  1,0, -1, -2,  2,  1,  0, -1, -2],[ 3,  2,  1,  0, -1,  3,  2,  1,  0, -1,  3,  2,  1,  0, -1,  3,  2,1,  0, -1,  3,  2,  1,  0, -1],[ 4,  3,  2,  1,  0,  4,  3,  2,  1,  0,  4,  3,  2,  1,  0,  4,  3,2,  1,  0,  4,  3,  2,  1,  0],[ 0, -1, -2, -3, -4,  0, -1, -2, -3, -4,  0, -1, -2, -3, -4,  0, -1,-2, -3, -4,  0, -1, -2, -3, -4],[ 1,  0, -1, -2, -3,  1,  0, -1, -2, -3,  1,  0, -1, -2, -3,  1,  0,-1, -2, -3,  1,  0, -1, -2, -3],[ 2,  1,  0, -1, -2,  2,  1,  0, -1, -2,  2,  1,  0, -1, -2,  2,  1,0, -1, -2,  2,  1,  0, -1, -2],[ 3,  2,  1,  0, -1,  3,  2,  1,  0, -1,  3,  2,  1,  0, -1,  3,  2,1,  0, -1,  3,  2,  1,  0, -1],[ 4,  3,  2,  1,  0,  4,  3,  2,  1,  0,  4,  3,  2,  1,  0,  4,  3,2,  1,  0,  4,  3,  2,  1,  0]]])
relative_coords_bias=
tensor([[[5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1,1, 1],[5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1,1, 1],[5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1,1, 1],[5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1,1, 1],[5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 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, 3, 2, 6, 5, 4, 3, 2, 6, 5, 4, 3, 2, 6, 5, 4, 3, 2, 6, 5, 4,3, 2],[7, 6, 5, 4, 3, 7, 6, 5, 4, 3, 7, 6, 5, 4, 3, 7, 6, 5, 4, 3, 7, 6, 5,4, 3],[8, 7, 6, 5, 4, 8, 7, 6, 5, 4, 8, 7, 6, 5, 4, 8, 7, 6, 5, 4, 8, 7, 6,5, 4],[9, 8, 7, 6, 5, 9, 8, 7, 6, 5, 9, 8, 7, 6, 5, 9, 8, 7, 6, 5, 9, 8, 7,6, 5]]])

2. kron运算

在结果中,我们发现很多重复的值,这就让我联想到kron运算。

python">import torch
import torch.nn as nntorch.set_printoptions(precision=3, sci_mode=False)if __name__ == '__main__':run_code = 0height = 5width = 5a_vector = torch.arange(width).to(torch.float).reshape(-1, 1)a_ones = torch.ones(1, width)a_matrix = a_vector @ a_onesprint(f"a_matrix=\n{a_matrix}")b_matrix = a_matrix - a_matrix.Tprint(f"b_matrix=\n{b_matrix}")b_matrix_ones = torch.ones_like(b_matrix)ab_kron = torch.kron(b_matrix,b_matrix_ones)print(f"ab_kron=\n{ab_kron}")final_ab = ab_kron+5print(f"final_ab=\n{final_ab}")
  • result:
python">a_matrix=
tensor([[0., 0., 0., 0., 0.],[1., 1., 1., 1., 1.],[2., 2., 2., 2., 2.],[3., 3., 3., 3., 3.],[4., 4., 4., 4., 4.]])
b_matrix=
tensor([[ 0., -1., -2., -3., -4.],[ 1.,  0., -1., -2., -3.],[ 2.,  1.,  0., -1., -2.],[ 3.,  2.,  1.,  0., -1.],[ 4.,  3.,  2.,  1.,  0.]])
ab_kron=
tensor([[ 0.,  0.,  0.,  0.,  0., -1., -1., -1., -1., -1., -2., -2., -2., -2.,-2., -3., -3., -3., -3., -3., -4., -4., -4., -4., -4.],[ 0.,  0.,  0.,  0.,  0., -1., -1., -1., -1., -1., -2., -2., -2., -2.,-2., -3., -3., -3., -3., -3., -4., -4., -4., -4., -4.],[ 0.,  0.,  0.,  0.,  0., -1., -1., -1., -1., -1., -2., -2., -2., -2.,-2., -3., -3., -3., -3., -3., -4., -4., -4., -4., -4.],[ 0.,  0.,  0.,  0.,  0., -1., -1., -1., -1., -1., -2., -2., -2., -2.,-2., -3., -3., -3., -3., -3., -4., -4., -4., -4., -4.],[ 0.,  0.,  0.,  0.,  0., -1., -1., -1., -1., -1., -2., -2., -2., -2.,-2., -3., -3., -3., -3., -3., -4., -4., -4., -4., -4.],[ 1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0., -1., -1., -1., -1.,-1., -2., -2., -2., -2., -2., -3., -3., -3., -3., -3.],[ 1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0., -1., -1., -1., -1.,-1., -2., -2., -2., -2., -2., -3., -3., -3., -3., -3.],[ 1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0., -1., -1., -1., -1.,-1., -2., -2., -2., -2., -2., -3., -3., -3., -3., -3.],[ 1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0., -1., -1., -1., -1.,-1., -2., -2., -2., -2., -2., -3., -3., -3., -3., -3.],[ 1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0., -1., -1., -1., -1.,-1., -2., -2., -2., -2., -2., -3., -3., -3., -3., -3.],[ 2.,  2.,  2.,  2.,  2.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,0., -1., -1., -1., -1., -1., -2., -2., -2., -2., -2.],[ 2.,  2.,  2.,  2.,  2.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,0., -1., -1., -1., -1., -1., -2., -2., -2., -2., -2.],[ 2.,  2.,  2.,  2.,  2.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,0., -1., -1., -1., -1., -1., -2., -2., -2., -2., -2.],[ 2.,  2.,  2.,  2.,  2.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,0., -1., -1., -1., -1., -1., -2., -2., -2., -2., -2.],[ 2.,  2.,  2.,  2.,  2.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,0., -1., -1., -1., -1., -1., -2., -2., -2., -2., -2.],[ 3.,  3.,  3.,  3.,  3.,  2.,  2.,  2.,  2.,  2.,  1.,  1.,  1.,  1.,1.,  0.,  0.,  0.,  0.,  0., -1., -1., -1., -1., -1.],[ 3.,  3.,  3.,  3.,  3.,  2.,  2.,  2.,  2.,  2.,  1.,  1.,  1.,  1.,1.,  0.,  0.,  0.,  0.,  0., -1., -1., -1., -1., -1.],[ 3.,  3.,  3.,  3.,  3.,  2.,  2.,  2.,  2.,  2.,  1.,  1.,  1.,  1.,1.,  0.,  0.,  0.,  0.,  0., -1., -1., -1., -1., -1.],[ 3.,  3.,  3.,  3.,  3.,  2.,  2.,  2.,  2.,  2.,  1.,  1.,  1.,  1.,1.,  0.,  0.,  0.,  0.,  0., -1., -1., -1., -1., -1.],[ 3.,  3.,  3.,  3.,  3.,  2.,  2.,  2.,  2.,  2.,  1.,  1.,  1.,  1.,1.,  0.,  0.,  0.,  0.,  0., -1., -1., -1., -1., -1.],[ 4.,  4.,  4.,  4.,  4.,  3.,  3.,  3.,  3.,  3.,  2.,  2.,  2.,  2.,2.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.],[ 4.,  4.,  4.,  4.,  4.,  3.,  3.,  3.,  3.,  3.,  2.,  2.,  2.,  2.,2.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.],[ 4.,  4.,  4.,  4.,  4.,  3.,  3.,  3.,  3.,  3.,  2.,  2.,  2.,  2.,2.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.],[ 4.,  4.,  4.,  4.,  4.,  3.,  3.,  3.,  3.,  3.,  2.,  2.,  2.,  2.,2.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.],[ 4.,  4.,  4.,  4.,  4.,  3.,  3.,  3.,  3.,  3.,  2.,  2.,  2.,  2.,2.,  1.,  1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.]])
final_ab=
tensor([[5., 5., 5., 5., 5., 4., 4., 4., 4., 4., 3., 3., 3., 3., 3., 2., 2., 2.,2., 2., 1., 1., 1., 1., 1.],[5., 5., 5., 5., 5., 4., 4., 4., 4., 4., 3., 3., 3., 3., 3., 2., 2., 2.,2., 2., 1., 1., 1., 1., 1.],[5., 5., 5., 5., 5., 4., 4., 4., 4., 4., 3., 3., 3., 3., 3., 2., 2., 2.,2., 2., 1., 1., 1., 1., 1.],[5., 5., 5., 5., 5., 4., 4., 4., 4., 4., 3., 3., 3., 3., 3., 2., 2., 2.,2., 2., 1., 1., 1., 1., 1.],[5., 5., 5., 5., 5., 4., 4., 4., 4., 4., 3., 3., 3., 3., 3., 2., 2., 2.,2., 2., 1., 1., 1., 1., 1.],[6., 6., 6., 6., 6., 5., 5., 5., 5., 5., 4., 4., 4., 4., 4., 3., 3., 3.,3., 3., 2., 2., 2., 2., 2.],[6., 6., 6., 6., 6., 5., 5., 5., 5., 5., 4., 4., 4., 4., 4., 3., 3., 3.,3., 3., 2., 2., 2., 2., 2.],[6., 6., 6., 6., 6., 5., 5., 5., 5., 5., 4., 4., 4., 4., 4., 3., 3., 3.,3., 3., 2., 2., 2., 2., 2.],[6., 6., 6., 6., 6., 5., 5., 5., 5., 5., 4., 4., 4., 4., 4., 3., 3., 3.,3., 3., 2., 2., 2., 2., 2.],[6., 6., 6., 6., 6., 5., 5., 5., 5., 5., 4., 4., 4., 4., 4., 3., 3., 3.,3., 3., 2., 2., 2., 2., 2.],[7., 7., 7., 7., 7., 6., 6., 6., 6., 6., 5., 5., 5., 5., 5., 4., 4., 4.,4., 4., 3., 3., 3., 3., 3.],[7., 7., 7., 7., 7., 6., 6., 6., 6., 6., 5., 5., 5., 5., 5., 4., 4., 4.,4., 4., 3., 3., 3., 3., 3.],[7., 7., 7., 7., 7., 6., 6., 6., 6., 6., 5., 5., 5., 5., 5., 4., 4., 4.,4., 4., 3., 3., 3., 3., 3.],[7., 7., 7., 7., 7., 6., 6., 6., 6., 6., 5., 5., 5., 5., 5., 4., 4., 4.,4., 4., 3., 3., 3., 3., 3.],[7., 7., 7., 7., 7., 6., 6., 6., 6., 6., 5., 5., 5., 5., 5., 4., 4., 4.,4., 4., 3., 3., 3., 3., 3.],[8., 8., 8., 8., 8., 7., 7., 7., 7., 7., 6., 6., 6., 6., 6., 5., 5., 5.,5., 5., 4., 4., 4., 4., 4.],[8., 8., 8., 8., 8., 7., 7., 7., 7., 7., 6., 6., 6., 6., 6., 5., 5., 5.,5., 5., 4., 4., 4., 4., 4.],[8., 8., 8., 8., 8., 7., 7., 7., 7., 7., 6., 6., 6., 6., 6., 5., 5., 5.,5., 5., 4., 4., 4., 4., 4.],[8., 8., 8., 8., 8., 7., 7., 7., 7., 7., 6., 6., 6., 6., 6., 5., 5., 5.,5., 5., 4., 4., 4., 4., 4.],[8., 8., 8., 8., 8., 7., 7., 7., 7., 7., 6., 6., 6., 6., 6., 5., 5., 5.,5., 5., 4., 4., 4., 4., 4.],[9., 9., 9., 9., 9., 8., 8., 8., 8., 8., 7., 7., 7., 7., 7., 6., 6., 6.,6., 6., 5., 5., 5., 5., 5.],[9., 9., 9., 9., 9., 8., 8., 8., 8., 8., 7., 7., 7., 7., 7., 6., 6., 6.,6., 6., 5., 5., 5., 5., 5.],[9., 9., 9., 9., 9., 8., 8., 8., 8., 8., 7., 7., 7., 7., 7., 6., 6., 6.,6., 6., 5., 5., 5., 5., 5.],[9., 9., 9., 9., 9., 8., 8., 8., 8., 8., 7., 7., 7., 7., 7., 6., 6., 6.,6., 6., 5., 5., 5., 5., 5.],[9., 9., 9., 9., 9., 8., 8., 8., 8., 8., 7., 7., 7., 7., 7., 6., 6., 6.,6., 6., 5., 5., 5., 5., 5.]])

http://www.ppmy.cn/devtools/171333.html

相关文章

【C++】C++中的动态内存分配(new和delete)

C中的动态内存分配(分配堆空间) 1. C语言与C动态内存分配2. 使用3.malloc和new有什么区别示例代码: 1. C语言与C动态内存分配 C语言 malloc calloc realloc free 函数 C new关键字分配堆空间 delete关键字释放堆空间 2. 使用 第一种&#…

深度优先搜索(DFS)在排列组合问题中的应用详解:C++实现与优化

一、排列问题&#xff08;Permutations&#xff09; 目标&#xff1a;生成所有可能的排列&#xff08;元素顺序不同视为不同结果&#xff09;。 示例&#xff1a;输入 [1,2,3]&#xff0c;输出所有长度为3的排列&#xff0c;共6种。 C实现代码 #include <iostream> #i…

【网络】HTTP 和 HTTPS

HTTP&#xff08;HyperText Transfer Protocol&#xff09;和 HTTPS&#xff08;HTTP Secure&#xff09;是互联网数据通信的核心协议&#xff0c;作为前端开发者&#xff0c;深入理解其原理和细节对性能优化、安全加固和问题排查至关重要。 一.HTTP核心概念 1.请求方法 GET…

力扣13. 罗马数字转整数:Java多种解法详解

力扣13. 罗马数字转整数&#xff1a;Java多种解法详解 题目描述 罗马数字由以下字符构成&#xff1a;I, V, X, L, C, D, M&#xff0c;分别对应数值1, 5, 10, 50, 100, 500, 1000。 特殊规则&#xff1a;当小值字符位于大值字符左侧时&#xff0c;表示减法&#xff08;如 IV4…

linux0.11内核源码修仙传第九章——时间初始化

&#x1f680; 前言 本文主要解释了计算机断电重启后能准确读取时间的原因&#xff0c;内容很短&#xff0c;对应于书中的第17回。希望各位给个三连&#xff0c;拜托啦&#xff0c;这对我真的很重要&#xff01;&#xff01;&#xff01; 目录 &#x1f680; 前言&#x1f3c6;…

【微服务架构】本地负载均衡的实现(基于随机算法)

前言 负载均衡 概念&#xff1a;一种将网络流量或业务请求均匀分配到多个服务器或服务实例上的技术&#xff0c;旨在提高系统的可用性、性能和可伸缩性。作用&#xff1a; 提高性能&#xff1a;通过将请求分散到多个实例上&#xff0c;避免单个实例因请求过多而过载&#xff…

GMII 接口

文章目录 概述硬件拓扑GMII 接口站管理接口发送数据时序接收数据时序参考 本文为笔者学习以太网对网上资料归纳整理所做的笔记&#xff0c;文末均附有参考链接&#xff0c;如侵权&#xff0c;请联系删除。 概述 GMII 是千兆网的MII接口&#xff0c;这个也有相应的 RGMII 接口&…

笔试专题(三)

文章目录 字符串中找出连续最长的数字串题解代码 拼三角题解代码 字符串中找出连续最长的数字串 题目链接 题解 1. 考察双指针 模拟 2. 算法思路&#xff1a;给定一个i 0&#xff0c;让i&#xff0c;如果遇到数字字符就创建一个变量j i&#xff0c;让j去遍历&#xff0c…