Multiplying 3D matrix with 2D matrix

大城市里の小女人 提交于 2019-12-20 04:27:06

问题


I have two matrices to multiply. One is weight matrix - W whose size is 900x2x2. Another is input matrix-I whose size is 2x2. Now I want to perform summation over c = WI which will be 900x1 matrix, but when I perform the operation it multiplies and gives me 900x2x2 matrix again.

Q 2) (related) So I made both of them 2D and multiplied 900x4 * 4x1 but that gives me an error saying

ValueError:operands could not be broadcast together with shapes (900,4) (4,1)


回答1:


It seems you are trying to lose the last two axes of the first array against the only two axes of the second weight array with that matrix-multiplication. We could translate that idea into NumPy code with np.tensordot and assuming arr1 and arr2 as the input arrays respectively, like so -

np.tensordot(arr1,arr2,axes=([1,2],[0,1]))

Another simpler way to put into NumPy code would be with np.einsum, like so -

np.einsum('ijk,jk',arr1,arr2)


来源:https://stackoverflow.com/questions/38785566/multiplying-3d-matrix-with-2d-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!