【torch函数】torch.matmul——矩阵乘法

torch.matmul

是 PyTorch 中用于执行张量之间的矩阵乘法的函数。它可以用于两个张量之间的矩阵乘法,也可以用于高维张量之间的批量矩阵乘法。


import torch

# 两个张量之间的矩阵乘法
A = torch.tensor([[1, 2], [3, 4]])
B = torch.tensor([[5, 6], [7, 8]])
result = torch.matmul(A, B)
print(result)
# 输出:
# tensor([[19, 22],
#         [43, 50]])

# 高维张量之间的批量矩阵乘法
C = torch.randn(2, 3, 4)  # shape: (2, 3, 4)
D = torch.randn(2, 4, 5)  # shape: (2, 4, 5)
result_batch = torch.matmul(C, D)
print(result_batch.shape)  # 输出: torch.Size([2, 3, 5])

在矩阵乘法中,torch.matmul 会根据输入张量的维度自动进行广播,以便进行有效的矩阵乘法运算。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值