小编AIB*_*all的帖子

torch.mm、torch.matmul 和 torch.mul 之间有什么区别?

torch.mm阅读完 pytorch 文档后,我仍然需要帮助来理解,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))
Run Code Online (Sandbox Code Playgroud)

所有三个都会产生以下输出(即它们执行矩阵乘法):

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]])
Run Code Online (Sandbox Code Playgroud)
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))
Run Code Online (Sandbox Code Playgroud)

产生不同的输出。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]])
Run Code Online (Sandbox Code Playgroud)

输入 …

python-3.x pytorch

18
推荐指数
1
解决办法
2万
查看次数

标签 统计

python-3.x ×1

pytorch ×1