1)如果是两个1维的,就向量内积;
2)如果两个都是2维的,就矩阵相乘
3)如果第一个是1维,第二个是2维;填充第一个使得能够和第二个参数相乘;如果第一个是2维,第二个是1维,就是矩阵和向量相乘;
例: a = torch.zeros(7,)
b= torch.zeros(7,8)

>>> torch.matmul(a,b)
tensor([0., 0., 0., 0., 0., 0., 0., 0.])

如果a是5,8维,报错。

例子2:

a = torch.zeros(8,)
b= torch.zeros(7,8)

>>> torch.matmul(b,a)
tensor([0., 0., 0., 0., 0., 0., 0.])

如果a是5,7维,报错。

4) 高维情况

i)其中一个1维,另一个N维(N>2): 类似3),即需要靠近的那个维数相同,比如(7,)和(7,8,11),又比如(7,8,11)和(11,)

ii)都高于2维,那么除掉最后两个维度之外(两个数的维数满足矩阵乘法,即,(m,p)和(p,n)),剩下的纬度满足pytorch的广播机制。比如:

# can line up trailing dimensions
>>> x=torch.empty(5,3,4,1)
>>> y=torch.empty( 3,1,1)
# x and y are broadcastable.
# 1st trailing dimension: both have size 1
# 2nd trailing dimension: y has size 1
# 3rd trailing dimension: x size == y size
# 4th trailing dimension: y dimension doesn't exist
https://pytorch.org/docs/stable/notes/broadcasting.html#broadcasting-semantics

https://pytorch.org/docs/stable/torch.html?highlight=matmul#torch.matmul

torch.matmul(tensor1tensor2out=None) → Tensor:

# can line up trailing dimensions
>>> x=torch.empty(5,3,4,1)
>>> y=torch.empty( 3,1,1)
# x and y are broadcastable.
# 1st trailing dimension: both have size 1
# 2nd trailing dimension: y has size 1
# 3rd trailing dimension: x size == y size
# 4th trailing dimension: y dimension doesn't exist ###########################################################################需要除掉两个数的后两个维度,然后再看是否满足广播机制
torch.matmul(tensor1tensor2out=None) → Tensor

Matrix product of two tensors.

The behavior depends on the dimensionality of the tensors as follows:

  • If both tensors are 1-dimensional, the dot product (scalar) is returned.

  • If both arguments are 2-dimensional, the matrix-matrix product is returned.

  • If the first argument is 1-dimensional and the second argument is 2-dimensional, a 1 is prepended to its dimension for the purpose of the matrix multiply. After the matrix multiply, the prepended dimension is removed.

  • If the first argument is 2-dimensional and the second argument is 1-dimensional, the matrix-vector product is returned.

  • If both arguments are at least 1-dimensional and at least one argument is N-dimensional (where N > 2), then a batched matrix multiply is returned. If the first argument is 1-dimensional, a 1 is prepended to its dimension for the purpose of the batched matrix multiply and removed after. If the second argument is 1-dimensional, a 1 is appended to its dimension for the purpose of the batched matrix multiple and removed after. The non-matrix (i.e. batch) dimensions are broadcasted (and thus must be broadcastable). For example, if tensor1 is a (j \times 1 \times n \times m)(j×1×n×m)tensor and tensor2 is a (k \times m \times p)(k×m×p) tensor, out will be an (j \times k \times n \times p)(j×k×n×p) tensor.

												

