问题
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