3x3矩阵,1x1矩阵,3X3零矩阵融合,矩阵乘法
文章目录
- 1. 理论
- 2. python 代码
1. 理论
[ 4 2 9 2 4 3 6 9 2 ] + 9 + 0 → [ 4 2 9 2 4 3 6 9 2 ] + [ 0 0 0 0 9 0 0 0 0 ] + [ 0 0 0 0 0 0 0 0 0 ] = [ 4 2 9 2 13 3 6 9 2 ] \begin{equation} \begin{bmatrix} 4&2&9\\\\ 2&4&3\\\\ 6&9&2\end{bmatrix}+9+0\to\begin{bmatrix} 4&2&9\\\\ 2&4&3\\\\ 6&9&2\end{bmatrix}+\begin{bmatrix} 0&0&0\\\\ 0&9&0\\\\ 0&0&0\end{bmatrix}+\begin{bmatrix} 0&0&0\\\\ 0&0&0\\\\ 0&0&0\end{bmatrix}=\begin{bmatrix} 4&2&9\\\\ 2&13&3\\\\ 6&9&2\end{bmatrix} \end{equation} 426249932 +9+0→ 426249932 + 000090000 + 000000000 = 4262139932
2. python 代码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName :padding3x3.py
# @Time :2024/12/1 14:00
# @Author :Jason Zhang
import torch
from torch import nn
torch.set_printoptions(sci_mode=False, precision=3)
torch.manual_seed(455)
if __name__ == "__main__":
run_code = 0
matrix_3 = torch.randint(1, 10, (3, 3), dtype=torch.float)
matrix_1 = torch.randint(1, 10, (1, 1), dtype=torch.float)
ones_left = torch.zeros((3, 1))
ones_left[1] = 1
print(f"ones_left=\n{ones_left}")
matrix_13 = ones_left @ matrix_1 @ ones_left.T
matrix_03 = torch.zeros_like(matrix_3)
result = matrix_03 + matrix_13 + matrix_3
print(f"matrix_1=\n{matrix_1}")
print(f"matrix_13=\n{matrix_13}")
print(f"matrix_3=\n{matrix_3}")
print(f"matrix_03=\n{matrix_03}")
print(f"result=\n{result}")
ones_left=
tensor([[0.],
[1.],
[0.]])
matrix_1=
tensor([[9.]])
matrix_13=
tensor([[0., 0., 0.],
[0., 9., 0.],
[0., 0., 0.]])
matrix_3=
tensor([[4., 2., 9.],
[2., 4., 3.],
[6., 9., 2.]])
matrix_03=
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
result=
tensor([[ 4., 2., 9.],
[ 2., 13., 3.],
[ 6., 9., 2.]])