pytorch的matmul怎么广播的更多相关文章

  1. TensorFlow+TVM优化NMT神经机器翻译

    TensorFlow+TVM优化NMT神经机器翻译 背景 神经机器翻译(NMT)是一种自动化的端到端方法,具有克服传统基于短语的翻译系统中的弱点的潜力.本文为全球电子商务部署NMT服务. 目前,将Tr ...

  2. [深度学习] pytorch学习笔记(1)(数据类型、基础使用、自动求导、矩阵操作、维度变换、广播、拼接拆分、基本运算、范数、argmax、矩阵比较、where、gather)

    一.Pytorch安装 安装cuda和cudnn,例如cuda10,cudnn7.5 官网下载torch:https://pytorch.org/ 选择下载相应版本的torch 和torchvisio ...

  3. PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()

    torch.mul() 函数功能:逐个对 input 和 other 中对应的元素相乘. 本操作支持广播,因此 input 和 other 均可以是张量或者数字. 举例如下: >>> ...

  4. PyTorch 中 torch.matmul() 函数的文档详解

    官方文档 torch.matmul() 函数几乎可以用于所有矩阵/向量相乘的情况,其乘法规则视参与乘法的两个张量的维度而定. 关于 PyTorch 中的其他乘法函数可以看这篇博文,有助于下面各种乘法的 ...

  5. PyTorch 广播机制

    PyTorch 广播机制 定义 PyTorch的tensor参数可以自动扩展其大小.一般的是小一点的会变大,来满足运算需求. 规则 满足一下情况的tensor是可以广播的. 至少有一个维度 两个ten ...

  6. 『PyTorch』第五弹_深入理解Tensor对象_中下:数学计算以及numpy比较_&_广播原理简介

    一.简单数学操作 1.逐元素操作 t.clamp(a,min=2,max=4)近似于tf.clip_by_value(A, min, max),修剪值域. a = t.arange(0,6).view ...

  7. 【Pytorch】关于torch.matmul和torch.bmm的输出tensor数值不一致问题

    发现 对于torch.matmul和torch.bmm,都能实现对于batch的矩阵乘法: a = torch.rand((2,3,10))b = torch.rand((2,2,10))### ma ...

  8. pytorch & numpy广播法则

    广播法则 所有数组向维度最高的数组看齐,若维度不足则在最前面的维度用1补齐 扩展维度后,所有数组在某一维度相同或者长度为1,否则不能计算 当可以计算时,将长度为1的维度扩展为另一数组相应维度的长度 a ...

  9. 『PyTorch × TensorFlow』第十七弹_ResNet快速实现

    『TensorFlow』读书笔记_ResNet_V2 对比之前的复杂版本,这次的torch实现其实简单了不少,不过这和上面的代码实现逻辑过于复杂也有关系. 一.PyTorch实现 # Author : ...

随机推荐

  1. MySQL添加唯一索引

    1 语法如下 ALTER TABLE `t_user` ADD unique(`username`);

  2. (转)ORACLE中关于外键缺少索引的探讨和总结

    在ORACLE数据库中,定义外键约束时,ORACLE是不会自动创建对应索引的,必须手动在外键约束相关的列上创建索引.那么外键字段上是否有必要创建索引呢?如果有必要的话,巡检时,如何找出外键字段上没有创 ...

  3. K nearest neighbor cs229

    vectorized code 带来的好处. import numpy as np from sklearn.datasets import fetch_mldata import time impo ...

  4. CF #546 D.E

    D coun[i]表示[i]这个数右边有多少个数j能和他组成题中所给的二元组(i,j) 如果一个数的coun[i]=n-i-ans 那么说明他可以与最后一个交换 同时不计算贡献 因为它是向右走的 对左 ...

  5. javaWeb中的session和cookie

    Cookie Cookie 是浏览器提供的一种技术,通过服务器的程序能将一些只须保存在客户端,或者 在客户端进行处理的数据,放在本地的计算机上,不需要通过网络传输,因而提高网页处理的效率,并且能够减少 ...

  6. 点击startup.bat启动tomcat出现乱码

    找到tomcat目录下的/conf/logging.properties添加语句:java.util.logging.ConsoleHandler.encoding = GBK重启tomcat 问题解 ...

  7. HTTPClick调用WebApi帮助类

    1.创建ApiHelper类 2.复制以下代码到类中 using System; using System.Collections.Generic; using System.Linq; using ...

  8. 本地文件上传到gitlab

    1.本地创建目录gbdt_model 2.进入文件目录,在文件中点击鼠标右键选择bash控制台进入 3.运行git init 命令,文件中会多出一个.git 命令 4. git commit -m & ...

  9. 解决mysql提示服务无法启动问题

    1.管理员权限打开命令,进入mysql下bin文件夹 删除根目录下data文件夹没有不用管,重新安装 2. ---------------------------------------------- ...

  10. 02 Vue介绍与安装,指令系统 v-*、音乐播放器

    VUE文档 https://cn.vuejs.org/v2/guide/ 1.vue的介绍 尤雨溪 1.vue的优点 2.vue的介绍 3.vue的安装 4.声明式渲染 <body> &l ...