torch_bmm验算及代码测试

文章目录

  • [1. torch_bmm](#1. torch_bmm)
  • [2. pytorch源码](#2. pytorch源码)

1. torch_bmm

torch.bmm的作用是基于batch_size的矩阵乘法,torch.bmm的作用是对应batch位置的矩阵相乘,比如,

  • mat1的第1个位置和mat2的第1个位置进行矩阵相乘得到mat3的第1个位置
  • mat1的第2个位置和mat2的第2个位置进行矩阵相乘得到mat3的第2个位置

2. pytorch源码

python 复制代码
import torch
import torch.nn as nn
import torch.nn.functional as F

torch.set_printoptions(precision=3, sci_mode=False)

if __name__ == "__main__":
    run_code = 0
    batch_size = 2
    mat1_h = 3
    mat1_w = 4
    mat1_total = batch_size * mat1_w * mat1_h
    mat2_h = 4
    mat2_w = 5
    mat2_total = batch_size * mat2_w * mat2_h
    mat1 = torch.arange(mat1_total).reshape((batch_size, mat1_h, mat1_w))
    mat2 = torch.arange(mat2_total).reshape((batch_size, mat2_h, mat2_w))
    mat3 = torch.bmm(mat1, mat2)
    print(f"mat1=\n{mat1}")
    print(f"mat2=\n{mat2}")
    print(f"mat3=\n{mat3}")
  • 结果:
python 复制代码
mat1=
tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]],

        [[12, 13, 14, 15],
         [16, 17, 18, 19],
         [20, 21, 22, 23]]])
mat2=
tensor([[[ 0,  1,  2,  3,  4],
         [ 5,  6,  7,  8,  9],
         [10, 11, 12, 13, 14],
         [15, 16, 17, 18, 19]],

        [[20, 21, 22, 23, 24],
         [25, 26, 27, 28, 29],
         [30, 31, 32, 33, 34],
         [35, 36, 37, 38, 39]]])
mat3=
tensor([[[  70,   76,   82,   88,   94],
         [ 190,  212,  234,  256,  278],
         [ 310,  348,  386,  424,  462]],

        [[1510, 1564, 1618, 1672, 1726],
         [1950, 2020, 2090, 2160, 2230],
         [2390, 2476, 2562, 2648, 2734]]])
相关推荐
凉冰不加冰几秒前
机器学习系统详解
人工智能·机器学习
诚思报告YH几秒前
普及化专业级3D扫描设备(三角结构光技术)市场洞察:未来六年复合年均增长率(CAGR)为4.6%
大数据·人工智能
橙-极纪元1 分钟前
AI代码生产部署安全标准作业程序(SOP)的附件1:风险评估矩阵
人工智能·安全·矩阵
SunnyRivers1 分钟前
理解人工智能与大语言模型(LLMs)
人工智能·大语言模型
好家伙VCC2 分钟前
**发散创新:用 Rust构建多智能体系统,让分布式协作更高效**在人工智能快速演进的今天,**多智能体系统(
java·人工智能·分布式·python·rust
梦幻精灵_cq2 分钟前
*终端渲染天花板:文心道法解码——闲聊终端渲染状态一统江山
python
yuanmenghao3 分钟前
Linux 性能实战 | 第 18 篇:ltrace 与库函数性能分析
linux·python·性能优化
格林威3 分钟前
Baumer相机汽车雨刮胶条磨损检测:实现寿命预测的 6 个关键技术,附 OpenCV+Halcon 实战代码!
人工智能·opencv·计算机视觉·汽车·视觉检测·工业相机·堡盟相机
ValhallaCoder3 分钟前
hot100-图论
数据结构·python·算法·图论
陈天伟教授6 分钟前
人工智能应用- 人机对战:01. AI 游戏
人工智能·深度学习·神经网络·游戏·自然语言处理·机器翻译