矩阵乘法实现填充矩阵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
相关推荐
求真求知的糖葫芦2 分钟前
微波工程4.2节阻抗与导纳矩阵学习(自用)
笔记·学习·线性代数·矩阵·射频工程
有Li32 分钟前
学习通过皮层发育连续性迁移实现全生命周期脑解剖对应/文献速递-基于人工智能的医学影像技术
人工智能·深度学习·机器学习
狮子座明仔1 小时前
Plan-and-Act:让AI智能体学会“先想后做“
人工智能·深度学习·语言模型·自然语言处理
sonadorje2 小时前
矩阵的“内积”和“乘法”
人工智能·机器学习·矩阵
lixin5565563 小时前
基于迁移学习的图像风格增强器
java·人工智能·pytorch·python·深度学习·语言模型
byzh_rc3 小时前
[数学建模从入门到入土] 评价模型
网络·人工智能·深度学习·数学建模·回归·ar
Yngz_Miao4 小时前
【深度学习】语义分割损失函数之SemScal Loss
人工智能·深度学习·语义分割·损失函数·semscalloss
Dingdangcat864 小时前
YOLOv26_数字万用表端口连接检测与识别_基于深度学习的自动识别系统
人工智能·深度学习·yolo
Maỿbe4 小时前
重走力扣hot的矩阵
算法·leetcode·矩阵
新缸中之脑5 小时前
微调 BERT 实现命名实体识别
人工智能·深度学习·bert