【问题标题】:PyTorch: Row-wise Dot ProductPyTorch:逐行点积
【发布时间】:2020-05-18 17:57:14
【问题描述】:

假设我有两个张量:

a = torch.randn(10, 1000, 1, 4)
b = torch.randn(10, 1000, 6, 4)

其中第三个索引是向量的索引。

我想对b 中的每个向量与a 中的向量进行点积。

为了说明,这就是我的意思:

dots = torch.Tensor(10, 1000, 6, 1)
for b in range(10):
     for c in range(1000):
           for v in range(6):
            dots[b,c,v] = torch.dot(b[b,c,v], a[b,c,0]) 

我将如何使用 Torch 函数实现这一点?

【问题讨论】:

    标签: python pytorch tensor dot-product


    【解决方案1】:
    a = torch.randn(10, 1000, 1, 4)
    b = torch.randn(10, 1000, 6, 4)
    
    c = torch.sum(a * b, dim=-1)
    
    print(c.shape)
    

    torch.Size([10, 1000, 6])

    c = c.unsqueeze(-1)
    print(c.shape)
    

    torch.Size([10, 1000, 6, 1])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 1970-01-01
      相关资源
      最近更新 更多