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
相关推荐
mengyoufengyu7 分钟前
DeepSeek11-Ollama + Open WebUI 搭建本地 RAG 知识库全流程指南
人工智能·深度学习·deepseek
vlln1 小时前
2025年与2030年AI及AI智能体 (Agent) 市场份额分析报告
人工智能·深度学习·神经网络·ai
GiantGo1 小时前
信息最大化(Information Maximization)
深度学习·无监督学习·信息最大化
Blossom.1188 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
scdifsn10 小时前
动手学深度学习12.7. 参数服务器-笔记&练习(PyTorch)
pytorch·笔记·深度学习·分布式计算·数据并行·参数服务器
海盗儿11 小时前
Attention Is All You Need (Transformer) 以及Transformer pytorch实现
pytorch·深度学习·transformer
cnbestec11 小时前
Xela矩阵三轴触觉传感器的工作原理解析与应用场景
人工智能·线性代数·触觉传感器
_Itachi__11 小时前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
不忘不弃11 小时前
计算矩阵A和B的乘积
线性代数·算法·矩阵
不爱写代码的玉子11 小时前
HALCON透视矩阵
人工智能·深度学习·线性代数·算法·计算机视觉·矩阵·c#