[英]numpy.dot for dimensions > 2
I am trying to understand how dot product works for dimensions more than 2.我试图了解点积如何适用于大于 2 的尺寸。
Thedocumentation<\/a> says:
文档<\/a>说:
![]()
dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])<\/code><\/pre><\/blockquote>
I don't understand this rule -- where does it come from?我不明白这个规则——它是从哪里来的? Why is it the last axis of a and the second-to-last axis of b?
"为什么它是a的最后一个轴和b的倒数第二个轴?
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:标准矩阵乘法定义:
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.