torch.tile 手动实现 kron+矩阵乘法

文章目录

  • [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
相关推荐
香蕉鼠片2 小时前
数字化图像的过程
人工智能·深度学习·计算机视觉
lqqjuly2 小时前
深度学习理论:从神经网络到Transformer—前馈网络、反向传播、注意力机制与训练
深度学习·神经网络·transformer
chsmiao3 小时前
张量(Tensor)
深度学习·ai编程
chsmiao4 小时前
深度学习之线性代数
人工智能·深度学习·线性代数
HyperAI超神经6 小时前
MiniCPM5-1B采用RL+OPD训练,多项复杂任务达SOTA;面向复杂医疗业务自动化:医疗智能体评测数据集 CHI-Bench
人工智能·深度学习·ai·计算化学
一个王同学6 小时前
从零到一 | CV转多模态大模型 | week12 | 整理 MiniLLaVA 工程与文档
人工智能·深度学习·算法·机器学习·计算机视觉
chsmiao6 小时前
深度学习之微积分
人工智能·深度学习
阳明山水6 小时前
LightGBM为何胜过Prophet做销量预测
人工智能·深度学习·机器学习·微信公众平台·微信开放平台
JeJe同学6 小时前
LabelImg报错:IndexError: list index out of range 解决方法
深度学习·目标检测
zhangfeng11337 小时前
2021-2026 年全球 传统厂家AI 算力卡 GPU 前沿技术研究报告
人工智能·深度学习·语言模型·gpu算力·芯片