简体   繁体   English

如何将 3D 阵列的内积转换为二维阵列?

[英]How to get inner product of 3D array to 2D array?

I have two Numpy array我有两个 Numpy 阵列

b=np.array([[1, 2, 3], [4, 5, 6]])
a=np.array([[[1, 2,1], [3, 4,1],[4,5,6],[6,7,8]], [[5, 6,1], [7, 8,1],[4,5,6],[6,7,8]]])

a.shape,b.shape
((2, 4, 3), (2, 3))

I want to calculate dot product of these array.我想计算这些数组的点积。 I tried below code:我尝试了以下代码:

s=np.flip(np.dot(a,b).transpose((0,2,1)),1)

but it throws below error:但它会引发以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-140-6ada0906cbfc> in <module>
----> 1 s=np.flip(np.dot(a,b).transpose((0,2,1)),1)

<__array_function__ internals> in dot(*args, **kwargs)

ValueError: shapes (2,4,3) and (2,3) not aligned: 3 (dim 2) != 2 (dim 0)

I want my resultant array should be of shape -(2,4)我希望我的结果数组的形状应该是 -(2,4)

How will I calculate this?我将如何计算这个? Plz suggest some other method to do that.请建议一些其他方法来做到这一点。

a 's shape should be (*, 3, 2) because b 's shape is (2, 3) . a的形状应该是(*, 3, 2)因为b的形状是(2, 3)

a_ · b = │ a11 a12 | * | b11 b12 b13 |
         │ a21 a22 |   | b21 b22 b23 |
         │ a31 a32 | 

where a_ is an element of a .其中a_是 a 的a元素。

With the following a :使用以下a

a=np.array([[
    [1, 2], [1, 3], [4, 1]
], [
    [4, 5] ,[6, 6], [7, 8]
], [
    [5, 6], [1, 7], [8, 1]
], [
    [4, 5], [6, 6] ,[7, 8]
]])

I got no error.我没有错误。 So try:所以试试:

np.dot(a.reshape(4, 3, 2), b)

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

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