问题
I have 2 arrays x
and y
with shapes (2, 3, 3)
, respectively, (3, 3)
. I want to compute the dot product z
with shape (2, 3)
in the following way:
x = np.array([[[a111, a121, a131], [a211, a221, a231], [a311, a321, a331]],
[[a112, a122, a132], [a212, a222, a232], [a312, a322, a332]]])
y = np.array([[b11, b12, b13], [b21, b22, b23], [b31, b32, b33]])
z = np.array([[a111*b11+a121*b12+a131*b13, a211*b21+a221*b22+a231*b23, a311*b31+a321*b32+a331*b33],
[a112*b11+a122*b12+a132*b13, a212*b21+a222*b22+a232*b23, a312*b31+a322*b32+a332*b33]])
Any ideas on how to do this in a vectorized way?
回答1:
On the sum-reductions shown in the question, it seems the reduction is along the last axis, while keeping the second axis of x
aligned with the first axis of y
. Because of that requirement of axis-alignment, we can use np.einsum. Thus, one vectorized solution would be -
np.einsum('ijk,jk->ij',x, y)
Sample run -
In [255]: x
Out[255]:
array([[[5, 1, 7],
[2, 1, 7],
[5, 1, 2]],
[[6, 4, 7],
[3, 8, 1],
[1, 7, 7]]])
In [256]: y
Out[256]:
array([[5, 4, 7],
[8, 2, 5],
[2, 3, 3]])
In [260]: np.einsum('ijk,jk->ij',x, y)
Out[260]:
array([[78, 53, 19],
[95, 45, 44]])
In [261]: 5*5 + 1*4 + 7*7
Out[261]: 78
In [262]: 2*8 + 1*2 + 7*5
Out[262]: 53
来源:https://stackoverflow.com/questions/45943636/dot-product-between-2d-and-3d-numpy-arrays