矩阵乘法实现填充矩阵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
相关推荐
西柚小萌新16 小时前
【深入浅出PyTorch】--6.2.PyTorch进阶训练技巧2
人工智能·pytorch·python
Kaydeon18 小时前
【AIGC】50倍加速!NVIDIA蒸馏算法rCM:分数正则化连续时间一致性模型的大规模扩散蒸馏
人工智能·pytorch·python·深度学习·计算机视觉·aigc
三年呀18 小时前
深度剖析Mixture of Experts(MoE)架构:从原理到实践的全面指南
人工智能·深度学习·架构·模型优化·大规模模型
mortimer19 小时前
从 Python+venv+pip 迁移到 uv 全过程 及 处理 torch + cuda 的跨平台指南
pytorch·python·macos
墨利昂19 小时前
神经网络常用激活函数公式
人工智能·深度学习·神经网络
初级炼丹师(爱说实话版)21 小时前
PGLRNet论文笔记
人工智能·深度学习·计算机视觉
无风听海1 天前
神经网络之理解梯度和方向导数
人工智能·深度学习·神经网络
zy_destiny1 天前
【工业场景】用YOLOv8实现人员打电话识别
人工智能·python·深度学习·yolo·机器学习·计算机视觉·目标跟踪
zzZ65651 天前
PyTorch 实现 MNIST 手写数字识别全流程
pytorch·深度学习
Mr.Winter`1 天前
深度强化学习 | 基于SAC算法的动态避障(ROS C++仿真)
人工智能·深度学习·神经网络·机器人·自动驾驶·ros·具身智能