简体   繁体   English

"numpy.dot 对于尺寸 > 2"

[英]numpy.dot for dimensions > 2

It may be easier to visualize this using the notation of np.einsum<\/code> .使用np.einsum<\/code>的表示法可能更容易将其可视化。 Start with regular 2D matrix multiplication, which is pretty unambiguous, and follows "normal math" rules:从规则的 2D 矩阵乘法开始,这是非常明确的,并遵循“普通数学”规则:

a = np.ones((2, 3))
b = np.ones((3, 4))
np.einsum('ij,jk->ik', a, b)  # Same as a.dot(b)

Here's my best attempt at making sense of the rule.这是我理解规则的最佳尝试。

Suppose that a is ap 1 x ... xp m-1 xp m array and that b is aq 1 x ... xq n-1 xq n array.假设a是 ap 1 x ... xp m-1 xp m数组并且b是 aq 1 x ... xq n-1 xq n数组。 The dot function seems to be interpreting a as an p 1 x ... xp m-2 array of p m-1 xp m matrices, and b as aq 1 x ... xq n-2 array of q n-1 xq n matrices. dot函数似乎将a解释为 p m-1 xp m矩阵的 p 1 x ... xp m-2数组,并将b解释为 aq 1 x ... xq n-2 q n-1 xq 数组n 个矩阵。

With that established, the entry dot(a,b)[i_1,...,i_(n-2),k_1,j_1,...,j_(m-2),k_2] is the k_1,k_2 entry of the matrix product dot(a[i_1,...,i_(n-2),:,:],b[j_1,...,j_(m-2),:,:]) of ap m-1 xp m with aq n-1 xq n .建立后,条目dot(a,b)[i_1,...,i_(n-2),k_1,j_1,...,j_(m-2),k_2]是的k_1,k_2条目ap m-1的矩阵乘积dot(a[i_1,...,i_(n-2),:,:],b[j_1,...,j_(m-2),:,:]) xp m与 aq n-1 xq n Notably, this product is only defined if p m =q n-1 .值得注意的是,仅当 p m =q n-1时才定义该乘积。

Standard matrix multiplication definition:标准矩阵乘法定义:

(AB)_{ij} = \\sum_k A_{ik} B_{kj}<\/a>

Which is equivalent to:这相当于:

dot(a, b)[i,j] = sum(a[i,:] * b[:,j])

First try to understand the case where both arrays are 2d.首先尝试理解两个数组都是二维的情况。

"the last axis of a and the second-to-last axis of b"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM