torch.tensordot#
- torch.tensordot(a, b, dims=2, out=None)[source]#
返回一个沿多个维度收缩的 a 和 b。
tensordot
实现了一个广义矩阵乘法。- 参数
当使用非负整数参数
dims
= 调用时,以及a
和b
的维度分别为 和 时,tensordot()
计算当使用列表形式的
dims
时,给定的维度将被收缩,以替换a
的最后 个维度和 的前 个维度。这些维度的大小必须匹配,但tensordot()
会处理广播的维度。示例
>>> a = torch.arange(60.).reshape(3, 4, 5) >>> b = torch.arange(24.).reshape(4, 3, 2) >>> torch.tensordot(a, b, dims=([1, 0], [0, 1])) tensor([[4400., 4730.], [4532., 4874.], [4664., 5018.], [4796., 5162.], [4928., 5306.]]) >>> a = torch.randn(3, 4, 5, device='cuda') >>> b = torch.randn(4, 5, 6, device='cuda') >>> c = torch.tensordot(a, b, dims=2).cpu() tensor([[ 8.3504, -2.5436, 6.2922, 2.7556, -1.0732, 3.2741], [ 3.3161, 0.0704, 5.0187, -0.4079, -4.3126, 4.8744], [ 0.8223, 3.9445, 3.2168, -0.2400, 3.4117, 1.7780]]) >>> a = torch.randn(3, 5, 4, 6) >>> b = torch.randn(6, 4, 5, 3) >>> torch.tensordot(a, b, dims=([2, 1, 3], [1, 2, 0])) tensor([[ 7.7193, -2.4867, -10.3204], [ 1.5513, -14.4737, -6.5113], [ -0.2850, 4.2573, -3.5997]])