了解tensordot

问题描述 投票:18回答:2

在我学会了如何使用einsum后,我现在正试图了解np.tensordot的工作原理。

但是,我有点失落,特别是关于参数axes的各种可能性。

要理解它,因为我从未练习过张量微积分,我使用以下示例:

A = np.random.randint(2, size=(2, 3, 5))
B = np.random.randint(2, size=(3, 2, 4))

在这种情况下,有什么不同的可能np.tensordot,你会如何手动计算?

python numpy linear-algebra tensor dot-product
2个回答
22
投票

使用tensordot的想法非常简单 - 我们输入数组和相应的轴,沿着这些轴减少总和。参与和减少的轴在输出中被移除,并且来自输入数组的所有剩余轴在输出中展开为不同的轴,保持输入阵列的馈送顺序。

让我们看几个带有一个和两个减少轴的样本案例,并交换输入位置,看看如何在输出中保留顺序。

I.一个减少的轴

输入:

 In [7]: A = np.random.randint(2, size=(2, 6, 5))
   ...:  B = np.random.randint(2, size=(3, 2, 4))
   ...: 

情况1:

In [9]: np.tensordot(A, B, axes=((0),(1))).shape
Out[9]: (6, 5, 3, 4)

A : (2, 6, 5) -> reduction of axis=0
B : (3, 2, 4) -> reduction of axis=1

Output : `(2, 6, 5)`, `(3, 2, 4)` ===(2 gone)==> `(6,5)` + `(3,4)` => `(6,5,3,4)`

案例#2(与案例#1相同,但输入被交换):

In [8]: np.tensordot(B, A, axes=((1),(0))).shape
Out[8]: (3, 4, 6, 5)

B : (3, 2, 4) -> reduction of axis=1
A : (2, 6, 5) -> reduction of axis=0

Output : `(3, 2, 4)`, `(2, 6, 5)` ===(2 gone)==> `(3,4)` + `(6,5)` => `(3,4,6,5)`.

II。两轴减和

输入:

In [11]: A = np.random.randint(2, size=(2, 3, 5))
    ...: B = np.random.randint(2, size=(3, 2, 4))
    ...: 

情况1:

In [12]: np.tensordot(A, B, axes=((0,1),(1,0))).shape
Out[12]: (5, 4)

A : (2, 3, 5) -> reduction of axis=(0,1)
B : (3, 2, 4) -> reduction of axis=(1,0)

Output : `(2, 3, 5)`, `(3, 2, 4)` ===(2,3 gone)==> `(5)` + `(4)` => `(5,4)`

案例#2:

In [14]: np.tensordot(B, A, axes=((1,0),(0,1))).shape
Out[14]: (4, 5)

B : (3, 2, 4) -> reduction of axis=(1,0)
A : (2, 3, 5) -> reduction of axis=(0,1)

Output : `(3, 2, 4)`, `(2, 3, 5)` ===(2,3 gone)==> `(4)` + `(5)` => `(4,5)`

我们可以将它扩展到尽可能多的轴。


4
投票

tensordot交换轴并重新整形输入,因此它可以将np.dot应用于2个2d阵列。然后它交换并重新形状回到目标。实验可能比解释更容易。没有特殊的张量数学,只是将dot扩展到更高维度。 tensor只是指超过2d的数组。如果你已经对einsum感到满意,那么将结果与之比较最简单。

样品测试,在1对轴上求和

In [823]: np.tensordot(A,B,[0,1]).shape
Out[823]: (3, 5, 3, 4)
In [824]: np.einsum('ijk,lim',A,B).shape
Out[824]: (3, 5, 3, 4)
In [825]: np.allclose(np.einsum('ijk,lim',A,B),np.tensordot(A,B,[0,1]))
Out[825]: True

另外,总结两个。

In [826]: np.tensordot(A,B,[(0,1),(1,0)]).shape
Out[826]: (5, 4)
In [827]: np.einsum('ijk,jim',A,B).shape
Out[827]: (5, 4)
In [828]: np.allclose(np.einsum('ijk,jim',A,B),np.tensordot(A,B,[(0,1),(1,0)]))
Out[828]: True

我们可以用(1,0)对做同样的事情。鉴于维度的混合我不认为还有另一种组合。

© www.soinside.com 2019 - 2024. All rights reserved.