当前位置: 首页 > article >正文

矩阵乘法实现填充矩阵F.padding

文章目录

  • 1. 理论
  • 2. 代码

1. 理论

输入: 有一个矩阵A ,给定需要填充零的方式,左1,右2,上3,下4

matrix=
tensor([[1., 9., 7.],
        [9., 3., 5.]])
left=1,right=2,top_r=3,button_r=4
my_result=
tensor([[0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 1., 9., 7., 0., 0.],
        [0., 9., 3., 5., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.]])

2. 代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :my_eye_new.py
# @Time      :2024/11/30 10:08
# @Author    :Jason Zhang
import torch
from torch import nn

torch.set_printoptions(sci_mode=False, precision=3)
torch.manual_seed(2323)

if __name__ == "__main__":
    run_code = 0
    left_c = 1
    right_c = 2
    top_r = 3
    button_r = 4
    matrix_row = 2
    matrix_column = 3
    left_cc = left_c + matrix_column
    top_rr = top_r + matrix_row
    matrix = torch.randint(1, 10, (matrix_row, matrix_column), dtype=torch.float)
    my_eye = left_c + right_c + matrix_column
    my_eye_up = top_r + matrix_row + button_r
    torch_eye = torch.eye(my_eye)
    torch_eye_up = torch.eye(my_eye_up)
    my_new = torch_eye[left_c:left_cc, :]
    my_up_one = torch_eye_up[top_r:top_rr, :].T
    my_result = my_up_one @ matrix @ my_new
    my_padding = nn.functional.pad(matrix, (1, 2, 3, 4))
    print(f"matrix=\n{matrix}")
    print(f"left={left_c},right={right_c},top_r={top_r},button_r={button_r}")
    print(f"my_result=\n{my_result}")
    print(f"my_padding=\n{my_padding}")
    check_result = torch.allclose(my_result, my_padding)
    print(f"my_result is {check_result} same with my_padding")
  • 结果:
matrix=
tensor([[1., 9., 7.],
        [9., 3., 5.]])
left=1,right=2,top_r=3,button_r=4
my_result=
tensor([[0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 1., 9., 7., 0., 0.],
        [0., 9., 3., 5., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.]])
my_padding=
tensor([[0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 1., 9., 7., 0., 0.],
        [0., 9., 3., 5., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0.]])
my_result is True same with my_padding

http://www.kler.cn/a/417717.html

相关文章:

  • Linux 安装 RabbitMQ
  • doris:临时分区
  • MySQL UNION 操作详解
  • DeepSeek AI模型本地部署指南:让技术变得简单
  • 解读“大语言模型(LLM)安全性测评基准”
  • 【论文复现】粘菌算法在最优经济排放调度中的发展与应用
  • C语言模拟实现简单链表的复盘
  • 【机器学习算法】XGBoost原理
  • 【故障处理系列--业务官网无法打开】
  • 0017. shell命令--tac
  • Milvus的索引类型
  • 【经典论文阅读】Transformer(多头注意力 编码器-解码器)
  • 打造高质量技术文档的关键要素(结合MATLAB)
  • selinux、firewalld
  • JD - HotKey:缓存热 Key 管理的高效解决方案
  • Vue + Vite + Element Plus 与 Django 进行前后端对接
  • 【系统架构设计师】高分论文:论敏捷软件开发方法及其成用
  • TYUT设计模式大题
  • 架构04-透明多级分流系统
  • LeetCode-315. Count of Smaller Numbers After Self
  • 解决因为数据变化,页面没有变化的情况 , 复习一下使用 vuex 的 modules
  • C#里怎么样使用Array.BinarySearch函数?
  • LeetCode题练习与总结:最小基因变化--433
  • 【踩坑】git中文乱码问题
  • 从0开始边做边学,用vue和python做一个博客,非规范化项目,怎么简单怎么弄,跑的起来有啥毛病解决啥毛病(一)
  • IIS管理器、Sql Server、windows操作系统,nginx