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

文章目录

  • [1. 理论](#1. 理论)
  • [2. 代码](#2. 代码)

1. 理论

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

python 复制代码
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. 代码

python 复制代码
#!/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")
  • 结果:
python 复制代码
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
相关推荐
Blossom.1183 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
scdifsn5 小时前
动手学深度学习12.7. 参数服务器-笔记&练习(PyTorch)
pytorch·笔记·深度学习·分布式计算·数据并行·参数服务器
海盗儿6 小时前
Attention Is All You Need (Transformer) 以及Transformer pytorch实现
pytorch·深度学习·transformer
_Itachi__6 小时前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
不忘不弃6 小时前
计算矩阵A和B的乘积
线性代数·算法·矩阵
不爱写代码的玉子6 小时前
HALCON透视矩阵
人工智能·深度学习·线性代数·算法·计算机视觉·矩阵·c#
sbc-study6 小时前
PCDF (Progressive Continuous Discrimination Filter)模块构建
人工智能·深度学习·计算机视觉
小喵喵生气气7 小时前
Python60日基础学习打卡Day46
深度学习·机器学习
红衣小蛇妖9 小时前
神经网络-Day44
人工智能·深度学习·神经网络
且慢.5899 小时前
Python_day47
python·深度学习·计算机视觉