Numpy计算自定义矩阵乘法
我有两个矩阵a,b
,我想创建一个3-D数组c
,以便
C[k,i,j]=A[k,j]*B[k,i]
我正在考虑使用np.einsum ,但找不到方法,我不确定在这里它是否有用。
一个天真的环是可能的,但听起来很效率。
更新
C=np.einsum('kj,ki->kij',A,B)
作品非常优雅,这是@warren Weckesser的答案之上
I have two matrices A,B
, and I'd like to create a 3-d array C
such that
C[k,i,j]=A[k,j]*B[k,i]
I was thinking about using np.einsum
but couldn't find a way and I'm not sure it's useful here.
A naïve loop is possible, but sounds quite inefficient.
UPDATE
C=np.einsum('kj,ki->kij',A,B)
works and is quite elegant, this is on top of @Warren Weckesser 's answer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
basic 广播将起作用,
例如,:] 具有形状(2、1、3)和
b [::::,none]
具有形状(2,4,, 1)。通过广播,这些表达式的产物将具有形状(2、4、3)。Basic broadcasting will work, e.g.
A[:,None,:]
has shape (2, 1, 3), andB[:,:,None]
has shape (2, 4, 1). With broadcasting, the product of those expressions will have shape (2, 4, 3).