python - PyTorch:逐行点积

标签 python pytorch tensor dot-product

假设我有两个张量:

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/

相关文章:

python - DataLoader num_workers 与 torch.set_num_threads

cmake - 链接静态库 pytorch 在构建过程中找不到其内部函数

python - 调用 storage() 方法时,Pytorch Tensor 存储具有相同的 id

Python 元循环求值器

python - 减去 L8 带时 gdal_calc.py 中负值的饱和度

python - 网络集群和python

python - 沿第三轴的点积

python - 如何在 Django 中将 css 加载为静态

python - 如何将 torch 张量转换为 Pandas 数据帧?

tensorflow - "relu"在 tf.nn.relu 中代表什么?