【发布时间】:2021-03-08 11:45:58
【问题描述】:
我有两个张量
t1=torch.Size([400, 32, 400])
t2= torch.Size([400, 32, 32])
当我执行这个
torch.matmul(t1,t2)
我收到了这个错误 RuntimeError:
预期张量在维度 1 处的大小为 400,但在维度 1 处的大小为 32 参数 #2 'batch2'(同时检查 bmm 的参数)
任何帮助将不胜感激
【问题讨论】:
我有两个张量
t1=torch.Size([400, 32, 400])
t2= torch.Size([400, 32, 32])
当我执行这个
torch.matmul(t1,t2)
我收到了这个错误 RuntimeError:
预期张量在维度 1 处的大小为 400,但在维度 1 处的大小为 32 参数 #2 'batch2'(同时检查 bmm 的参数)
任何帮助将不胜感激
【问题讨论】:
你得到错误是因为矩阵乘法的顺序错误。
应该是:
a = torch.randn(400, 32, 400)
b = torch.randn(400, 32, 32)
out = torch.matmul(b, a) # You performed torch.matmul(a, b)
# You can also do a simpler version of the matrix multiplication using the below code
out = b @ a
【讨论】:
a = torch.randn(400, 32, 400).permute(0, 2, 1),然后你可以这样做out = a @ b