假设我有两个张量:
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])
我将如何使用手电筒功能实现这一目标?
最佳答案
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)
火炬大小([10, 1000, 6, 1])
关于python - PyTorch:逐行点积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61875963/