文章目录
- [1. tile](#1. tile)
- [2. pytorch](#2. pytorch)
1. tile
torch.tile 是对矩阵进行指定维度的复制数据,为了实现矩阵复制,使用kron 算子将对角矩阵I 复制后形成基于行变换和列变换的矩阵
2. pytorch
python
import torch
import torch.nn as nn
import torch.nn.functional as F
torch.set_printoptions(precision=3, sci_mode=False)
if __name__ == "__main__":
run_code = 0
a_matrix = torch.randn(2, 3)
dim0 = 4
dim1 = 3
tile_matrix = torch.tile(a_matrix, dims=(dim0, dim1))
print(f"a_matrix.shape=\n{a_matrix.shape}")
print(f"tile_matrix.shape=\n{tile_matrix.shape}")
print(f"a_matrix=\n{a_matrix}")
print(f"tile_matrix=\n{tile_matrix}")
my_one = torch.zeros(2 * dim0, 2)
my_one[0::2, 0] = 1
my_one[1::2, 1] = 1
print(f"my_one=\n{my_one}")
a_one = torch.ones(2).reshape(-1, 1)
a_row = torch.eye(2)
a_kron = torch.kron(a_one, a_row)
print(f"a_kron=\n{a_kron}")
a_co_one = torch.ones(3).reshape(1, -1)
a_column = torch.eye(3)
b_kron = torch.kron(a_co_one, a_column)
print(f"b_kron=\n{b_kron}")
my_one_result = my_one @ a_matrix @ b_kron
print(f"my_one_result=\n{my_one_result}")
m_check_result = torch.allclose(my_one_result,tile_matrix)
print(f"m_check_result={m_check_result}")
- 结果:
python
a_matrix.shape=
torch.Size([2, 3])
tile_matrix.shape=
torch.Size([8, 9])
a_matrix=
tensor([[0.340, 0.766, 0.622],
[0.366, 1.425, 0.886]])
tile_matrix=
tensor([[0.340, 0.766, 0.622, 0.340, 0.766, 0.622, 0.340, 0.766, 0.622],
[0.366, 1.425, 0.886, 0.366, 1.425, 0.886, 0.366, 1.425, 0.886],
[0.340, 0.766, 0.622, 0.340, 0.766, 0.622, 0.340, 0.766, 0.622],
[0.366, 1.425, 0.886, 0.366, 1.425, 0.886, 0.366, 1.425, 0.886],
[0.340, 0.766, 0.622, 0.340, 0.766, 0.622, 0.340, 0.766, 0.622],
[0.366, 1.425, 0.886, 0.366, 1.425, 0.886, 0.366, 1.425, 0.886],
[0.340, 0.766, 0.622, 0.340, 0.766, 0.622, 0.340, 0.766, 0.622],
[0.366, 1.425, 0.886, 0.366, 1.425, 0.886, 0.366, 1.425, 0.886]])
my_one=
tensor([[1., 0.],
[0., 1.],
[1., 0.],
[0., 1.],
[1., 0.],
[0., 1.],
[1., 0.],
[0., 1.]])
a_kron=
tensor([[1., 0.],
[0., 1.],
[1., 0.],
[0., 1.]])
b_kron=
tensor([[1., 0., 0., 1., 0., 0., 1., 0., 0.],
[0., 1., 0., 0., 1., 0., 0., 1., 0.],
[0., 0., 1., 0., 0., 1., 0., 0., 1.]])
my_one_result=
tensor([[0.340, 0.766, 0.622, 0.340, 0.766, 0.622, 0.340, 0.766, 0.622],
[0.366, 1.425, 0.886, 0.366, 1.425, 0.886, 0.366, 1.425, 0.886],
[0.340, 0.766, 0.622, 0.340, 0.766, 0.622, 0.340, 0.766, 0.622],
[0.366, 1.425, 0.886, 0.366, 1.425, 0.886, 0.366, 1.425, 0.886],
[0.340, 0.766, 0.622, 0.340, 0.766, 0.622, 0.340, 0.766, 0.622],
[0.366, 1.425, 0.886, 0.366, 1.425, 0.886, 0.366, 1.425, 0.886],
[0.340, 0.766, 0.622, 0.340, 0.766, 0.622, 0.340, 0.766, 0.622],
[0.366, 1.425, 0.886, 0.366, 1.425, 0.886, 0.366, 1.425, 0.886]])
m_check_result=True