Search code examples
pythonnumpymatrixmatrix-multiplication

Multiplying 3D matrix with 2D matrix


I have two matrices to multiply. One is the weight matrix W, whose size is 900x2x2. Another is input matrix I, whose size is 2x2.

I want to perform a summation over c = WI which will be a 900x1 matrix, but when I perform the operation it multiplies them and gives me a 900x2x2 matrix again.

Question #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)

Solution

  • 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)