【问题标题】:torch.matmul gives RuntimeErrortorch.matmul 给出 RuntimeError
【发布时间】: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 的参数)

任何帮助将不胜感激

【问题讨论】:

    标签: pytorch runtime


    【解决方案1】:

    你得到错误是因为矩阵乘法的顺序错误。

    应该是:

    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
    

    【讨论】:

    • 非常感谢@planet,但我需要做torch.matmul(a,b),或者out=a @ b,有没有什么好的重塑方法让我可以做到这一点
    • 在这种情况下,你可以这样做:a = torch.randn(400, 32, 400).permute(0, 2, 1),然后你可以这样做out = a @ b
    猜你喜欢
    • 2020-08-12
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 2020-12-05
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多