- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
阅读pytorch文档后,我仍然需要帮助来理解torch.mm
、torch.matmul
和torch.mul
之间的区别.由于我不完全理解它们,我无法简明扼要地解释这一点。
B = torch.tensor([[ 1.1207],
[-0.3137],
[ 0.0700],
[ 0.8378]])
C = torch.tensor([[ 0.5146, 0.1216, -0.5244, 2.2382]])
print(torch.mul(B,C))
print(torch.matmul(B,C))
print(torch.mm(B,C))
所有三个都产生以下输出(即它们执行矩阵乘法):
tensor([[ 0.5767, 0.1363, -0.5877, 2.5084],
[-0.1614, -0.0381, 0.1645, -0.7021],
[ 0.0360, 0.0085, -0.0367, 0.1567],
[ 0.4311, 0.1019, -0.4393, 1.8752]])
A = torch.tensor([[1.8351,2.1536], [-0.8320,-1.4578]])
B = torch.tensor([[2.9355, 0.3450], [0.5708, 1.9957]])
print(torch.mul(A,B))
print(torch.matmul(A,B))
print(torch.mm(A,B))
产生了
不同输出。 torch.mm 不再执行矩阵乘法(改为广播并执行逐元素乘法,而其他两个仍执行矩阵乘法。
tensor([[ 5.3869, 0.7430],
[-0.4749, -2.9093]])
tensor([[ 6.6162, 4.9310],
[-3.2744, -3.1964]])
tensor([[ 6.6162, 4.9310],
[-3.2744, -3.1964]])
输入
tensor1 = torch.randn(10, 3, 4)
tensor2 = torch.randn(4)
tensor1 =
tensor([[[-0.2267, 0.6311, -0.5689, 1.2712],
[-0.0241, -0.5362, 0.5481, -0.4534],
[-0.9773, -0.6842, 0.6927, 0.3363]],
[[-2.6759, 0.7817, 2.6821, 0.7037],
[ 0.1804, 0.3938, -1.2235, 0.8729],
[-1.9873, -0.5030, 0.0945, 0.2688]],
[[ 0.4244, 1.7350, 0.0558, -0.1861],
[-0.9063, -0.4737, -0.4284, -0.3883],
[ 0.4827, -0.2628, 1.0084, 0.2769]],
[[ 0.2939, 0.4604, 0.8014, -1.8760],
[ 1.8807, 0.1623, 0.2344, -0.6221],
[ 1.3964, 3.1637, 0.7889, 0.1195]],
[[-0.7202, 1.4250, 2.4302, 1.4811],
[-0.2301, 0.6280, 0.5379, 0.5178],
[-2.1073, -1.4399, -0.9451, 0.8534]],
[[ 2.8178, -0.4451, -0.7871, -0.5198],
[ 0.2825, 1.0692, 0.1559, 1.2945],
[-0.5828, -1.6287, -2.0661, -0.4107]],
[[ 0.5077, -0.6349, -0.0160, -0.4477],
[-0.8070, 0.3746, 1.1852, 0.0351],
[-0.6454, 1.5877, 0.8561, 1.1021]],
[[ 0.1191, 1.0116, 0.5807, 1.2105],
[-0.5403, 1.2404, 1.1532, 0.6537],
[ 1.4757, -1.3648, -1.7158, -1.0289]],
[[-0.1326, 0.3715, 0.2429, -0.0794],
[ 0.3224, -0.3064, 0.1963, 0.7276],
[ 0.9098, 1.5984, -1.4953, 0.0420]],
[[ 0.1511, 0.9691, -0.5204, 0.3858],
[ 0.4566, 1.5482, -0.3401, 0.5960],
[-0.9998, 0.7198, 0.9286, 0.4498]]])
tensor2 =
tensor([-1.6350, 1.0335, -0.9023, 0.0696])
print(torch.mul(tensor1,tensor2))
print(torch.matmul(tensor1,tensor2))
print(torch.mm(tensor1,tensor2))
输出完全不同。我认为 torch.mul
广播并将矩阵的每 4 个元素乘以向量 tensor2,即 [-0.2267, 0.6311, -0.5689, 1.2712] x 张量 2
元素-wise, [-0.0241, -0.5362, 0.5481, -0.4534] x tensor 2
element-wise 等等。我不明白 torch.matmul
在做什么。我认为这与文档的第 5 个要点有关(如果两个参数......),但我无法理解这一点。 https://pytorch.org/docs/stable/generated/torch.matmul.html
我认为 torch.mm
无法产生输出的原因是它无法广播(如果我错了请纠正我)。
tensor([[[ 3.7071e-01, 6.5221e-01, 5.1335e-01, 8.8437e-02],
[ 3.9400e-02, -5.5417e-01, -4.9460e-01, -3.1539e-02],
[ 1.5979e+00, -7.0715e-01, -6.2499e-01, 2.3398e-02]],
[[ 4.3752e+00, 8.0790e-01, -2.4201e+00, 4.8957e-02],
[-2.9503e-01, 4.0699e-01, 1.1040e+00, 6.0723e-02],
[ 3.2494e+00, -5.1981e-01, -8.5253e-02, 1.8701e-02]],
[[-6.9397e-01, 1.7931e+00, -5.0379e-02, -1.2945e-02],
[ 1.4818e+00, -4.8954e-01, 3.8657e-01, -2.7010e-02],
[-7.8920e-01, -2.7163e-01, -9.0992e-01, 1.9265e-02]],
[[-4.8055e-01, 4.7582e-01, -7.2309e-01, -1.3051e-01],
[-3.0750e+00, 1.6770e-01, -2.1146e-01, -4.3281e-02],
[-2.2832e+00, 3.2697e+00, -7.1183e-01, 8.3139e-03]],
[[ 1.1775e+00, 1.4727e+00, -2.1928e+00, 1.0304e-01],
[ 3.7617e-01, 6.4900e-01, -4.8534e-01, 3.6025e-02],
[ 3.4455e+00, -1.4882e+00, 8.5277e-01, 5.9369e-02]],
[[-4.6072e+00, -4.6005e-01, 7.1024e-01, -3.6160e-02],
[-4.6191e-01, 1.1051e+00, -1.4067e-01, 9.0053e-02],
[ 9.5283e-01, -1.6833e+00, 1.8643e+00, -2.8571e-02]],
[[-8.3005e-01, -6.5622e-01, 1.4461e-02, -3.1148e-02],
[ 1.3195e+00, 3.8716e-01, -1.0694e+00, 2.4421e-03],
[ 1.0553e+00, 1.6409e+00, -7.7250e-01, 7.6669e-02]],
[[-1.9477e-01, 1.0455e+00, -5.2398e-01, 8.4209e-02],
[ 8.8343e-01, 1.2820e+00, -1.0405e+00, 4.5478e-02],
[-2.4128e+00, -1.4106e+00, 1.5482e+00, -7.1578e-02]],
[[ 2.1675e-01, 3.8391e-01, -2.1914e-01, -5.5219e-03],
[-5.2707e-01, -3.1668e-01, -1.7711e-01, 5.0619e-02],
[-1.4876e+00, 1.6520e+00, 1.3493e+00, 2.9198e-03]],
[[-2.4706e-01, 1.0015e+00, 4.6955e-01, 2.6842e-02],
[-7.4663e-01, 1.6001e+00, 3.0685e-01, 4.1462e-02],
[ 1.6347e+00, 7.4395e-01, -8.3792e-01, 3.1291e-02]]])
tensor([[ 1.6247, -1.0409, 0.2891],
[ 2.8120, 1.2767, 2.6630],
[ 1.0358, 1.3518, -1.9515],
[-0.8583, -3.1620, 0.2830],
[ 0.5605, 0.5759, 2.8694],
[-4.3932, 0.5925, 1.1053],
[-1.5030, 0.6397, 2.0004],
[ 0.4109, 1.1704, -2.3467],
[ 0.3760, -0.9702, 1.5165],
[ 1.2509, 1.2018, 1.5720]])
最佳答案
简而言之:
torch.mm
- 执行矩阵乘法无需广播 - (2D 张量) by (2D 张量)torch.mul
- 执行elementwise乘法广播 - (Tensor)by(张量或数字)torch.matmul
- 矩阵乘积带广播 - (张量)由(张量)根据张量形状(点产品、矩阵产品、批量矩阵产品)。一些细节:
torch.mm
- 执行矩阵乘法无需广播它需要两个二维张量,所以 n×m * m×p = n×p
来自文档 https://pytorch.org/docs/stable/generated/torch.mm.html :
This function does not broadcast. For broadcasting matrix products, see torch.matmul().
torch.mul
- 执行elementwise乘法广播 - (Tensor)by(张量或数字)文档:https://pytorch.org/docs/stable/generated/torch.mul.html
torch.mul
不执行矩阵乘法。它广播两个张量并执行逐元素乘法。因此,当您将它与张量 1x4 * 4x1 一起使用时,它的工作方式类似于:
import torch
a = torch.FloatTensor([[1], [2], [3]])
b = torch.FloatTensor([[1, 10, 100]])
a, b = torch.broadcast_tensors(a, b)
print(a)
print(b)
print(a * b)
tensor([[1., 1., 1.],
[2., 2., 2.],
[3., 3., 3.]])
tensor([[ 1., 10., 100.],
[ 1., 10., 100.],
[ 1., 10., 100.]])
tensor([[ 1., 10., 100.],
[ 2., 20., 200.],
[ 3., 30., 300.]])
torch.matmul
还是看官方文档比较好https://pytorch.org/docs/stable/generated/torch.matmul.html因为它根据输入张量使用不同的模式。它可以通过广播执行点积、矩阵-矩阵积或批量矩阵积。
关于您关于产品的问题:
tensor1 = torch.randn(10, 3, 4)
tensor2 = torch.randn(4)
它是一个产品的批处理版本。请检查这个简单的例子来理解:
import torch
# 3x1x3
a = torch.FloatTensor([[[1, 2, 3]], [[3, 4, 5]], [[6, 7, 8]]])
# 3
b = torch.FloatTensor([1, 10, 100])
r1 = torch.matmul(a, b)
r2 = torch.stack((
torch.matmul(a[0], b),
torch.matmul(a[1], b),
torch.matmul(a[2], b),
))
assert torch.allclose(r1, r2)
因此它可以被看作是多个操作在 batch 维度上堆叠在一起。
阅读有关广播的内容可能也很有用:
https://pytorch.org/docs/stable/notes/broadcasting.html#broadcasting-semantics
关于python-3.x - torch.mm、torch.matmul 和 torch.mul 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73924697/
我正在尝试使用这两种格式解析日期 2014-12-03T10:05:59.5646+08:00: yyyy-MM-dd'T'HH:mm:ss yyyy-MM-dd'T'HH:mm:ssXXX 当我使用
Paypal 返回以下格式的时间戳: yyyy-MM-ddTHH:mm:ssZ 我不知道该怎么办... 如何在 php 中使用本地时区将其转换为 yyyy-MM-dd HH:mm:ss? 我很想pre
我正在使用 Excel 2010 或 Excel 2007 导入包含日期/时间信息的 CSV 文件。我的 CSV 文件中的时间戳具有以下格式:yyyy-mm-dd hh:mm:ss。 (例如:2015
这个问题已经有答案了: Separate Date and Time objects (2 个回答) 已关闭 4 年前。 如何从 SimpleDateFormat("MM/dd/yyyy kk:mm"
这个问题已经有答案了: Java string to date conversion (17 个回答) 已关闭 6 年前。 我需要将日期字符串转换为另一种特定格式。 例如:我有一个日期,可以是 YYY
我想将字符串:24/11/2016 04:30 pm 转换为日期时间值:11/24/2016 04:30 pm。 我的代码为: DateTime date = DateTime.ParseExact(
我想使用 linux 将像“26/11/05 06:00:01,057000000”这样的纪元转换为 yyyy-mm-ddThh:mm:ss? 我曾尝试使用以下脚本但没有成功: echo 26/11/
这个问题在这里已经有了答案: mysql YYYY-MM-DDThh:mm:ss (1 个回答) 关闭 6 年前。 我想上传包含 yyyy-mm-ddThh:mm:ss.sssZ 数据的 csv 文
我在“dd-MM-yyyy HH:mm”中有一个字符串,需要将其转换为格式为日期的对象“yyyy-MM-dd HH:mm”。 下面是我用来转换的代码 oldScheduledDate = "16-05
我有一个数据框(df),它有一个日期列(列名:sale_date),它以以下格式存储数据 dd/mm/yy hh:mm:ss 我正在尝试将其转换为 yyyy-mm-dd hh:mm:ss。尝试了以下但
我的数据库中有日期时间列(格式为 YYYY-mm-dd hh:mm:ss)。 我需要将其转换为 dd-mm-YYYY hh:mm:ss 格式。 我该怎么办?帮助我。 最佳答案 不确定如何在 javas
想知道它们是否代表不同的格式或本质上相同(只是新与旧的演示文稿)。 最佳答案 基于DateTimeFormatter : Offset X and x: This formats the offset
如标题所示,我有一个问题。我需要将 LocalDataTime yyyy-MM-ssThh-mm-ss 解析为 LocalDataTime yyyy-MM-ss hh-mm-ss 但是当我这样做时 S
我想用 mySQL 将我的数据从“yyyy-mm-ddThh-mm-ss.sssZ”转换为“yyyy-mm-dd hh-mm-ss”。 我尝试使用 convert_tz: mysql> SELECT
这个问题在这里已经有了答案: LOAD DATA INFILE easily convert YYYYMMDD to YYYY-MM-DD? (1 个回答) 关闭 6 年前。 我正在尝试将 CSV
我需要更改 string 的日期格式。原始字符串的格式如下: var timeStamp = '2014/07/30 - 14:15:36' 这是我想要实现的日期格式: var timeStampAr
我正在尝试将 yyyy-MM-dd'T'HH:mm:ss.SSSz 格式的日期格式化为 yyyy-mm-dd HH:mm:ss,这应该很容易,但我无法获得它可以工作。 需要解析的日期格式为:2012-
我正在尝试通过传递时间戳作为命令行参数来使用 Synapse 管道运行 Spark 作业。在与 Spark 作业相关的代码运行之前,synapse 正在将字符串命令行参数值从 ISO 格式 2019-
我试过下面的代码: String created_Date = "25-Nov-15 14:23:34"; SimpleDateFormat sdf = new SimpleDateFormat("d
我创建了一个函数,它以与原始格式不同的格式返回日期。基本上,我正在使用此 Select MonthSub('2014-04-10',2)# 语句进行测试,它应该返回2014-02,而不是 2014-0
我是一名优秀的程序员,十分优秀!