发现

对于torch.matmul和torch.bmm,都能实现对于batch的矩阵乘法:

a = torch.rand((2,3,10))
b = torch.rand((2,2,10))
### matmal()
res1 = torch.matmul(a,b.transpose(1,2))
print res1
"""
...
[torch.FloatTensor of size 2x3x2]
"""
### bmm()
res2 = torch.bmm(a,b.transpose(1,2))
print res2
"""
...
[torch.FloatTensor of size 2x3x2]
"""
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
从打印出来的几位数字来看,嗯,是一样的,但是在用等式(或者torch.eq())检查是却发现了问题,竟然有很多不一样的元素

print torch.eq(res1,res2)
"""
(0 ,.,.) =
1 0
1 1
1 1

(1 ,.,.) =
0 1
1 1
1 1
[torch.ByteTensor of size 2x3x2]
"""
1
2
3
4
5
6
7
8
9
10
11
12
13
将一样的数值在ipython直接输出(print会截断位数)

>>>res1[0,0,0]
2.229752540588379
>>>res2[0,0,0]
2.229752540588379
1
2
3
4
再来看看不一样的

>>>res1[0,0,1]
3.035151720046997
>>>res2[0,0,1]
3.035151481628418
1
2
3
4
可以看到从小数点后位7位开始两个输出值出现了差异!

结论

所以说在tensor的同样操作下,出现不一致结果(精度上)的可能性很大,在做相等条件判断时需要注意,即使同样的输入同样的操作可能出现不一样的结果。
之后又尝试对于a,b的位置进行交换,竟然发现即使是同一个函数操作,如matmal(),matmul(a,b.transpose(1,2))和matmul(b,a.transpose(1,2)).transpose(1,2)结果也存在不一样的元素。
---------------------
作者:Laox1ao
来源:CSDN
原文:https://blog.csdn.net/laox1ao/article/details/79159303
版权声明:本文为博主原创文章,转载请附上博文链接!

【Pytorch】关于torch.matmul和torch.bmm的输出tensor数值不一致问题的更多相关文章

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

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

  2. pytorch中的math operation: torch.bmm()

    torch.bmm(batch1, batch2, out=None) → Tensor Performs a batch matrix-matrix product of matrices stor ...

  3. PyTorch 介绍 | AUTOMATIC DIFFERENTIATION WITH TORCH.AUTOGRAD

    训练神经网络时,最常用的算法就是反向传播.在该算法中,参数(模型权重)会根据损失函数关于对应参数的梯度进行调整. 为了计算这些梯度,PyTorch内置了名为 torch.autograd 的微分引擎. ...

  4. [pytorch笔记] torch.nn vs torch.nn.functional; model.eval() vs torch.no_grad(); nn.Sequential() vs nn.moduleList

    1. torch.nn与torch.nn.functional之间的区别和联系 https://blog.csdn.net/GZHermit/article/details/78730856 nn和n ...

  5. 小白学习之pytorch框架(4)-softmax回归(torch.gather()、torch.argmax()、torch.nn.CrossEntropyLoss())

    学习pytorch路程之动手学深度学习-3.4-3.7 置信度.置信区间参考:https://cloud.tencent.com/developer/news/452418 本人感觉还是挺好理解的 交 ...

  6. [深度学习] Pytorch学习(一)—— torch tensor

    [深度学习] Pytorch学习(一)-- torch tensor 学习笔记 . 记录 分享 . 学习的代码环境:python3.6 torch1.3 vscode+jupyter扩展 #%% im ...

  7. DEEP LEARNING WITH PYTORCH: A 60 MINUTE BLITZ | TORCH.AUTOGRAD

    torch.autograd 是PyTorch的自动微分引擎,用以推动神经网络训练.在本节,你将会对autograd如何帮助神经网络训练的概念有所理解. 背景 神经网络(NNs)是在输入数据上执行的嵌 ...

  8. PyTorch - torch.eq、torch.ne、torch.gt、torch.lt、torch.ge、torch.le

    PyTorch - torch.eq.torch.ne.torch.gt.torch.lt.torch.ge.torch.le 参考:https://flyfish.blog.csdn.net/art ...

  9. Pytorch本人疑问(1) torch.nn和torch.nn.functional之间的区别

    在写代码时发现我们在定义Model时,有两种定义方法: torch.nn.Conv2d()和torch.nn.functional.conv2d() 那么这两种方法到底有什么区别呢,我们通过下述代码看 ...

随机推荐

  1. UI:sqlite数据库

    使用sqllite的时候一些笔记 数据库(管理类),在工程 general 导入 动态链接库 libsqllite3.0.dylib  在.h文件里引入导入 sqllite3.h 头文件 在.h里面声 ...

  2. TensorFlow博客翻译——用TensorFlow在云端进行机器学习

    https://github.com/tensorflow/tensorflow 原文地址 Machine Learning in the Cloud, with TensorFlow Wednesd ...

  3. postgresql 9.4.4 源码安装

    Postgresql下载网址: http://www.postgresql.org/ftp/source/ 这里我们选择最新的稳定版版postgresql-9.4.4 的源码版 https://ftp ...

  4. Tensor Operation

    Main operation categories that encompass the operations of tensors. Reshaping operations Element-wis ...

  5. 第四篇(那些JAVA程序BUG中的常见单词)

    xxx cannot be resolved to a variable xxx无法解析为变量 resolve 解析

  6. Luogu P1113 杂务 【拓扑排序】 By cellur925

    题目传送门 这题我们一看就知道是拓扑排序,然而在如何转化问题上花了大工夫,一个小时后最后还是无奈看了题解qwq. 显然我们可以对于每个任务,从他的前导任务到他连一条边,最后我们可以得到一个DAG.在这 ...

  7. [CERC2017]Buffalo Barricades

    这个题目,扫描线+玄学** 大概操作就是用个扫描线从上往下扫. 博主有点懒,就直接贴代码了,但是我还是给大家贴个比较详细的博客,除了代码都可以看wym的博客,我基本上就是按wym大佬的思路来的,当然, ...

  8. TCP/IP网络协议基础

    实验楼学习网络协议传送门 一.TCP/IP简介 TCP/IP(Transmission Control Protocol/Internet Protocol)是传输控制协议和网络协议的简称,它定义了电 ...

  9. 题解报告:hdu 1576 A/B(exgcd、乘法逆元+整数快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n ...

  10. Java中的流(1)流简介

    简介 1.在java中stream代表一种数据流(源),java.io的底层数据元.(比作成水管)2.InputStream 比作进水管,水从里面流向你,你要接收,read3.OutputStream ...