矩阵乘法实现填充矩阵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
相关推荐
我不是小upper17 分钟前
anaconda、conda、pip、pytorch、torch、tensorflow到底是什么?它们之间有何联系与区别?
人工智能·pytorch·深度学习·conda·tensorflow·pip
z樾37 分钟前
Sum-rate计算
开发语言·python·深度学习
zzywxc7871 小时前
在处理大数据列表渲染时,React 虚拟列表是提升性能的关键技术,但在实际实现中常遇到渲染抖动和滚动定位偏移等问题。
前端·javascript·人工智能·深度学习·react.js·重构·ecmascript
美狐美颜sdk1 小时前
直播平台中的美白滤镜实现:美颜SDK的核心架构与性能优化指南
人工智能·深度学习·计算机视觉·美颜sdk·第三方美颜sdk·视频美颜sdk·美颜api
TT-Kun5 小时前
PyTorch基础——张量计算
人工智能·pytorch·python
老鱼说AI10 小时前
循环神经网络RNN原理精讲,详细举例!
人工智能·rnn·深度学习·神经网络·自然语言处理·语音识别
爱分享的飘哥11 小时前
第三十篇:AI的“思考引擎”:神经网络、损失与优化器的核心机制【总结前面2】
人工智能·深度学习·神经网络·优化器·损失函数·mlp·训练循环
阿男官官12 小时前
[Token]ALGM: 基于自适应局部-全局token合并的简单视觉Transformer用于高效语义分割, CVPR2024
人工智能·深度学习·transformer·语义分割
李元豪13 小时前
nl2sql grpo强化学习训练,加大数据量和轮数后,准确率没提升,反而下降了,如何调整
人工智能·深度学习·机器学习
wydxry13 小时前
MOE架构详解:原理、应用与PyTorch实现
人工智能·pytorch·架构