文章目录

  • 矩阵维度
  • 矩阵乘法
    • torch.mm
    • torch.bmm
    • torch.matmul

矩阵维度

首先需要确认多维矩阵每个维度的对应含义。

a = torch.tensor([[[3.], [1.]], [[4.], [0.]], [[2.], [1.]]])

这是一个三维矩阵,他的size是:3,2,1
很容易理解和记忆,最外层[]内的元素数即size的第一个参数,第二层对应第二个参数,以此类推。
那么其中元素对应关系就有了:
a000=3a_{000}=3a000​=3a210=1a_{210}=1 a210​=1
整个矩阵就有:
[[31][40][21]]\begin{bmatrix} \begin{bmatrix}3 \\ 1\end{bmatrix} \begin{bmatrix}4 \\ 0\end{bmatrix} \begin{bmatrix}2 \\ 1\end{bmatrix} \end{bmatrix} [[31​][40​][21​]​]
正常来讲,最内侧的两维可以用矩阵行列表示,外面就只能通过嵌套矩阵表示了。
最内侧为行,次内侧为列

矩阵乘法

pytorch的矩阵乘法中,主要内容为2维×2维。也就是用torch.mm这个函数。

torch.mm

直接上例子:

import torch
b = torch.tensor([[2., 3.]])
c = torch.tensor([[3.], [1.]])
print(b)
print(c)
out1 = torch.mm(b, c)
print(out1)
# ### output
tensor([[2., 3.]])
tensor([[3.],[1.]])
tensor([[9.]])

[23]×[31]=[9]\begin{bmatrix}2 \ 3 \end{bmatrix} ×\begin{bmatrix}3 \\ 1\end{bmatrix}=[9][2 3​]×[31​]=[9]
先理清矩阵维度,这个就很容易理解了。

torch.bmm

矩阵批处理乘法。怎么命名不重要,功能就是两批2维矩阵对应相乘。

a = torch.tensor([[[2., 3.], [1., 2.]], [[3., 4.], [0., 5.]]])
w = torch.tensor([[[3.], [1.]], [[2.], [4.]]])
print(a)
print(w)
out = torch.bmm(a, w)
print(out)tensor([[[2., 3.],[1., 2.]],[[3., 4.],[0., 5.]]])
tensor([[[3.],[1.]],[[2.],[4.]]])
tensor([[[ 9.],[ 5.]],[[22.],[20.]]])

torch.matmul

这个我只知道在二维时与mm结果一样,高维的更复杂,但是没有深入研究。

pytorch矩阵乘法mm,bmm相关推荐

  1. Pytorch矩阵乘法之torch.mul() 、 torch.mm() 及torch.matmul()的区别

    torch.mul() . torch.mm() 及torch.matmul()的区别 一.简介 torch.mul(a, b) 是矩阵a和b对应位相乘,a和b的维度必须相等,比如a的维度是(1, 2 ...

  2. pytorch矩阵乘法总结

    文章目录 点乘 `torch.mul(a,b)` 二维矩阵乘 `torch.mm(a,b)` 三维矩阵乘 `torch.bmm(`a,b) 高维矩阵乘 `torch.matmul(a,b)` 点乘 t ...

  3. [PyTorch] 矩阵乘法

    参考 『PyTorch』矩阵乘法总结 1. * 两个张量在对应的位置上进行数值相乘. x = torch.randn(2, 2) y = x * x 2. torch.mm() 二维矩阵乘法 x = ...

  4. pytoch矩阵乘法torch.bmm

    torch.bmm函数可实现矩阵乘法.pytorch官方文档地址:torch.bmm - PyTorch 1.10.1 documentationhttps://pytorch.org/docs/st ...

  5. pytorch 矩阵和向量乘法

    向量 点乘:又叫做点积.内积.数量积.标量积,向量a[a1,a2,...,an]和向量b[b1,b2b...,bn]点乘的结果是一个标量,记作a.b: 得到一个值. 叉乘:又叫向量积.外积.叉积,叉乘 ...

  6. pytorch matmul和mm和bmm区别

    pytorch中matmul和mm和bmm区别 matmul mm bmm 结论 先看下官网上对这三个函数的介绍. matmul mm bmm 顾名思义, 就是两个batch矩阵乘法. 结论 从官方文 ...

  7. 【pytorch】torch.mm,torch.bmm以及torch.matmul的使用

    torch.mm torch.mm是两个矩阵相乘,即两个二维的张量相乘 如下面的例子 mat1 = torch.randn(2,3) print("mat1=", mat1)mat ...

  8. pytorch 内积 矩阵乘法

    1维张量内积-torch.dot() 内积返回的是一个值,如果都归一化了,可以看做相似度. torch.dot(input, tensor) → Tensor #计算两个张量的点积(内积) #官方提示 ...

  9. Pytorch:矩阵乘法总结

    1.矩阵相乘 (1)二维矩阵乘法:torch.mm(mat1, mat2, out=None) → Tensor 该函数一般只用来计算两个二维矩阵的矩阵乘法,并且不支持broadcast操作. 代码例 ...

最新文章

  1. Redis 主库宕机如何快速恢复?面试必问!
  2. 万字长文 | 美团王慧文,王兴14年的带刀护卫
  3. 找不到托盘菜单配置文件_随手在仓库捡的木托盘,简单改造一下,10件家具不用买了...
  4. 苏宁易购:Hadoop失宠前提是出现更强替代品
  5. I - Defeat the Enemy UVALive - 7146 二分 + 贪心
  6. 使用OpenCV-python实现手写数字识别
  7. Jupyter Notebook界面也可以如此炫酷?有人把Notebook玩出了新花样
  8. CC2540开发板学习笔记(五)——串口通信
  9. jieba库(jieba库的介绍以及分词原理,jieba的三种模式和常用函数,利用Jieba模块进行中文词语的统计)
  10. MP3比特率编码模式
  11. H264/H265码流的编码码率设置
  12. 自定义view之刻度尺
  13. composer下面composer.lock有什么用处 以及 如何优雅地删除它
  14. Qt::Painter 详解
  15. SAS学习笔记之《SAS编程与数据挖掘商业案例》(3)变量操作、观测值操作、SAS数据集管理
  16. web网页设计—— 指环王:护戒使者(13页) 电影网页设计 在线电影制作 个人设计web前端大作业
  17. python基于模糊推理的智能家居安防系统设计
  18. 内存完整性已关闭,你的设备可能易受攻击已解决之处理方法
  19. Error:1045, Access denied for user 'root'@'localhost' (using password: YES) (Phon3.6+Mysql8.1 )
  20. 2016年4月18号***学习总结

热门文章

  1. nginx无证书代理http/https协议两种解决方案——筑梦之路
  2. HiTool工具烧录rootfs过程
  3. C语言Matrix编程题——[Recursion]D. Liang 8.1 Computing factorials
  4. 这儿有你所需要的所有关于科研的网址
  5. python爬考研_Python爬取考研必备单词
  6. 【T+】畅捷通T+选项设置界面没有“数据精度”调整项
  7. python视频教程410集免费_Python Web开发—进阶提升 490集超强Python视频教程 真正零基础学习Python视频教程...
  8. 江苏中理网络科技有限公司介绍
  9. html转换pdf java_java转pdf(html转为pdf)
  10. C#【必备技能篇】使用批处理脚本(.bat文件)打开控制台应用程序